<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiFilter;
use App\Entity\Reference\ReferencePrescriberRelation;
use App\Repository\PrescriberRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Serializer\Annotation\Groups;
/**
* @ApiResource(
*
* normalizationContext={"groups"={"prescriber:read"}, "swagger_definition_name"="Read"},
* denormalizationContext={"groups"={"prescriber:write"}, "swagger_definition_name"="Write"},
* itemOperations={
* "get"={
* "security"="(is_granted('ROLE_AGENT') and object.getCollaborator()==user) or is_granted('ROLE_MANAGER')",
* "security_message"="Vous n'avez pas le droit de visualier ce prescripteur"
* },
* "put" = {
* "denormalization_context"={"groups"={"prescriber:put"}},
* "security"="(is_granted('ROLE_AGENT') and object.getCollaborator()==user) or is_granted('ROLE_MANAGER')",
* },
* },
* collectionOperations={
* "get"={"security"="is_granted('ROLE_AGENT') or is_granted('ROLE_MANAGER')"},
* "post"={"security"="is_granted('ROLE_AGENT') or is_granted('ROLE_MANAGER')"}
* },
* attributes={
* "pagination_items_per_page"=10,
* "formats"={"jsonld", "json", "html", "jsonhal", "csv"={"text/csv"} },
* "order"={"id":"DESC"}
* }
* )
* @ApiFilter(OrderFilter::class,
* properties={"id","firstName","lastName"},
* arguments= {"orderParameterName" : "order"})
* @ORM\Entity(repositoryClass=PrescriberRepository::class)
*
* @UniqueEntity(
* fields={"email"},
* entityClass=Prescriber::class,
* message="AbstractPeople with email {{ value }} is already used."
* )
* @UniqueEntity(
* fields={"mainPhone"},
* entityClass=Prescriber::class,
* message="AbstractPeople with main phone {{ value }} is already used."
* )
*
* @ORM\HasLifecycleCallbacks()
*/
class Prescriber extends AbstractPeople implements Loggable
{
/**
* @ORM\ManyToOne(targetEntity=Agent::class, inversedBy="prescribers")
* @ORM\JoinColumn(nullable=false)
* @Groups({
* "prescriber:read",
* "siege:write","siege:put",
* "transaction-item:read"
* })
*/
private ?Agent $collaborator;
/**
* @ORM\OneToMany(targetEntity=Property::class, mappedBy="prescriber")
*/
private $properties;
/**
* @ORM\ManyToOne(targetEntity=ReferencePrescriberRelation::class)
*
* @Groups({
* "prescriber:read","prescriber:put","prescriber:write"
* })
*/
private ?ReferencePrescriberRelation $referencePrescriberRelation;
public function __construct()
{
$this->properties = new ArrayCollection();
}
public function getCollaborator(): ?Agent
{
return $this->collaborator;
}
public function setCollaborator(?Agent $collaborator): self
{
$this->collaborator = $collaborator;
return $this;
}
/**
* @return Collection|Property[]
*/
public function getProperties(): Collection
{
return $this->properties;
}
public function addProperty(Property $property): self
{
if (!$this->properties->contains($property)) {
$this->properties[] = $property;
$property->setPrescriber($this);
}
return $this;
}
public function removeProperty(Property $property): self
{
// set the owning side to null (unless already changed)
if ($this->properties->removeElement($property) && $property->getPrescriber() === $this) {
$property->setPrescriber(null);
}
return $this;
}
/**
* @return array
*/
public function getFields(): array
{
return [
'gender' => 'gender',
'address' => 'address',
'email' => 'email',
'mainPhone' => 'mainPhone',
'birthDay' => 'birthDay',
'cin' => 'cin',
'rc' => 'rc',
'commonCompanyIdentifier' => 'commonCompanyIdentifier',
'companyName' => 'companyName'
];
}
public function getReferencePrescriberRelation(): ?ReferencePrescriberRelation
{
return $this->referencePrescriberRelation;
}
public function setReferencePrescriberRelation(?ReferencePrescriberRelation $referencePrescriberRelation): self
{
$this->referencePrescriberRelation = $referencePrescriberRelation;
return $this;
}
}