src/Entity/Expert.php line 77

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use ApiPlatform\Core\Annotation\ApiFilter;
  4. use ApiPlatform\Core\Annotation\ApiResource;
  5. use ApiPlatform\Core\Annotation\ApiSubresource;
  6. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
  7. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
  8. use ApiPlatform\Core\Serializer\Filter\GroupFilter;
  9. use ApiPlatform\Core\Serializer\Filter\PropertyFilter;
  10. use App\Repository\ExpertRepository;
  11. use Doctrine\Common\Collections\ArrayCollection;
  12. use Doctrine\Common\Collections\Collection;
  13. use Doctrine\ORM\Mapping as ORM;
  14. use Gedmo\Mapping\Annotation as Gedmo;
  15. use Symfony\Component\HttpFoundation\File\UploadedFile;
  16. use Symfony\Component\Serializer\Annotation\Groups;
  17. use Symfony\Component\Validator\Constraints as Assert;
  18. /**
  19.  * @ORM\Entity(repositoryClass=ExpertRepository::class)
  20.  * @ApiResource(
  21.  *      normalizationContext={
  22.  *          "groups"={"expert:read"}
  23.  *      },
  24.  *      denormalizationContext={
  25.  *          "groups"={"expert:write"}
  26.  *      },
  27.  *      collectionOperations={
  28.  *          "get"={
  29.  *              "security"="is_granted('ROLE_USER')"
  30.  *          },
  31.  *          "get_experts_by_program"={
  32.  *             "description"="Get experts by Program.",
  33.  *             "path"="/programs/{id}/experts",
  34.  *             "method"="GET",
  35.  *             "controller"="App\Controller\Api\ExpertController::getExpertsByProgram",
  36.  *             "security"="is_granted('ROLE_USER')"
  37.  *          },
  38.  *          "post"={
  39.  *              "security"="is_granted('ROLE_ADMIN')"
  40.  *          },
  41.  *          "post_update"={
  42.  *              "security"="is_granted('ROLE_ADMIN')",
  43.  *              "path"="/experts/{id}",
  44.  *              "description"="Update an Expert with method POST (using content-type: 'multipart')",
  45.  *              "method"="POST",
  46.  *              "controller"="App\Controller\Api\ExpertController::update"
  47.  *          }
  48.  *     },
  49.  *     itemOperations={
  50.  *          "get"={
  51.  *              "security"="is_granted('ROLE_USER')"
  52.  *          },
  53.  *          "patch"={
  54.  *              "security"="is_granted('ROLE_ADMIN')"
  55.  *          },
  56.  *          "delete"={
  57.  *              "security"="is_granted('ROLE_ADMIN')"
  58.  *          }
  59.  *     }
  60.  * )
  61.  * @ApiFilter(GroupFilter::class, arguments={"parameterName": "groups", "overrideDefaultGroups": true, "whitelist": {
  62.  *      "expert_list", "experts_form"
  63.  * }})
  64.  * @ApiFilter(OrderFilter::class)
  65.  * @ApiFilter(SearchFilter::class, properties={"lastName":"partial"})
  66.  * @ApiFilter(PropertyFilter::class, 
  67.  *      arguments={
  68.  *          "parameterName"="fields", 
  69.  *          "overrideDefaultProperties"=true
  70.  *     }
  71.  * )
  72.  * @ORM\HasLifecycleCallbacks()
  73.  */
  74. class Expert
  75. {
  76.     /**
  77.      * @ORM\Id
  78.      * @ORM\GeneratedValue
  79.      * @ORM\Column(type="integer")
  80.      * @Groups({"expert:read:id", "expert:read", "homepage:read", "program:read", "search_engine", "video:read", "expert_list", "experts_form", "teamplay_challenge:read"})
  81.      */
  82.     private $id;
  83.     /**
  84.      * @ORM\Column(type="string", length=255)
  85.      * @Groups({"expert:read:firstName", "channel:read", "homepage:read", "expert:read", "expert:write", "program:read", "video:read", "slide:read",
  86.      *     "search_engine", "day:read", "expert_list", "teamplay_challenge:read"})
  87.      * @Assert\NotBlank()
  88.      */
  89.     private $firstName;
  90.     /**
  91.      * @ORM\Column(type="string", length=255)
  92.      * @Groups({"expert:read:lastName", "channel:read", "homepage:read", "expert:read", "expert:write", "program:read", "video:read",
  93.      *     "slide:read", "search_engine", "day:read", "expert_list", "teamplay_challenge:read"})
  94.      * @Assert\NotBlank()
  95.      */
  96.     private $lastName;
  97.     /**
  98.      * @ORM\Column(type="string", length=255)
  99.      * @Groups({"expert:read:field", "channel:read", "homepage:read", "expert:read", "expert:write", "program:read", "video:read",
  100.      *     "slide:read", "search_engine", "day:read", "expert_list", "teamplay_challenge:read"})
  101.      * @Assert\NotBlank()
  102.      */
  103.     private $field;
  104.     /**
  105.      * @ORM\Column(type="text")
  106.      * @Groups({"expert:read:career", "channel:read", "homepage:read", "expert:read", "expert:write", "program:read", "video:read",
  107.      *     "slide:read", "search_engine", "day:read", "expert_list", "teamplay_challenge:read"})
  108.      * @Assert\NotBlank()
  109.      */
  110.     private $career;
  111.     /**
  112.      * @ORM\ManyToOne(targetEntity=MediaObject::class)
  113.      * @Groups({"expert:read:avatar", "channel:read", "homepage:read", "expert:read", "expert:write", "program:read", "video:read",
  114.      *     "slide:read", "search_engine", "day:read", "expert_list", "teamplay_challenge:read"})
  115.      */
  116.     private $avatar;
  117.     /**
  118.      * @ORM\ManyToOne(targetEntity=MediaObject::class)
  119.      * @Groups({"expert:read:picture", "channel:read", "homepage:read", "expert:read", "expert:write", "program:read", "video:read",
  120.      *     "slide:read", "search_engine", "day:read", "expert_list", "teamplay_challenge:read"})
  121.      */
  122.     private $picture;
  123.     /**
  124.      * @ORM\Column(type="datetime")
  125.      * @Gedmo\Timestampable(on="create")
  126.      */
  127.     private $createdAt;
  128.     /**
  129.      * @ORM\ManyToMany(targetEntity=Video::class, mappedBy="experts")
  130.      * @ApiSubresource(maxDepth=1)
  131.      */
  132.     private $videos;
  133.     /**
  134.      * @var int
  135.      * @Groups({"expert:read:videosCount", "expert:read", "search_engine", "expert_list"})
  136.      */
  137.     private $videosCount 0;
  138.     /**
  139.      * @var ?string
  140.      * @Groups({"expert:read:firstName", "channel:read", "homepage:read", "expert:read", "expert:write", "program:read", "video:read", "slide:read",
  141.      *     "search_engine", "day:read", "expert_list", "teamplay_challenge:read"})
  142.      */
  143.     private $name;
  144.     public function __construct()
  145.     {
  146.         $this->createdAt = new \DateTime();
  147.         $this->videos = new ArrayCollection();
  148.     }
  149.     public function getId(): ?int
  150.     {
  151.         return $this->id;
  152.     }
  153.     public function getFirstName(): ?string
  154.     {
  155.         return $this->firstName;
  156.     }
  157.     public function setFirstName(string $firstName): self
  158.     {
  159.         $this->firstName $firstName;
  160.         return $this;
  161.     }
  162.     public function getLastName(): ?string
  163.     {
  164.         return $this->lastName;
  165.     }
  166.     public function setLastName(string $lastName): self
  167.     {
  168.         $this->lastName $lastName;
  169.         return $this;
  170.     }
  171.     public function getField(): ?string
  172.     {
  173.         return $this->field;
  174.     }
  175.     public function setField(string $field): self
  176.     {
  177.         $this->field $field;
  178.         return $this;
  179.     }
  180.     public function getCareer(): ?string
  181.     {
  182.         return $this->career;
  183.     }
  184.     public function setCareer(string $career): self
  185.     {
  186.         $this->career $career;
  187.         return $this;
  188.     }
  189.     public function getAvatar(): ?MediaObject
  190.     {
  191.         return $this->avatar;
  192.     }
  193.     public function setAvatar(?MediaObject $avatar): self
  194.     {
  195.         $this->avatar $avatar;
  196.         return $this;
  197.     }
  198.     /**
  199.      * @Groups({"expert:write"})
  200.      */
  201.     public function setAvatarFile($file null): self
  202.     {
  203.         if($file instanceof UploadedFile) {
  204.             $avatar = empty($this->avatar) ? new MediaObject $this->avatar;
  205.             $avatar->setFile($file);
  206.             $this->setAvatar($avatar);
  207.         }
  208.         return $this;
  209.     }
  210.     public function getPicture(): ?MediaObject
  211.     {
  212.         return $this->picture;
  213.     }
  214.     public function setPicture(?MediaObject $picture): self
  215.     {
  216.         $this->picture $picture;
  217.         return $this;
  218.     }
  219.     /**
  220.      * @Groups({"expert:write"})
  221.      */
  222.     public function setPictureFile($file null): self
  223.     {
  224.         if($file instanceof UploadedFile) {
  225.             $picture = empty($this->picture) ? new MediaObject $this->picture;
  226.             $picture->setFile($file);
  227.             $this->setPicture($picture);
  228.         }
  229.         return $this;
  230.     }
  231.     public function getCreatedAt(): ?\DateTimeInterface
  232.     {
  233.         return $this->createdAt;
  234.     }
  235.     public function setCreatedAt(\DateTimeInterface $createdAt): self
  236.     {
  237.         $this->createdAt $createdAt;
  238.         return $this;
  239.     }
  240.     /**
  241.      * @return Collection|Video[]
  242.      */
  243.     public function getVideos(): Collection
  244.     {
  245.         return $this->videos;
  246.     }
  247.     public function addVideo(Video $video): self
  248.     {
  249.         if (!$this->videos->contains($video)) {
  250.             $this->videos[] = $video;
  251.             $video->addExpert($this);
  252.         }
  253.         return $this;
  254.     }
  255.     public function removeVideo(Video $video): self
  256.     {
  257.         if ($this->videos->removeElement($video)) {
  258.             $video->removeExpert($this);
  259.         }
  260.         return $this;
  261.     }
  262.     /**
  263.      * @return int
  264.      */
  265.     public function getVideosCount(): int
  266.     {
  267.         return $this->videosCount;
  268.     }
  269.     /**
  270.      * @param int $videosCount
  271.      * @return Expert
  272.      */
  273.     public function setVideosCount(int $videosCount): Expert
  274.     {
  275.         $this->videosCount $videosCount;
  276.         return $this;
  277.     }
  278.     /**
  279.      * @Groups({"expert:read:name", "experts_form", "video_form_complete"})
  280.      * @return string
  281.      */
  282.     public function getName(): string
  283.     {
  284.         $this->name $this->firstName ' ' .$this->lastName;
  285.         return $this->name;
  286.     }
  287. }