src/Entity/Cargo.php line 58

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Entity;
  4. use ApiPlatform\Core\Annotation\ApiFilter;
  5. use ApiPlatform\Core\Annotation\ApiProperty;
  6. use ApiPlatform\Core\Annotation\ApiResource;
  7. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
  8. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
  9. use ApiPlatform\Core\Serializer\Filter\PropertyFilter;
  10. use App\Trait\TimestampableEntity;
  11. use Doctrine\Common\Collections\ArrayCollection;
  12. use Doctrine\Common\Collections\Collection;
  13. use Doctrine\ORM\Mapping as ORM;
  14. use Symfony\Component\Serializer\Annotation\Groups;
  15. use Symfony\Component\Validator\Constraints as Assert;
  16. use Symfony\Component\Serializer\Annotation\MaxDepth;
  17. use Ramsey\Uuid\Uuid;
  18. use Ramsey\Uuid\UuidInterface;
  19. /**
  20.  * @see https://schema.org/Thing
  21.  *
  22.  * @author Jordi Fernandes Alves <jfadev@gmail.com>
  23.  */
  24. #[ORM\Entity]
  25. #[ApiResource(
  26.     iri'Cargo',
  27.     itemOperations: [
  28.         'get' => ['normalization_context' => ['groups' => 'cargo:item:get''enable_max_depth' => true]],
  29.         'put' => [
  30.             'normalization_context' => ['groups' => 'cargo:item:put''enable_max_depth' => true],
  31.             'denormalization_context' => ['groups' => 'cargo:item:put''enable_max_depth' => true],
  32.         ],
  33.         'delete' => [],
  34.     ],
  35.     collectionOperations: [
  36.         'get' => [
  37.             'normalization_context' => ['groups' => ['cargo:collection:get''createdAt'], 'enable_max_depth' => true],
  38.         ],
  39.         'post' => [
  40.             'normalization_context' => ['groups' => 'cargo:collection:post''enable_max_depth' => true],
  41.             'denormalization_context' => ['groups' => 'cargo:collection:post''enable_max_depth' => true],
  42.         ],
  43.     ],
  44. )]
  45. #[ApiFilter(
  46.     SearchFilter::class,
  47.     properties: ['name' => 'partial''cargoType' => 'exact''subtype' => 'exact''createdAt' => 'start'],
  48. )]
  49. #[ApiFilter(
  50.     OrderFilter::class,
  51.     properties: ['name''cargoType''subtype''createdAt'],
  52. )]
  53. #[ApiFilter(PropertyFilter::class)]
  54. class Cargo
  55. {
  56.     use TimestampableEntity;
  57.     public const CARGO_TYPES = [
  58.         'Liquid',
  59.         'Dry'
  60.     ];
  61.     public const SUBTYPES = [
  62.         'Bulk Cargo',
  63.         'Bagged Cargo',
  64.         'General Cargo',
  65.         'Liquid Cargo / CPP - Clean Petroleum Products',
  66.         'Liquid Cargo / Chemical',
  67.         'Liquid Cargo / DPP - Dirty Petroleum Products'
  68.     ];
  69.     
  70.     #[ORM\Id]
  71.     #[ORM\GeneratedValue(strategy'NONE')]
  72.     #[ORM\Column(type'uuid'uniquetrue)]
  73.     private ?UuidInterface $id null;
  74.     #[ORM\Column(type'string'nullablefalse)]
  75.     #[ApiProperty(iri'https://schema.org/name')]
  76.     #[Assert\NotBlank]
  77.     #[Assert\Type('string')]
  78.     #[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'])]
  79.     private ?string $name null;
  80.     #[ORM\Column(type'string'nullablefalse)]
  81.     #[Assert\NotNull]
  82.     #[Assert\Type('string')]
  83.     #[Assert\Choice(choicesself::CARGO_TYPESmessage'The type is not valid.')]
  84.     #[ApiProperty(iri'https://schema.org/cargoType')]
  85.     #[Groups(['cargo:collection:get''cargo:collection:post''cargo:item:get''cargo:item:put'])]
  86.     private ?string $cargoType null;
  87.     #[ORM\Column(type'string'nullabletrue)]
  88.     #[Assert\NotNull]
  89.     #[Assert\Type('string')]
  90.     #[Assert\Choice(choicesself::SUBTYPESmessage'The type is not valid.')]
  91.     #[ApiProperty(iri'https://schema.org/subtype')]
  92.     #[Groups(['cargo:collection:get''cargo:collection:post''cargo:item:get''cargo:item:put'])]
  93.     private ?string $subtype null;
  94.     #[ORM\Column(type'text'nullabletrue)]
  95.     #[ApiProperty(iri'https://schema.org/observations')]
  96.     #[Groups(['cargo:collection:get''cargo:collection:post''cargo:item:get''cargo:item:put'])]
  97.     private ?string $observations null;
  98.     #[ORM\OneToMany(targetEntity'App\Entity\ContractCargo'mappedBy'cargo')]
  99.     #[ApiProperty(iri'https://schema.org/ContractCargo')]
  100.     #[Groups(['cargo:collection:get''cargo:collection:post''cargo:item:get''cargo:item:put'])]
  101.     #[MaxDepth(1)]
  102.     private ?Collection $contractCargos null;
  103.     public function __construct()
  104.     {
  105.         $this->id Uuid::uuid4();
  106.         $this->contractCargos = new ArrayCollection();
  107.     }
  108.     public function getId(): ?UuidInterface
  109.     {
  110.         return $this->id;
  111.     }
  112.     public function setName(?string $name): void
  113.     {
  114.         $this->name $name;
  115.     }
  116.     public function getName(): ?string
  117.     {
  118.         return $this->name;
  119.     }
  120.     public function setSubtype(?string $subtype): void
  121.     {
  122.         $this->subtype $subtype;
  123.     }
  124.     public function getSubtype(): ?string
  125.     {
  126.         return $this->subtype;
  127.     }
  128.     public function setCargoType(?string $cargoType): void
  129.     {
  130.         $this->cargoType $cargoType;
  131.     }
  132.     public function getCargoType(): ?string
  133.     {
  134.         return $this->cargoType;
  135.     }
  136.     public function setObservations(?string $observations): void
  137.     {
  138.         $this->observations $observations;
  139.     }
  140.     public function getObservations(): ?string
  141.     {
  142.         return $this->observations;
  143.     }
  144.     
  145.     public function addContractCargo(ContractCargo $contractCargo): void
  146.     {
  147.         $this->contractCargos[] = $contractCargo;
  148.     }
  149.     public function removeContractCargo(ContractCargo $contractCargo): void
  150.     {
  151.         $this->contractCargos->removeElement($contractCargo);
  152.     }
  153.     public function getContractCargos(): Collection
  154.     {
  155.         return $this->contractCargos;
  156.     }
  157. }