src/Entity/Article.php line 58

  1. <?php
  2. namespace App\Entity;
  3. use ApiPlatform\Doctrine\Orm\Filter\BooleanFilter;
  4. use ApiPlatform\Doctrine\Orm\Filter\OrderFilter;
  5. use ApiPlatform\Doctrine\Orm\Filter\SearchFilter;
  6. use ApiPlatform\Metadata\ApiFilter;
  7. use ApiPlatform\Metadata\ApiResource;
  8. use ApiPlatform\Metadata\GetCollection;
  9. use ApiPlatform\Metadata\Get;
  10. use ApiPlatform\Metadata\Link;
  11. use ApiPlatform\Metadata\Patch;
  12. use ApiPlatform\Metadata\Post;
  13. use App\Controller\Api\Article\ApiEstablishmentArticleNewController;
  14. use App\Repository\ArticleRepository;
  15. use App\State\UpdateArticleProcessor;
  16. use App\Trait\ActivableTrait;
  17. use App\Trait\ArrayableTrait;
  18. use App\Trait\AuthorTrait;
  19. use App\Trait\CurrentStateTrait;
  20. use App\Trait\DateTrait;
  21. use App\Trait\DeletableTrait;
  22. use DateTimeImmutable;
  23. use DateTimeInterface;
  24. use Doctrine\Common\Collections\ArrayCollection;
  25. use Doctrine\Common\Collections\Collection;
  26. use Doctrine\DBAL\Types\Types;
  27. use Doctrine\ORM\Mapping as ORM;
  28. use Symfony\Component\Serializer\Annotation\Groups;
  29. use Tchoulom\ViewCounterBundle\Model\ViewCountable;
  30. #[ApiResource(
  31.     operations: [
  32.         new GetCollection(
  33.             normalizationContext: ['groups' => ['article:list']]
  34.         ),
  35.         new GetCollection(
  36.             uriTemplate"/departments/{departmentId}/articles",
  37.             normalizationContext: ['groups' => ['article:list']]
  38.         ),
  39.         new Get(),
  40.         new Post(
  41.             controllerApiEstablishmentArticleNewController::class,
  42.             denormalizationContext: ['groups' => ['article:new']]
  43.         ),
  44.         new Patch(
  45.             denormalizationContext: ['groups' => ['article:patch']],
  46.             processorUpdateArticleProcessor::class
  47.         )
  48.     ],
  49.     normalizationContext: ['groups' => ['article:list''article:view']],
  50. )]
  51. #[ApiFilter(OrderFilter::class, properties: ['id' => 'DESC'])]
  52. #[ApiFilter(SearchFilter::class, properties: ['id' => 'exact''type' => 'exact''inLanguage' => 'partial'])]
  53. #[ApiFilter(BooleanFilter::class, properties: ['isActive'])]
  54. #[ORM\Entity(repositoryClassArticleRepository::class)]
  55. class Article implements ViewCountable
  56. {
  57.     const TYPE_PHYSICS 0;
  58.     const TYPE_SERVICE 1;
  59.     const TYPE_SUBSCRIPTION 2;
  60.     const TYPES = [
  61.         'Physique' => self::TYPE_PHYSICS,
  62.         'Service' => self::TYPE_SERVICE,
  63.         'Abonnement' => self::TYPE_SUBSCRIPTION,
  64.     ];
  65.     use ArrayableTrait;
  66.     use DateTrait {DateTrait::__construct as private dateConstruct;}
  67.     use AuthorTrait;
  68.     use ActivableTrait;
  69.     use CurrentStateTrait;
  70.     use DeletableTrait;
  71.     #[ORM\Id]
  72.     #[ORM\GeneratedValue]
  73.     #[ORM\Column]
  74.     #[Groups(['article:list'])]
  75.     private ?int $id null;
  76.     #[ORM\Column(length255)]
  77.     #[Groups(['article:list''article:new''article:patch'])]
  78.     private ?string $name null;
  79.     #[ORM\Column(length255nullabletrue)]
  80.     #[Groups(['article:list''article:new''article:patch'])]
  81.     private ?string $slug null;
  82.     #[ORM\Column(typeTypes::TEXT)]
  83.     #[Groups(['article:list''article:new''article:patch'])]
  84.     private ?string $content null;
  85.     #[ORM\ManyToOne(cascade: ["persist"])]
  86.     #[ORM\JoinColumn(nullabletrue)]
  87.     #[Groups(['article:list'])]
  88.     private ?Attachment $thumbnail null;
  89.     #[ORM\Column]
  90.     #[Groups(['article:list''article:new''article:patch''order:list'])]
  91.     private ?float $amount null;
  92.     #[ORM\Column]
  93.     #[Groups(['article:list''article:new''article:patch'])]
  94.     private ?bool $isPromote false;
  95.     #[ORM\Column(nullabletrue)]
  96.     #[Groups(['article:list''article:new''article:patch'])]
  97.     private ?float $promotePrice null;
  98.     #[ORM\ManyToOne]
  99.     #[ORM\JoinColumn(nullablefalse)]
  100.     #[Groups(['article:list''article:patch''order:list''article:new'])]
  101.     private ?Currency $currency null;
  102.     #[ORM\Column(length255nullabletrue)]
  103.     #[Groups(['article:list''article:new''article:patch''order:list'])]
  104.     private ?string $shortcode null;
  105.     #[ORM\Column(nullabletrue)]
  106.     #[Groups(['article:list''article:new'])]
  107.     private ?int $quantity null;
  108.     #[ORM\Column(length255)]
  109.     #[Groups(['article:list''article:new''article:patch''order:list'])]
  110.     private ?string $type null;
  111.     #[ORM\ManyToMany(targetEntityOption::class, cascade: ["persist"])]
  112.     #[Groups(['article:view''article:new'])]
  113.     private Collection $options;
  114.     #[ORM\ManyToMany(targetEntityCollections::class, inversedBy'articles')]
  115.     private Collection $collections;
  116.     #[ORM\ManyToOne(inversedBy'articles')]
  117.     #[ORM\JoinColumn(nullablefalse)]
  118.     private ?Establishment $establishment null;
  119.     #[ORM\Column]
  120.     #[Groups(['article:list''article:new''article:patch'])]
  121.     private ?bool $isTaxable false;
  122.     #[ORM\OneToMany(mappedBy'article'targetEntityArticleDepartment::class, orphanRemovaltrue)]
  123.     #[Groups(['article:view'])]
  124.     private Collection $departments;
  125.     #[ORM\Column(typeTypes::DATETIME_MUTABLEnullabletrue)]
  126.     #[Groups(['article:view''article:patch'])]
  127.     private ?DateTimeInterface $expiryAt null;
  128.     #[ORM\Column(typeTypes::DATETIME_MUTABLEnullabletrue)]
  129.     #[Groups(['article:view'])]
  130.     private ?DateTimeInterface $lastSupplyAt null;
  131.     #[ORM\OneToMany(mappedBy'article'targetEntityArticleSupply::class, orphanRemovaltrue)]
  132.     private Collection $supplies;
  133.     #[ORM\Column(nullabletrue)]
  134.     #[Groups(['article:view'])]
  135.     private ?int $minimum null;
  136.     #[ORM\Column(nullabletrue)]
  137.     #[Groups(['article:list''article:new''article:patch'])]
  138.     private ?int $lowQuantity null;
  139.     #[ORM\Column]
  140.     #[Groups(['article:list''article:new''article:patch'])]
  141.     private ?bool $isBookable false;
  142.     #[ORM\Column(nullabletrue)]
  143.     #[Groups(['article:list''article:new''article:patch'])]
  144.     private ?int $reservationTime null;
  145.     #[ORM\Column(nullabletrue)]
  146.     #[Groups(['article:list''article:new''article:patch'])]
  147.     private ?float $reservationMargin null;
  148.     #[ORM\Column(nullabletrue)]
  149.     private ?array $controls null;
  150.     #[ORM\ManyToOne]
  151.     #[Groups(['article:list''article:new'])]
  152.     private ?Tax $tax null;
  153.     #[ORM\Column(nullabletrue)]
  154.     private ?int $views 0;
  155.     #[ORM\OneToMany(mappedBy'article'targetEntityViewCounter::class)]
  156.     private Collection $viewCounters;
  157.     public function __construct()
  158.     {
  159.         $this->dateConstruct();
  160.         $this->currentState 'draft';
  161.         $this->isActive true;
  162.         $this->options = new ArrayCollection();
  163.         $this->collections = new ArrayCollection();
  164.         $this->departments = new ArrayCollection();
  165.         $this->supplies = new ArrayCollection();
  166.         $this->viewCounters = new ArrayCollection();
  167.     }
  168.     public function getId(): ?int
  169.     {
  170.         return $this->id;
  171.     }
  172.     /**
  173.      * @param int|null $id
  174.      *
  175.      * @return $this
  176.      */
  177.     public function setId(?int $id): self
  178.     {
  179.         $this->id $id;
  180.         return $this;
  181.     }
  182.     public function getName(): ?string
  183.     {
  184.         return $this->name;
  185.     }
  186.     public function getNameOrder(): ?string
  187.     {
  188.         return "{$this->getName()} ({$this->getAmount()} {$this->getCurrency()?->getSymbol()})";
  189.     }
  190.     public function setName(string $name): self
  191.     {
  192.         $this->name $name;
  193.         return $this;
  194.     }
  195.     public function getSlug(): ?string
  196.     {
  197.         return $this->slug;
  198.     }
  199.     public function setSlug(?string $slug): self
  200.     {
  201.         $this->slug $slug;
  202.         return $this;
  203.     }
  204.     public function getContent(): ?string
  205.     {
  206.         return $this->content;
  207.     }
  208.     public function setContent(string $content): self
  209.     {
  210.         $this->content $content;
  211.         return $this;
  212.     }
  213.     public function getThumbnail(): ?Attachment
  214.     {
  215.         return $this->thumbnail;
  216.     }
  217.     public function setThumbnail(?Attachment $thumbnail): self
  218.     {
  219.         $this->thumbnail $thumbnail;
  220.         return $this;
  221.     }
  222.     public function getAmount(): ?float
  223.     {
  224.         return $this->amount;
  225.     }
  226.     public function setAmount(float $amount): self
  227.     {
  228.         $this->amount $amount;
  229.         return $this;
  230.     }
  231.     public function isIsPromote(): ?bool
  232.     {
  233.         return $this->isPromote;
  234.     }
  235.     public function setIsPromote(bool $isPromote): self
  236.     {
  237.         $this->isPromote $isPromote;
  238.         return $this;
  239.     }
  240.     public function getPromotePrice(): ?float
  241.     {
  242.         return $this->promotePrice;
  243.     }
  244.     public function setPromotePrice(?float $promotePrice): self
  245.     {
  246.         $this->promotePrice $promotePrice;
  247.         return $this;
  248.     }
  249.     public function getCurrency(): ?Currency
  250.     {
  251.         return $this->currency;
  252.     }
  253.     public function setCurrency(?Currency $currency): self
  254.     {
  255.         $this->currency $currency;
  256.         return $this;
  257.     }
  258.     public function getShortcode(): ?string
  259.     {
  260.         return $this->shortcode;
  261.     }
  262.     public function setShortcode(?string $shortcode): self
  263.     {
  264.         $this->shortcode $shortcode;
  265.         return $this;
  266.     }
  267.     public function getQuantity(): ?int
  268.     {
  269.         return $this->quantity;
  270.     }
  271.     public function setQuantity(?int $quantity): self
  272.     {
  273.         $this->quantity $quantity;
  274.         return $this;
  275.     }
  276.     public function getType(): string
  277.     {
  278.         return $this->type;
  279.     }
  280.     public function getTypeName(): ?string
  281.     {
  282.         return array_flip(self::TYPES)[$this->getType()];
  283.     }
  284.     public function setType(string $type): self
  285.     {
  286.         $this->type $type;
  287.         return $this;
  288.     }
  289.     /**
  290.      * @return Collection<int, Option>
  291.      */
  292.     public function getOptions(): Collection
  293.     {
  294.         return $this->options;
  295.     }
  296.     public function addOption(Option $option): self
  297.     {
  298.         if (!$this->options->contains($option)) {
  299.             $this->options->add($option);
  300.         }
  301.         return $this;
  302.     }
  303.     public function removeOption(Option $option): self
  304.     {
  305.         $this->options->removeElement($option);
  306.         return $this;
  307.     }
  308.     /**
  309.      * @return Collection<int, Collections>
  310.      */
  311.     public function getCollections(): Collection
  312.     {
  313.         return $this->collections;
  314.     }
  315.     public function addCollection(Collections $collection): self
  316.     {
  317.         if (!$this->collections->contains($collection)) {
  318.             $this->collections->add($collection);
  319.         }
  320.         return $this;
  321.     }
  322.     public function removeCollection(Collections $collection): self
  323.     {
  324.         $this->collections->removeElement($collection);
  325.         return $this;
  326.     }
  327.     public function getEstablishment(): ?Establishment
  328.     {
  329.         return $this->establishment;
  330.     }
  331.     public function setEstablishment(?Establishment $establishment): self
  332.     {
  333.         $this->establishment $establishment;
  334.         return $this;
  335.     }
  336.     public function isIsTaxable(): ?bool
  337.     {
  338.         return $this->isTaxable;
  339.     }
  340.     public function setIsTaxable(bool $isTaxable): self
  341.     {
  342.         $this->isTaxable $isTaxable;
  343.         return $this;
  344.     }
  345.     /**
  346.      * @return Collection<int, ArticleDepartment>
  347.      */
  348.     public function getDepartments(): Collection
  349.     {
  350.         return $this->departments;
  351.     }
  352.     public function addDepartment(ArticleDepartment $department): self
  353.     {
  354.         if (!$this->departments->contains($department)) {
  355.             $this->departments->add($department);
  356.             $department->setArticle($this);
  357.         }
  358.         return $this;
  359.     }
  360.     public function removeDepartment(ArticleDepartment $department): self
  361.     {
  362.         if ($this->departments->removeElement($department)) {
  363.             // set the owning side to null (unless already changed)
  364.             if ($department->getArticle() === $this) {
  365.                 $department->setArticle(null);
  366.             }
  367.         }
  368.         return $this;
  369.     }
  370.     public function getExpiryAt(): ?DateTimeInterface
  371.     {
  372.         return $this->expiryAt;
  373.     }
  374.     public function setExpiryAt(?DateTimeInterface $expiryAt): self
  375.     {
  376.         $this->expiryAt $expiryAt;
  377.         return $this;
  378.     }
  379.     public function getLastSupplyAt(): ?DateTimeInterface
  380.     {
  381.         return $this->lastSupplyAt;
  382.     }
  383.     public function setLastSupplyAt(?DateTimeInterface $lastSupplyAt): self
  384.     {
  385.         $this->lastSupplyAt $lastSupplyAt;
  386.         return $this;
  387.     }
  388.     /**
  389.      * @return Collection<int, ArticleSupply>
  390.      */
  391.     public function getSupplies(): Collection
  392.     {
  393.         return $this->supplies;
  394.     }
  395.     public function addSupply(ArticleSupply $supply): self
  396.     {
  397.         if (!$this->supplies->contains($supply)) {
  398.             $this->supplies->add($supply);
  399.             $supply->setArticle($this);
  400.         }
  401.         return $this;
  402.     }
  403.     public function removeSupply(ArticleSupply $supply): self
  404.     {
  405.         if ($this->supplies->removeElement($supply)) {
  406.             // set the owning side to null (unless already changed)
  407.             if ($supply->getArticle() === $this) {
  408.                 $supply->setArticle(null);
  409.             }
  410.         }
  411.         return $this;
  412.     }
  413.     public function getMinimum(): ?int
  414.     {
  415.         return $this->minimum;
  416.     }
  417.     public function setMinimum(?int $minimum): self
  418.     {
  419.         $this->minimum $minimum;
  420.         return $this;
  421.     }
  422.     public function getLowQuantity(): ?int
  423.     {
  424.         return $this->lowQuantity;
  425.     }
  426.     public function setLowQuantity(?int $lowQuantity): self
  427.     {
  428.         $this->lowQuantity $lowQuantity;
  429.         return $this;
  430.     }
  431.     public function isIsBookable(): ?bool
  432.     {
  433.         return $this->isBookable;
  434.     }
  435.     public function setIsBookable(bool $isBookable): self
  436.     {
  437.         $this->isBookable $isBookable;
  438.         return $this;
  439.     }
  440.     public function getReservationTime(): ?int
  441.     {
  442.         return $this->reservationTime;
  443.     }
  444.     public function setReservationTime(?int $reservationTime): self
  445.     {
  446.         $this->reservationTime $reservationTime;
  447.         return $this;
  448.     }
  449.     public function getReservationMargin(): ?float
  450.     {
  451.         return $this->reservationMargin;
  452.     }
  453.     public function setReservationMargin(?float $reservationMargin): self
  454.     {
  455.         $this->reservationMargin $reservationMargin;
  456.         return $this;
  457.     }
  458.     public function getControls(): ?array
  459.     {
  460.         return $this->controls;
  461.     }
  462.     public function setControls(?array $controls): static
  463.     {
  464.         $this->controls $controls;
  465.         return $this;
  466.     }
  467.     #[Groups(['article:list'])]
  468.     public function getValue(): ?int
  469.     {
  470.         return $this->id;
  471.     }
  472.     #[Groups(['article:list'])]
  473.     public function getLabel(): ?string
  474.     {
  475.         return $this->name;
  476.     }
  477.     public static function getVariables(): array
  478.     {
  479.         return [
  480.             "Prix" => "amount",
  481.             "Temps de reservation" => "reservationTime",
  482.             "Quantité" => "quantity",
  483.         ];
  484.     }
  485.     public function getTax(): ?Tax
  486.     {
  487.         return $this->tax;
  488.     }
  489.     public function setTax(?Tax $tax): static
  490.     {
  491.         $this->tax $tax;
  492.         return $this;
  493.     }
  494.     public function __toString(): string
  495.     {
  496.         return $this->name;
  497.     }
  498.     public function getViews(): ?int
  499.     {
  500.         return $this->views;
  501.     }
  502.     public function setViews($views): void
  503.     {
  504.         $this->views $views;
  505.     }
  506.     /**
  507.      * @return Collection<int, ViewCounter>
  508.      */
  509.     public function getViewCounters(): Collection
  510.     {
  511.         return $this->viewCounters;
  512.     }
  513.     public function addViewCounter(ViewCounter $viewCounter): static
  514.     {
  515.         if (!$this->viewCounters->contains($viewCounter)) {
  516.             $this->viewCounters->add($viewCounter);
  517.             $viewCounter->setArticle($this);
  518.         }
  519.         return $this;
  520.     }
  521.     public function removeViewCounter(ViewCounter $viewCounter): static
  522.     {
  523.         if ($this->viewCounters->removeElement($viewCounter)) {
  524.             // set the owning side to null (unless already changed)
  525.             if ($viewCounter->getArticle() === $this) {
  526.                 $viewCounter->setArticle(null);
  527.             }
  528.         }
  529.         return $this;
  530.     }
  531. }