<?php
namespace App\Entity;
use App\Repository\SlideRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Sylius\Component\Core\Model\ImageInterface;
use Sylius\Component\Core\Model\ImagesAwareInterface;
use Sylius\Component\Resource\Model\ResourceInterface;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity(repositoryClass=SlideRepository::class)
*/
class Slide implements ResourceInterface, ImagesAwareInterface
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @Assert\NotBlank()
* @ORM\Column(type="string", length=255)
*/
private $title;
/**
* @Assert\NotBlank()
* @ORM\Column(type="text", nullable=true)
*/
private $content;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $url;
/**
* @ORM\OneToMany(targetEntity=SlideImage::class, mappedBy="owner", cascade={"all"}, orphanRemoval=true)
*/
private $images;
public function __construct()
{
$this->images = new ArrayCollection();
$this->addImage(new SlideImage());
}
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 getContent(): ?string
{
return $this->content;
}
public function setContent(?string $content): self
{
$this->content = $content;
return $this;
}
public function getUrl(): ?string
{
return $this->url;
}
public function setUrl(?string $url): self
{
$this->url = $url;
return $this;
}
/**
* @return Collection<int, SlideImage>
*/
public function getImages(): Collection
{
return $this->images;
}
public function addImage(ImageInterface $image): void
{
if (!$this->images->contains($image)) {
$this->images[] = $image;
$image->setOwner($this);
}
}
public function removeImage(ImageInterface $image): void
{
if ($this->images->removeElement($image)) {
// set the owning side to null (unless already changed)
if ($image->getOwner() === $this) {
$image->setOwner(null);
}
}
}
/**
* {@inheritdoc}
*/
public function getImagesByType(string $type): Collection
{
return $this->images->filter(function (ImageInterface $image) use ($type) {
return $type === $image->getType();
});
}
/**
* {@inheritdoc}
*/
public function hasImages(): bool
{
return !$this->images->isEmpty();
}
/**
* {@inheritdoc}
*/
public function hasImage(ImageInterface $image): bool
{
return $this->images->contains($image);
}
public function getImage(): ?ImageInterface
{
return $this->getImages()->first()?:null;
}
}