128 lines
3.1 KiB
PHP
128 lines
3.1 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: 'customer')]
|
|
#[ApiResource(
|
|
operations: [
|
|
new Get(
|
|
requirements: ['id' => '\d+'],
|
|
normalizationContext: ['groups' => ['customer:read']],
|
|
),
|
|
new GetCollection(
|
|
normalizationContext: ['groups' => ['customer:read']],
|
|
),
|
|
new Post(
|
|
normalizationContext: ['groups' => ['customer:read']],
|
|
denormalizationContext: ['groups' => ['customer:write']],
|
|
security: "is_granted('ROLE_ADMIN')",
|
|
),
|
|
new Patch(
|
|
normalizationContext: ['groups' => ['customer:read']],
|
|
denormalizationContext: ['groups' => ['customer:write']],
|
|
security: "is_granted('ROLE_ADMIN')",
|
|
),
|
|
],
|
|
security: "is_granted('ROLE_USER')",
|
|
)]
|
|
class Customer
|
|
{
|
|
#[ORM\Id]
|
|
#[ORM\GeneratedValue]
|
|
#[ORM\Column]
|
|
#[Groups(['shipment:read', 'customer:read'])]
|
|
private ?int $id = null;
|
|
|
|
#[ORM\Column(length: 255)]
|
|
#[Groups(['customer:read', 'customer:write', 'shipment:read'])]
|
|
private ?string $label = null;
|
|
|
|
#[ORM\Column(length: 255)]
|
|
#[Groups(['customer:read', 'customer:write', 'shipment:read'])]
|
|
private ?string $code = null;
|
|
|
|
/**
|
|
* @var Collection<int, Address>
|
|
*/
|
|
#[ORM\ManyToMany(targetEntity: Address::class, inversedBy: 'customers')]
|
|
#[ORM\JoinTable(name: 'customer_address')]
|
|
#[Groups(['customer:read', 'customer:write'])]
|
|
#[ApiProperty(readableLink: true)]
|
|
private Collection $addresses;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->addresses = new ArrayCollection();
|
|
}
|
|
|
|
public function getId(): ?int
|
|
{
|
|
return $this->id;
|
|
}
|
|
|
|
public function getLabel(): ?string
|
|
{
|
|
return $this->label;
|
|
}
|
|
|
|
public function setLabel(?string $label): void
|
|
{
|
|
$this->label = $label;
|
|
}
|
|
|
|
public function getCode(): ?string
|
|
{
|
|
return $this->code;
|
|
}
|
|
|
|
public function setCode(?string $code): void
|
|
{
|
|
$this->code = $code;
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|