<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Repository\WidgetRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
#[ORM\Entity(repositoryClass: WidgetRepository::class)]
#[ApiResource]
class Widget
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
#[Groups([
'read:club:collection:getWidgets',
])]
private ?int $id = null;
#[ORM\Column(length: 255)]
#[Groups([
'read:club:collection:getWidgets',
])]
private ?string $title = null;
#[ORM\Column]
#[Assert\Range(
notInRangeMessage: 'You must be between {{ min }} and {{ max }} tall to enter',
min: 1,
max: 4,
)]
#[Groups([
'read:club:collection:getWidgets',
])]
private ?int $width = null;
#[ORM\Column(length: 255, nullable: true)]
#[Groups([
'read:club:collection:getWidgets',
])]
private ?string $link = null;
#[ORM\ManyToOne(inversedBy: 'widgets')]
#[ORM\JoinColumn(nullable: true, onDelete: "SET NULL")]
private ?Club $club = null;
#[ORM\Column(type: Types::TEXT)]
#[Groups([
'read:club:collection:getWidgets',
])]
private ?string $content = null;
#[ORM\Column]
#[Groups([
'read:club:collection:getWidgets',
])]
private ?int $position_x = null;
#[ORM\Column]
#[Groups([
'read:club:collection:getWidgets',
])]
private ?int $position_y = null;
#[ORM\Column]
#[Groups([
'read:club:collection:getWidgets',
])]
private ?int $height = null;
#[ORM\OneToMany(mappedBy: 'widget', targetEntity: Photo::class, cascade: ["persist"])]
private Collection $photos;
public function __construct()
{
$this->photos = new ArrayCollection();
}
public function getImageUrl(): string
{
$mainPhoto = $this->getMainPhoto();
if ($mainPhoto) {
return $mainPhoto->getImageLarge()->getUrl();
}
return 'https://localbeez.ri7.fr/assets/img/image_placeholder.jpg';
}
public function getMainPhoto()
{
foreach ($this->photos as $p) {
if ($p->isMain()) {
return $p;
}
}
if (sizeof($this->photos) > 0) {
return $this->photos[0];
}
return false;
}
public function getSmallImageUrl(): string
{
$mainPhoto = $this->getMainPhoto();
if ($mainPhoto) {
return $mainPhoto->getImageSmall()->getUrl();
}
return 'https://localbeez.ri7.fr/assets/img/image_placeholder.jpg';
}
public function getMediumImageUrl(): string
{
$mainPhoto = $this->getMainPhoto();
if ($mainPhoto) {
return $mainPhoto->getImageMedium()->getUrl();
}
return 'https://localbeez.ri7.fr/assets/img/image_placeholder.jpg';
}
public function getId(): ?int
{
return $this->id;
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(string $title): self
{
$this->title = $title;
return $this;
}
public function getWidth(): ?int
{
return $this->width;
}
public function setWidth(int $width): self
{
$this->width = $width;
return $this;
}
public function getLink(): ?string
{
return $this->link;
}
public function setLink(string $link): self
{
$this->link = $link;
return $this;
}
public function getClub(): ?Club
{
return $this->club;
}
public function setClub(?Club $club): self
{
$this->club = $club;
return $this;
}
public function getContent(): ?string
{
return $this->content;
}
public function setContent(string $content): static
{
$this->content = $content;
return $this;
}
public function getPositionX(): ?int
{
return $this->position_x;
}
public function setPositionX(int $position_x): static
{
$this->position_x = $position_x;
return $this;
}
public function getPositionY(): ?int
{
return $this->position_y;
}
public function setPositionY(int $position_y): static
{
$this->position_y = $position_y;
return $this;
}
public function getHeight(): ?int
{
return $this->height;
}
public function setHeight(int $height): static
{
$this->height = $height;
return $this;
}
/**
* @return Collection<int, Photo>
*/
public function getPhotos(): Collection
{
return $this->photos;
}
public function addPhoto(Photo $photo): static
{
if (!$this->photos->contains($photo)) {
$this->photos->add($photo);
$photo->setWidget($this);
}
return $this;
}
public function removePhoto(Photo $photo): static
{
if ($this->photos->removeElement($photo)) {
// set the owning side to null (unless already changed)
if ($photo->getWidget() === $this) {
$photo->setWidget(null);
}
}
return $this;
}
}