src/Entity/Customer.php line 65

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Controller\Action\DeleteCustomerController;
  4. use App\Controller\Action\DeleteSitesController;
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Doctrine\Common\Collections\Collection;
  7. use Doctrine\ORM\Mapping as ORM;
  8. use ApiPlatform\Metadata\ApiResource;
  9. use ApiPlatform\Metadata\Delete;
  10. use ApiPlatform\Metadata\Get;
  11. use ApiPlatform\Metadata\GetCollection;
  12. use ApiPlatform\Metadata\Post;
  13. use ApiPlatform\Metadata\Put;
  14. use App\Controller\Action\SiteTreeAction;
  15. use App\Traits\AutoTimestampTrait;
  16. use Symfony\Component\Validator\Constraints as Assert;
  17. use Symfony\Component\Serializer\Annotation\Groups;
  18. #[ORM\Table(name'customer')]
  19. #[ORM\Entity]
  20. #[ORM\HasLifecycleCallbacks]
  21. #[ApiResource(
  22.     normalizationContext: ['groups' => ['customer.get','user.get''discussion.get']],
  23.     operations: [
  24.         new Post(
  25.             security'user.isMasterAdmin()',
  26.         ),
  27.         new Get(
  28.             name'_api_/customers/tree',
  29.             uriTemplate'/customers/tree',
  30.             controllerSiteTreeAction::class,
  31.             writefalse,
  32.             readfalse,
  33.             cacheHeaders: [
  34.                 'max_age' => 86400,
  35.                 'shared_max_age' => 86400,
  36.                 'vary' => ['Authorization''Accept']
  37.             ]
  38.         ),
  39.         new GetCollection(
  40.             paginationEnabledfalse,
  41.             order: ['name' => 'ASC']
  42.         ),
  43.         new Get(
  44.         ),
  45.         new Put(
  46.             security'user.isMasterAdmin()',
  47.         ),
  48.         new Delete(
  49.             security'user.isMasterAdmin()',
  50.         ),
  51.         new Post(
  52.             name'_api_/delete_customer',
  53.             uriTemplate'/delete_customer',
  54.             controllerDeleteCustomerController::class,
  55.             writefalse,
  56.             readfalse
  57.         ),
  58.     ]
  59. )]
  60. class Customer
  61. {
  62.     use AutoTimestampTrait;
  63.     #[ORM\Column(name'id'type'integer'nullablefalse)]
  64.     #[ORM\Id]
  65.     #[ORM\GeneratedValue(strategy'IDENTITY')]
  66.     #[Groups(['customer.get''user.get'])]
  67.     private int $id;
  68.     #[ORM\Column(name'old_id'type'string'length255nullabletrue)]
  69.     private ?string $oldId null;
  70.     #[ORM\Column(name'code'type'string'length255nullabletrue)]
  71.     #[Assert\NotBlank]
  72.     #[Groups(['customer.get''user.get'])]
  73.     private ?string $code null;
  74.     #[ORM\Column(name'reference'type'string'length255nullabletrue)]
  75.     #[Groups(['customer.get''user.get'])]
  76.     private ?string $reference null;
  77.     #[ORM\Column(name'name'type'string'length255nullabletrue)]
  78.     #[Assert\NotBlank]
  79.     #[Groups(['customer.get''user.get''discussion.get'])]
  80.     private ?string $name null;
  81.     #[ORM\OneToMany(mappedBy'customer'targetEntityUser::class)]
  82.     private Collection $users;
  83. //    #[Groups(['customer.get'])]
  84.     #[ORM\OneToMany(mappedBy'customer'targetEntitySite::class)]
  85.     private Collection $sites;
  86.     #[ORM\Column(nullabletrue)]
  87.     #[Groups(['customer.get''user.get'])]
  88.     private ?int $year null;
  89.     public function __construct()
  90.     {
  91.         $this->users = new ArrayCollection();
  92.         $this->sites = new ArrayCollection();
  93.     }
  94.     /**
  95.      * @return Collection<int, Site>
  96.      */
  97.     public function getSites(): Collection
  98.     {
  99.         return $this->sites;
  100.     }
  101.     public function setSites($sites): self
  102.     {
  103.         $this->sites $sites;
  104.         return $this;
  105.     }
  106.     public function addSite(Site $site): self
  107.     {
  108.         if (!$this->sites->contains($site)) {
  109.             $this->sites->add($site);
  110.             $site->setCustomer($this);
  111.         }
  112.         return $this;
  113.     }
  114.     public function removeSite(Site $site): self
  115.     {
  116.         if ($this->sites->removeElement($site)) {
  117.             // set the owning side to null (unless already changed)
  118.             if ($site->getCustomer() === $this) {
  119.                 $site->setCustomer(null);
  120.             }
  121.         }
  122.         return $this;
  123.     }
  124.     public function getId(): ?int
  125.     {
  126.         return $this->id;
  127.     }
  128.     public function getOldId(): ?string
  129.     {
  130.         return $this->oldId;
  131.     }
  132.     public function setOldId(?string $oldId): self
  133.     {
  134.         $this->oldId $oldId;
  135.         return $this;
  136.     }
  137.     public function getCode(): ?string
  138.     {
  139.         return $this->code;
  140.     }
  141.     public function setCode(?string $code): self
  142.     {
  143.         $this->code $code;
  144.         return $this;
  145.     }
  146.     public function getReference(): ?string
  147.     {
  148.         return $this->reference;
  149.     }
  150.     public function setReference(?string $reference): self
  151.     {
  152.         $this->reference $reference;
  153.         return $this;
  154.     }
  155.     public function getName(): ?string
  156.     {
  157.         return $this->name;
  158.     }
  159.     public function setName(?string $name): self
  160.     {
  161.         $this->name $name;
  162.         return $this;
  163.     }
  164.     /**
  165.      * @return Collection<int, User>
  166.      */
  167.     public function getUsers(): Collection
  168.     {
  169.         return $this->users;
  170.     }
  171.     public function addUser(User $user): self
  172.     {
  173.         if (!$this->users->contains($user)) {
  174.             $this->users->add($user);
  175.             $user->setCustomer($this);
  176.         }
  177.         return $this;
  178.     }
  179.     public function removeUser(User $user): self
  180.     {
  181.         if ($this->users->removeElement($user)) {
  182.             // set the owning side to null (unless already changed)
  183.             if ($user->getCustomer() === $this) {
  184.                 $user->setCustomer(null);
  185.             }
  186.         }
  187.         return $this;
  188.     }
  189.     /*
  190.     public function getSites(): array
  191.     {
  192.         return $this->sites;
  193.     }
  194.     public function setSites($sites): self
  195.     {
  196.         $this->sites = $sites;
  197.         return $this;
  198.     }*/
  199.     public function getYear(): ?int
  200.     {
  201.         return $this->year;
  202.     }
  203.     public function setYear(?int $year): self
  204.     {
  205.         $this->year $year;
  206.         return $this;
  207.     }
  208. }