<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Annotation\ApiSubresource;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Core\Serializer\Filter\GroupFilter;
use ApiPlatform\Core\Serializer\Filter\PropertyFilter;
use App\Repository\ExpertRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity(repositoryClass=ExpertRepository::class)
* @ApiResource(
* normalizationContext={
* "groups"={"expert:read"}
* },
* denormalizationContext={
* "groups"={"expert:write"}
* },
* collectionOperations={
* "get"={
* "security"="is_granted('ROLE_USER')"
* },
* "get_experts_by_program"={
* "description"="Get experts by Program.",
* "path"="/programs/{id}/experts",
* "method"="GET",
* "controller"="App\Controller\Api\ExpertController::getExpertsByProgram",
* "security"="is_granted('ROLE_USER')"
* },
* "post"={
* "security"="is_granted('ROLE_ADMIN')"
* },
* "post_update"={
* "security"="is_granted('ROLE_ADMIN')",
* "path"="/experts/{id}",
* "description"="Update an Expert with method POST (using content-type: 'multipart')",
* "method"="POST",
* "controller"="App\Controller\Api\ExpertController::update"
* }
* },
* itemOperations={
* "get"={
* "security"="is_granted('ROLE_USER')"
* },
* "patch"={
* "security"="is_granted('ROLE_ADMIN')"
* },
* "delete"={
* "security"="is_granted('ROLE_ADMIN')"
* }
* }
* )
* @ApiFilter(GroupFilter::class, arguments={"parameterName": "groups", "overrideDefaultGroups": true, "whitelist": {
* "expert_list", "experts_form"
* }})
* @ApiFilter(OrderFilter::class)
* @ApiFilter(SearchFilter::class, properties={"lastName":"partial"})
* @ApiFilter(PropertyFilter::class,
* arguments={
* "parameterName"="fields",
* "overrideDefaultProperties"=true
* }
* )
* @ORM\HasLifecycleCallbacks()
*/
class Expert
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
* @Groups({"expert:read:id", "expert:read", "homepage:read", "program:read", "search_engine", "video:read", "expert_list", "experts_form", "teamplay_challenge:read"})
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
* @Groups({"expert:read:firstName", "channel:read", "homepage:read", "expert:read", "expert:write", "program:read", "video:read", "slide:read",
* "search_engine", "day:read", "expert_list", "teamplay_challenge:read"})
* @Assert\NotBlank()
*/
private $firstName;
/**
* @ORM\Column(type="string", length=255)
* @Groups({"expert:read:lastName", "channel:read", "homepage:read", "expert:read", "expert:write", "program:read", "video:read",
* "slide:read", "search_engine", "day:read", "expert_list", "teamplay_challenge:read"})
* @Assert\NotBlank()
*/
private $lastName;
/**
* @ORM\Column(type="string", length=255)
* @Groups({"expert:read:field", "channel:read", "homepage:read", "expert:read", "expert:write", "program:read", "video:read",
* "slide:read", "search_engine", "day:read", "expert_list", "teamplay_challenge:read"})
* @Assert\NotBlank()
*/
private $field;
/**
* @ORM\Column(type="text")
* @Groups({"expert:read:career", "channel:read", "homepage:read", "expert:read", "expert:write", "program:read", "video:read",
* "slide:read", "search_engine", "day:read", "expert_list", "teamplay_challenge:read"})
* @Assert\NotBlank()
*/
private $career;
/**
* @ORM\ManyToOne(targetEntity=MediaObject::class)
* @Groups({"expert:read:avatar", "channel:read", "homepage:read", "expert:read", "expert:write", "program:read", "video:read",
* "slide:read", "search_engine", "day:read", "expert_list", "teamplay_challenge:read"})
*/
private $avatar;
/**
* @ORM\ManyToOne(targetEntity=MediaObject::class)
* @Groups({"expert:read:picture", "channel:read", "homepage:read", "expert:read", "expert:write", "program:read", "video:read",
* "slide:read", "search_engine", "day:read", "expert_list", "teamplay_challenge:read"})
*/
private $picture;
/**
* @ORM\Column(type="datetime")
* @Gedmo\Timestampable(on="create")
*/
private $createdAt;
/**
* @ORM\ManyToMany(targetEntity=Video::class, mappedBy="experts")
* @ApiSubresource(maxDepth=1)
*/
private $videos;
/**
* @var int
* @Groups({"expert:read:videosCount", "expert:read", "search_engine", "expert_list"})
*/
private $videosCount = 0;
/**
* @var ?string
* @Groups({"expert:read:firstName", "channel:read", "homepage:read", "expert:read", "expert:write", "program:read", "video:read", "slide:read",
* "search_engine", "day:read", "expert_list", "teamplay_challenge:read"})
*/
private $name;
public function __construct()
{
$this->createdAt = new \DateTime();
$this->videos = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getFirstName(): ?string
{
return $this->firstName;
}
public function setFirstName(string $firstName): self
{
$this->firstName = $firstName;
return $this;
}
public function getLastName(): ?string
{
return $this->lastName;
}
public function setLastName(string $lastName): self
{
$this->lastName = $lastName;
return $this;
}
public function getField(): ?string
{
return $this->field;
}
public function setField(string $field): self
{
$this->field = $field;
return $this;
}
public function getCareer(): ?string
{
return $this->career;
}
public function setCareer(string $career): self
{
$this->career = $career;
return $this;
}
public function getAvatar(): ?MediaObject
{
return $this->avatar;
}
public function setAvatar(?MediaObject $avatar): self
{
$this->avatar = $avatar;
return $this;
}
/**
* @Groups({"expert:write"})
*/
public function setAvatarFile($file = null): self
{
if($file instanceof UploadedFile) {
$avatar = empty($this->avatar) ? new MediaObject : $this->avatar;
$avatar->setFile($file);
$this->setAvatar($avatar);
}
return $this;
}
public function getPicture(): ?MediaObject
{
return $this->picture;
}
public function setPicture(?MediaObject $picture): self
{
$this->picture = $picture;
return $this;
}
/**
* @Groups({"expert:write"})
*/
public function setPictureFile($file = null): self
{
if($file instanceof UploadedFile) {
$picture = empty($this->picture) ? new MediaObject : $this->picture;
$picture->setFile($file);
$this->setPicture($picture);
}
return $this;
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeInterface $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
/**
* @return Collection|Video[]
*/
public function getVideos(): Collection
{
return $this->videos;
}
public function addVideo(Video $video): self
{
if (!$this->videos->contains($video)) {
$this->videos[] = $video;
$video->addExpert($this);
}
return $this;
}
public function removeVideo(Video $video): self
{
if ($this->videos->removeElement($video)) {
$video->removeExpert($this);
}
return $this;
}
/**
* @return int
*/
public function getVideosCount(): int
{
return $this->videosCount;
}
/**
* @param int $videosCount
* @return Expert
*/
public function setVideosCount(int $videosCount): Expert
{
$this->videosCount = $videosCount;
return $this;
}
/**
* @Groups({"expert:read:name", "experts_form", "video_form_complete"})
* @return string
*/
public function getName(): string
{
$this->name = $this->firstName . ' ' .$this->lastName;
return $this->name;
}
}