src/Entity/Ad.php line 21

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use ApiPlatform\Core\Annotation\ApiResource;
  4. use App\Repository\AdRepository;
  5. use DateTime;
  6. use Doctrine\Common\Collections\ArrayCollection;
  7. use Doctrine\Common\Collections\Collection;
  8. use Doctrine\DBAL\Types\Types;
  9. use Doctrine\ORM\Mapping as ORM;
  10. use Gedmo\Timestampable\Traits\TimestampableEntity;
  11. use Symfony\Component\Serializer\Annotation\Groups;
  12. #[ORM\Entity(repositoryClassAdRepository::class)]
  13. #[ApiResource(
  14.     normalizationContext: [
  15.         'groups' => 'read:ad',
  16.     ]
  17. )]
  18. class Ad
  19. {
  20.     #[ORM\Id]
  21.     #[ORM\GeneratedValue]
  22.     #[ORM\Column]
  23.     #[Groups([
  24.         'read:club:collection:getAds',
  25.         'read:ad',
  26.     ])]
  27.     private ?int $id null;
  28.     use TimestampableEntity;
  29.     #[ORM\Column(length255)]
  30.     #[Groups([
  31.         'read:club:collection:getAds',
  32.         'read:ad',
  33.     ])]
  34.     private ?string $title null;
  35.     #[ORM\Column(typeTypes::TEXTnullabletrue)]
  36.     #[Groups([
  37.         'read:club:collection:getAds',
  38.         'read:ad',
  39.     ])]
  40.     private ?string $content null;
  41.     #[ORM\Column(length255)]
  42.     #[Groups([
  43.         'read:club:collection:getAds',
  44.         'read:ad',
  45.     ])]
  46.     private ?string $type null;
  47.     #[ORM\OneToMany(mappedBy'ad'targetEntityPhoto::class, cascade: ["persist"])]
  48.     #[Groups([
  49.         'read:ad',
  50.     ])]
  51.     private Collection $photos;
  52.     #[ORM\ManyToOne(inversedBy'ads')]
  53.     #[ORM\JoinColumn(nullablefalseonDelete"CASCADE")]
  54.     #[Groups([
  55.         'read:ad',
  56.     ])]
  57.     private ?Club $club null;
  58.     #[ORM\ManyToOne(inversedBy'ads')]
  59.     #[Groups([
  60.         'read:club:collection:getAds',
  61.         'read:ad',
  62.     ])]
  63.     private ?User $author null;
  64.     #[ORM\ManyToOne(inversedBy'ads')]
  65.     #[ORM\JoinColumn(nullablefalseonDelete"CASCADE")]
  66.     #[Groups([
  67.         'read:club:collection:getAds',
  68.         'read:ad',
  69.     ])]
  70.     private ?AdCategory $category null;
  71.     public function __construct()
  72.     {
  73.         $this->photos = new ArrayCollection();
  74.         $this->ads = new ArrayCollection();
  75.     }
  76.     #[Groups([
  77.         'read:club:collection:getAds',
  78.         'read:ad',
  79.     ])]
  80.     public function getCreatedAt(): ?DateTime
  81.     {
  82.         return $this->createdAt;
  83.     }
  84.     #[Groups([
  85.         'read:club:collection:getAds',
  86.         'read:ad',
  87.     ])]
  88.     public function getImageUrl(): string
  89.     {
  90.         $mainPhoto $this->getMainPhoto();
  91.         if ($mainPhoto) {
  92.             return $mainPhoto->getImageLarge()->getUrl();
  93.         }
  94.         return 'https://localbeez.ri7.fr/assets/img/image_placeholder.jpg';
  95.     }
  96.     public function getMainPhoto()
  97.     {
  98.         foreach ($this->photos as $p) {
  99.             if ($p->isMain()) {
  100.                 return $p;
  101.             }
  102.         }
  103.         if (sizeof($this->photos) > 0) {
  104.             return $this->photos[0];
  105.         }
  106.         return false;
  107.     }
  108.     #[Groups([
  109.         'read:club:collection:getAds',
  110.         'read:ad',
  111.     ])]
  112.     public function getSmallImageUrl(): string
  113.     {
  114.         $mainPhoto $this->getMainPhoto();
  115.         if ($mainPhoto) {
  116.             return $mainPhoto->getImageMedium()->getUrl();
  117.         }
  118.         return 'https://localbeez.ri7.fr/assets/img/image_placeholder.jpg';
  119.     }
  120.     #[Groups([
  121.         'read:club:collection:getAds',
  122.         'read:ad',
  123.     ])]
  124.     public function getMediumImageUrl(): string
  125.     {
  126.         $mainPhoto $this->getMainPhoto();
  127.         if ($mainPhoto) {
  128.             return $mainPhoto->getImageMedium()->getUrl();
  129.         }
  130.         return 'https://localbeez.ri7.fr/assets/img/image_placeholder.jpg';
  131.     }
  132.     public function getId(): ?int
  133.     {
  134.         return $this->id;
  135.     }
  136.     public function getTitle(): ?string
  137.     {
  138.         return $this->title;
  139.     }
  140.     public function setTitle(string $title): self
  141.     {
  142.         $this->title $title;
  143.         return $this;
  144.     }
  145.     public function getContent(): ?string
  146.     {
  147.         return $this->content;
  148.     }
  149.     public function setContent(?string $content): self
  150.     {
  151.         $this->content $content;
  152.         return $this;
  153.     }
  154.     public function getType(): ?string
  155.     {
  156.         return $this->type;
  157.     }
  158.     public function setType(string $type): self
  159.     {
  160.         $this->type $type;
  161.         return $this;
  162.     }
  163.     /**
  164.      * @return Collection<int, Photo>
  165.      */
  166.     public function getPhotos(): Collection
  167.     {
  168.         return $this->photos;
  169.     }
  170.     public function addPhoto(Photo $photo): self
  171.     {
  172.         if (!$this->photos->contains($photo)) {
  173.             $this->photos->add($photo);
  174.             $photo->setAd($this);
  175.         }
  176.         return $this;
  177.     }
  178.     public function removePhoto(Photo $photo): self
  179.     {
  180.         if ($this->photos->removeElement($photo)) {
  181.             // set the owning side to null (unless already changed)
  182.             if ($photo->getAd() === $this) {
  183.                 $photo->setAd(null);
  184.             }
  185.         }
  186.         return $this;
  187.     }
  188.     public function getClub(): ?Club
  189.     {
  190.         return $this->club;
  191.     }
  192.     public function setClub(?Club $club): self
  193.     {
  194.         $this->club $club;
  195.         return $this;
  196.     }
  197.     public function getAuthor(): ?User
  198.     {
  199.         return $this->author;
  200.     }
  201.     public function setAuthor(?User $author): self
  202.     {
  203.         $this->author $author;
  204.         return $this;
  205.     }
  206.     public function getCategory(): ?AdCategory
  207.     {
  208.         return $this->category;
  209.     }
  210.     public function setCategory(?AdCategory $category): self
  211.     {
  212.         $this->category $category;
  213.         return $this;
  214.     }
  215. }