156 lines
3.7 KiB
PHP
156 lines
3.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Entity;
|
|
|
|
use ApiPlatform\Metadata\ApiProperty;
|
|
use ApiPlatform\Metadata\ApiResource;
|
|
use ApiPlatform\Metadata\Get;
|
|
use ApiPlatform\Metadata\GetCollection;
|
|
use ApiPlatform\Metadata\Patch;
|
|
use ApiPlatform\Metadata\Post;
|
|
use Doctrine\Common\Collections\ArrayCollection;
|
|
use Doctrine\Common\Collections\Collection;
|
|
use Doctrine\ORM\Mapping as ORM;
|
|
use Symfony\Component\Serializer\Attribute\Groups;
|
|
|
|
#[ORM\Entity]
|
|
#[ORM\Table(name: 'supplier')]
|
|
#[ApiResource(
|
|
operations: [
|
|
new Get(
|
|
requirements: ['id' => '\d+'],
|
|
normalizationContext: ['groups' => ['supplier:read']],
|
|
),
|
|
new GetCollection(
|
|
normalizationContext: ['groups' => ['supplier:read']],
|
|
),
|
|
new GetCollection(
|
|
uriTemplate: '/admin/suppliers',
|
|
normalizationContext: ['groups' => ['supplier:read']],
|
|
security: "is_granted('ROLE_ADMIN')"
|
|
),
|
|
new Post(
|
|
normalizationContext: ['groups' => ['supplier:read']],
|
|
denormalizationContext: ['groups' => ['supplier:write']],
|
|
security: "is_granted('ROLE_ADMIN')",
|
|
),
|
|
new Patch(
|
|
normalizationContext: ['groups' => ['supplier:read']],
|
|
denormalizationContext: ['groups' => ['supplier:write']],
|
|
security: "is_granted('ROLE_ADMIN')",
|
|
),
|
|
],
|
|
security: "is_granted('ROLE_USER')",
|
|
)]
|
|
class Supplier
|
|
{
|
|
#[ORM\Id]
|
|
#[ORM\GeneratedValue]
|
|
#[ORM\Column]
|
|
#[Groups(['supplier:read', 'reception:read'])]
|
|
private ?int $id = null;
|
|
|
|
#[ORM\Column(length: 180)]
|
|
#[Groups(['supplier:read', 'reception:read', 'supplier:write'])]
|
|
private string $name = '';
|
|
|
|
#[ORM\Column(length: 180, nullable: true)]
|
|
#[Groups(['supplier:read', 'reception:read', 'supplier:write'])]
|
|
private ?string $email = null;
|
|
|
|
#[ORM\Column(length: 40, nullable: true)]
|
|
#[Groups(['supplier:read', 'reception:read', 'supplier:write'])]
|
|
private ?string $phone = null;
|
|
|
|
/**
|
|
* @var Collection<int, Address>
|
|
*/
|
|
#[ORM\ManyToMany(targetEntity: Address::class, inversedBy: 'suppliers')]
|
|
#[ORM\JoinTable(name: 'supplier_address')]
|
|
#[Groups(['supplier:read', 'supplier:write'])]
|
|
#[ApiProperty(readableLink: true)]
|
|
private Collection $addresses;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->addresses = new ArrayCollection();
|
|
}
|
|
|
|
public function getId(): ?int
|
|
{
|
|
return $this->id;
|
|
}
|
|
|
|
public function getName(): string
|
|
{
|
|
return $this->name;
|
|
}
|
|
|
|
public function setName(string $name): self
|
|
{
|
|
$this->name = $name;
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function getEmail(): ?string
|
|
{
|
|
return $this->email;
|
|
}
|
|
|
|
public function setEmail(?string $email): self
|
|
{
|
|
$this->email = $email;
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function getPhone(): ?string
|
|
{
|
|
return $this->phone;
|
|
}
|
|
|
|
public function setPhone(?string $phone): self
|
|
{
|
|
$this->phone = $phone;
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* @return Collection<int, Address>
|
|
*/
|
|
public function getAddresses(): Collection
|
|
{
|
|
return $this->addresses;
|
|
}
|
|
|
|
public function setAddresses(iterable $addresses): self
|
|
{
|
|
$this->addresses->clear();
|
|
foreach ($addresses as $address) {
|
|
$this->addAddress($address);
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function addAddress(Address $address): self
|
|
{
|
|
if (!$this->addresses->contains($address)) {
|
|
$this->addresses->add($address);
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function removeAddress(Address $address): self
|
|
{
|
|
$this->addresses->removeElement($address);
|
|
|
|
return $this;
|
|
}
|
|
}
|