<?php
declare(strict_types=1);
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Core\Serializer\Filter\PropertyFilter;
use App\Trait\TimestampableEntity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Serializer\Annotation\MaxDepth;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;
/**
* @see https://schema.org/Thing
*
* @author Jordi Fernandes Alves <jfadev@gmail.com>
*/
#[ORM\Entity]
#[ApiResource(
iri: 'Cargo',
itemOperations: [
'get' => ['normalization_context' => ['groups' => 'cargo:item:get', 'enable_max_depth' => true]],
'put' => [
'normalization_context' => ['groups' => 'cargo:item:put', 'enable_max_depth' => true],
'denormalization_context' => ['groups' => 'cargo:item:put', 'enable_max_depth' => true],
],
'delete' => [],
],
collectionOperations: [
'get' => [
'normalization_context' => ['groups' => ['cargo:collection:get', 'createdAt'], 'enable_max_depth' => true],
],
'post' => [
'normalization_context' => ['groups' => 'cargo:collection:post', 'enable_max_depth' => true],
'denormalization_context' => ['groups' => 'cargo:collection:post', 'enable_max_depth' => true],
],
],
)]
#[ApiFilter(
SearchFilter::class,
properties: ['name' => 'partial', 'cargoType' => 'exact', 'subtype' => 'exact', 'createdAt' => 'start'],
)]
#[ApiFilter(
OrderFilter::class,
properties: ['name', 'cargoType', 'subtype', 'createdAt'],
)]
#[ApiFilter(PropertyFilter::class)]
class Cargo
{
use TimestampableEntity;
public const CARGO_TYPES = [
'Liquid',
'Dry'
];
public const SUBTYPES = [
'Bulk Cargo',
'Bagged Cargo',
'General Cargo',
'Liquid Cargo / CPP - Clean Petroleum Products',
'Liquid Cargo / Chemical',
'Liquid Cargo / DPP - Dirty Petroleum Products'
];
#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'NONE')]
#[ORM\Column(type: 'uuid', unique: true)]
private ?UuidInterface $id = null;
#[ORM\Column(type: 'string', nullable: false)]
#[ApiProperty(iri: 'https://schema.org/name')]
#[Assert\NotBlank]
#[Assert\Type('string')]
#[Groups(['cargo:collection:get', 'cargo:collection:post', 'cargo:item:get', 'cargo:item:put', 'contract:item:get', 'contract:item:put', 'contract:collection:get', 'contract:collection:post'])]
private ?string $name = null;
#[ORM\Column(type: 'string', nullable: false)]
#[Assert\NotNull]
#[Assert\Type('string')]
#[Assert\Choice(choices: self::CARGO_TYPES, message: 'The type is not valid.')]
#[ApiProperty(iri: 'https://schema.org/cargoType')]
#[Groups(['cargo:collection:get', 'cargo:collection:post', 'cargo:item:get', 'cargo:item:put'])]
private ?string $cargoType = null;
#[ORM\Column(type: 'string', nullable: true)]
#[Assert\NotNull]
#[Assert\Type('string')]
#[Assert\Choice(choices: self::SUBTYPES, message: 'The type is not valid.')]
#[ApiProperty(iri: 'https://schema.org/subtype')]
#[Groups(['cargo:collection:get', 'cargo:collection:post', 'cargo:item:get', 'cargo:item:put'])]
private ?string $subtype = null;
#[ORM\Column(type: 'text', nullable: true)]
#[ApiProperty(iri: 'https://schema.org/observations')]
#[Groups(['cargo:collection:get', 'cargo:collection:post', 'cargo:item:get', 'cargo:item:put'])]
private ?string $observations = null;
#[ORM\OneToMany(targetEntity: 'App\Entity\ContractCargo', mappedBy: 'cargo')]
#[ApiProperty(iri: 'https://schema.org/ContractCargo')]
#[Groups(['cargo:collection:get', 'cargo:collection:post', 'cargo:item:get', 'cargo:item:put'])]
#[MaxDepth(1)]
private ?Collection $contractCargos = null;
public function __construct()
{
$this->id = Uuid::uuid4();
$this->contractCargos = new ArrayCollection();
}
public function getId(): ?UuidInterface
{
return $this->id;
}
public function setName(?string $name): void
{
$this->name = $name;
}
public function getName(): ?string
{
return $this->name;
}
public function setSubtype(?string $subtype): void
{
$this->subtype = $subtype;
}
public function getSubtype(): ?string
{
return $this->subtype;
}
public function setCargoType(?string $cargoType): void
{
$this->cargoType = $cargoType;
}
public function getCargoType(): ?string
{
return $this->cargoType;
}
public function setObservations(?string $observations): void
{
$this->observations = $observations;
}
public function getObservations(): ?string
{
return $this->observations;
}
public function addContractCargo(ContractCargo $contractCargo): void
{
$this->contractCargos[] = $contractCargo;
}
public function removeContractCargo(ContractCargo $contractCargo): void
{
$this->contractCargos->removeElement($contractCargo);
}
public function getContractCargos(): Collection
{
return $this->contractCargos;
}
}