src/Entity/Segmentation/Department.php line 47

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Segmentation;
  3. use ApiPlatform\Core\Annotation\ApiProperty;
  4. use ApiPlatform\Core\Annotation\ApiResource;
  5. use App\Entity\Operation\Coupon;
  6. use App\Repository\DepartmentRepository;
  7. use Doctrine\Common\Collections\ArrayCollection;
  8. use Doctrine\Common\Collections\Collection;
  9. use Doctrine\ORM\Mapping as ORM;
  10. use Gedmo\Timestampable\Traits\TimestampableEntity;
  11. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  12. use Symfony\Component\Serializer\Annotation\Groups;
  13. use Symfony\Component\Serializer\Annotation\SerializedName;
  14. use Symfony\Component\Validator\Constraints as Assert;
  15. use Ramsey\Uuid\Doctrine\UuidGenerator;
  16. /**
  17.  * @ApiResource(
  18.  *     attributes={
  19.  *          "normalization_context"={"groups"={
  20.  *              "Department:output",
  21.  *              "Department:io",
  22.  *          }},
  23.  *          "denormalization_context"={"groups"={
  24.  *              "Department:input",
  25.  *              "Department:io",
  26.  *          }}
  27.  *      },
  28.  *     collectionOperations={
  29.  *          "get"={
  30.  *              "method"="GET",
  31.  *              "access_control"="is_granted('ROLE_USER')",
  32.  *          },
  33.  *     },
  34.  *     itemOperations={
  35.  *          "get"={
  36.  *              "method"="GET",
  37.  *              "access_control"="is_granted('ROLE_USER')",
  38.  *          },
  39.  *      }
  40.  * )
  41.  * @ORM\Entity(repositoryClass=DepartmentRepository::class)
  42.  * @UniqueEntity("code")
  43.  */
  44. class Department
  45. {
  46.     /**
  47.      * Hook timestampable behavior
  48.      * updates createdAt, updatedAt fields
  49.      */
  50.     use TimestampableEntity;
  51.     /**
  52.      * @ORM\Id
  53.      * @ORM\Column(type="uuid", unique=true)
  54.      * @ORM\GeneratedValue(strategy="CUSTOM")
  55.      * @ORM\CustomIdGenerator(class=UuidGenerator::class)
  56.      * @SerializedName("id")
  57.      * @ApiProperty(identifier=true)
  58.      * @Assert\Uuid()
  59.      * @Groups({
  60.      *     "Department:output",
  61.      *     "get_all_public_coupons",
  62.      * })
  63.      *
  64.      */
  65.     private string $id;
  66.     /**
  67.      * @ORM\Column(type="string", length=3)
  68.      * @SerializedName("code")
  69.      * @Groups({
  70.      *     "Department:output",
  71.      *     "get_all_public_coupons",
  72.      * })
  73.      */
  74.     private ?string $code;
  75.     /**
  76.      * @var Collection
  77.      *
  78.      * @ORM\ManyToMany(targetEntity="App\Entity\Operation\Coupon", mappedBy="departments")
  79.      * @SerializedName("coupons")
  80.      * @Groups({
  81.      *     "Department:output",
  82.      * })
  83.      */
  84.     private Collection $coupons;
  85.     /**
  86.      * @var Collection
  87.      *
  88.      * @ORM\OneToMany(targetEntity="App\Entity\User\Address\Home", mappedBy="department", cascade={"persist"})
  89.      */
  90.     private $address;
  91.     /**
  92.      * @var Collection
  93.      *
  94.      * @ORM\OneToMany(targetEntity="App\Entity\User\Address\Delivery", mappedBy="department", cascade={"persist"})
  95.      */
  96.     private $delivery;
  97.     /**
  98.      * @var Collection
  99.      *
  100.      * @ORM\OneToMany(targetEntity="App\Entity\User\Address\Order", mappedBy="department", cascade={"persist"})
  101.      */
  102.     private $order;
  103.     /**
  104.      * @ORM\Column(type="string", length=255, nullable=true)
  105.      */
  106.     private $name;
  107.     /**
  108.      * @ORM\ManyToOne(targetEntity=Region::class, inversedBy="departments", fetch="EAGER")
  109.      * @Groups({
  110.      *     "Department:output",
  111.      * })
  112.      */
  113.     private $region;
  114.     public function __construct()
  115.     {
  116.         $this->coupons = new ArrayCollection();
  117.     }
  118.     public function getId(): ?string
  119.     {
  120.         return $this->id;
  121.     }
  122.     public function getCode(): ?string
  123.     {
  124.         return $this->code;
  125.     }
  126.     public function setCode(string $code): self
  127.     {
  128.         $this->code $code;
  129.         return $this;
  130.     }
  131.     /**
  132.      * @return Collection
  133.      */
  134.     public function getCoupons(): Collection
  135.     {
  136.         return $this->coupons;
  137.     }
  138.     public function addCoupon(Coupon $coupon): Department
  139.     {
  140.         if ($this->coupons->contains($coupon)) {
  141.             return $this;
  142.         }
  143.         $this->coupons[] = $coupon;
  144.         return $this;
  145.     }
  146.     public function getName(): ?string
  147.     {
  148.         return $this->name;
  149.     }
  150.     public function setName(string $name): self
  151.     {
  152.         $this->name $name;
  153.         return $this;
  154.     }
  155.     public function getRegion(): ?Region
  156.     {
  157.         return $this->region;
  158.     }
  159.     public function setRegion(?Region $region): self
  160.     {
  161.         $this->region $region;
  162.         return $this;
  163.     }
  164. }