<?php
declare(strict_types=1);
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
use App\Trait\TimestampableEntity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Serializer\Annotation\MaxDepth;
/**
* An organization such as a school, NGO, corporation, club, etc.
*
* @see https://schema.org/Organization
*
* @author Jordi Fernandes Alves <jfadev@gmail.com>
*/
#[ORM\Entity]
#[ApiResource(
iri: 'Broker',
itemOperations: [
'get' => [
// 'security' => "is_granted('ROLE_USER')",
'normalization_context' => [
'groups' => 'broker:item:get',
'enable_max_depth' => true
]
],
'put' => [
// 'security' => "is_granted('ROLE_USER')",
'normalization_context' => [
'groups' => 'broker:item:put',
'enable_max_depth' => true
],
'denormalization_context' => [
'groups' => 'broker:item:put',
'enable_max_depth' => true
]
],
'delete' => [
// 'security' => "is_granted('ROLE_USER')",
]
],
collectionOperations: [
'get' => [
// 'security' => "is_granted('ROLE_USER')",
'normalization_context' => [
'groups' => ['broker:collection:get', 'createdAt'],
'enable_max_depth' => true
]
],
'post' => [
// 'security' => "is_granted('ROLE_USER')",
'normalization_context' => [
'groups' => 'broker:collection:post',
'enable_max_depth' => true
],
'denormalization_context' => [
'groups' => 'broker:collection:post',
'enable_max_depth' => true
]
],
]
)]
#[ApiFilter(SearchFilter::class, properties: [
'taxID' => 'start',
'name' => 'partial',
'legalName' => 'partial',
'createdAt' => 'start',
])]
#[ApiFilter(OrderFilter::class, properties: [
'name',
'taxID',
'legalName',
'createdAt'
])]
class Broker
{
use TimestampableEntity;
#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'AUTO')]
#[ORM\Column(type: 'integer')]
#[Groups([
'broker:collection:get',
'broker:item:get',
'user:collection:post',
'user:item:put',
'channel:item:get',
'channel:item:put',
'insured:item:put',
])]
private ?int $id = null;
/**
* The name of the item.
*
* @see https://schema.org/name
*/
#[ORM\Column(type: 'string')]
#[ApiProperty(iri: 'https://schema.org/name')]
#[Assert\Type('string')]
#[Groups([
'broker:collection:get',
'broker:collection:post',
'broker:item:get',
'broker:item:put',
'user:item:get',
'channel:item:get',
'channel:collection:get',
'insured:item:get',
])]
private ?string $name = null;
/**
* Email address.
*
* @see https://schema.org/email
*/
#[ORM\Column(type: 'string')]
#[ApiProperty(iri: 'https://schema.org/email')]
#[Assert\Email]
#[Groups([
'broker:collection:post',
'broker:item:get',
'broker:item:put',
])]
private ?string $email = null;
/**
* The official name of the organization, e.g. the registered company name.
*
* @see https://schema.org/legalName
*/
#[ORM\Column(type: 'string')]
#[ApiProperty(iri: 'https://schema.org/legalName')]
#[Assert\Type('string')]
#[Groups([
'broker:collection:get',
'broker:collection:post',
'broker:item:get',
'broker:item:put',
])]
private ?string $legalName = null;
/**
* The Tax / Fiscal ID of the organization or person, e.g. the TIN in the US or the CIF/NIF in Spain.
*
* @see https://schema.org/taxID
*/
#[ORM\Column(type: 'string')]
#[ApiProperty(iri: 'https://schema.org/taxID')]
#[Assert\Type('string')]
#[Groups([
'broker:collection:get',
'broker:collection:post',
'broker:item:get',
'broker:item:put',
'user:item:get',
'channel:item:get',
'channel:collection:get',
'insured:item:get',
])]
private ?string $taxID = null;
/**
* The telephone number.
*
* @see https://schema.org/telephone
*/
#[ORM\Column(type: 'string', nullable: true)]
#[ApiProperty(iri: 'https://schema.org/telephone')]
#[Assert\Type('string')]
#[Groups([
'broker:collection:post',
'broker:item:get',
'broker:item:put',
])]
private ?string $telephone = null;
#[ORM\OneToMany(mappedBy: 'broker', targetEntity: BrokerUser::class)]
#[Groups([
'broker:item:get',
])]
#[MaxDepth(1)]
private $users;
#[ORM\OneToMany(mappedBy: 'broker', targetEntity: Channel::class)]
#[Groups([
'broker:item:get',
])]
#[MaxDepth(1)]
private $channels;
#[ORM\OneToMany(mappedBy: 'broker', targetEntity: Insured::class)]
#[Groups([
'broker:item:get',
])]
#[MaxDepth(1)]
private $insureds;
public function __construct()
{
$this->users = new ArrayCollection();
$this->channels = new ArrayCollection();
$this->insureds = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function setName(?string $name): void
{
$this->name = $name;
}
public function getName(): ?string
{
return $this->name;
}
public function setEmail(?string $email): void
{
$this->email = $email;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setLegalName(?string $legalName): void
{
$this->legalName = $legalName;
}
public function getLegalName(): ?string
{
return $this->legalName;
}
public function setTaxID(?string $taxID): void
{
$this->taxID = $taxID;
}
public function getTaxID(): ?string
{
return $this->taxID;
}
public function setTelephone(?string $telephone): void
{
$this->telephone = $telephone;
}
public function getTelephone(): ?string
{
return $this->telephone;
}
/**
* @return Collection|BrokerUser[]
*/
public function getUsers(): Collection
{
return $this->users;
}
public function addUser(BrokerUser $user): self
{
if (!$this->users->contains($user)) {
$this->users[] = $user;
$user->setBroker($this);
}
return $this;
}
public function removeUser(BrokerUser $user): self
{
if ($this->users->removeElement($user)) {
// set the owning side to null (unless already changed)
if ($user->getBroker() === $this) {
$user->setBroker(null);
}
}
return $this;
}
/**
* @return Collection|Channel[]
*/
public function getChannels(): Collection
{
return $this->channels;
}
public function addChannel(Channel $channel): self
{
if (!$this->channels->contains($channel)) {
$this->channels[] = $channel;
$channel->setBroker($this);
}
return $this;
}
public function removeChannel(Channel $channel): self
{
if ($this->channels->removeElement($channel)) {
// set the owning side to null (unless already changed)
if ($channel->getBroker() === $this) {
$channel->setBroker(null);
}
}
return $this;
}
/**
* @return Collection|Insured[]
*/
public function getInsureds(): Collection
{
return $this->insureds;
}
public function addInsured(Insured $insured): self
{
if (!$this->insureds->contains($insured)) {
$this->insureds[] = $insured;
$insured->setBroker($this);
}
return $this;
}
public function removeInsured(Insured $insured): self
{
if ($this->insureds->removeElement($insured)) {
// set the owning side to null (unless already changed)
if ($insured->getBroker() === $this) {
$insured->setBroker(null);
}
}
return $this;
}
}