src/Entity/Cargo.php line 123

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 Ramsey\Uuid\Uuid;
  17. use Ramsey\Uuid\UuidInterface;
  18. #[ORM\Entity]
  19. #[ApiResource(
  20.     iri'Cargo',
  21.     itemOperations: [
  22.         'get' => [
  23.             'security' => "
  24.                 is_granted('ROLE_ADMIN') or 
  25.                 is_granted('ROLE_OPERATOR') or
  26.                 is_granted('ROLE_OPERATIONS_COORDINATOR') or 
  27.                 is_granted('ROLE_RESEARCH_ANALYST') or
  28.                 is_granted('ROLE_BROKER') or 
  29.                 is_granted('ROLE_INTERN')
  30.             ",
  31.             'normalization_context' => [
  32.                 'groups' => 'cargo:item:get'
  33.                 'enable_max_depth' => true
  34.             ]
  35.         ],
  36.         'put' => [
  37.             'security' => "
  38.                 is_granted('ROLE_ADMIN') or 
  39.                 is_granted('ROLE_OPERATOR') or
  40.                 is_granted('ROLE_OPERATIONS_COORDINATOR') or 
  41.                 is_granted('ROLE_RESEARCH_ANALYST') or 
  42.                 is_granted('ROLE_BROKER')
  43.             ",
  44.             'normalization_context' => [
  45.                 'groups' => 'cargo:item:put'
  46.                 'enable_max_depth' => true
  47.             ],
  48.             'denormalization_context' => [
  49.                 'groups' => 'cargo:item:put'
  50.                 'enable_max_depth' => true
  51.             ],
  52.         ],
  53.         'delete' => [
  54.             'security' => "
  55.                 is_granted('ROLE_ADMIN') or 
  56.                 is_granted('ROLE_OPERATOR') or
  57.                 is_granted('ROLE_OPERATIONS_COORDINATOR') or 
  58.                 is_granted('ROLE_RESEARCH_ANALYST') or 
  59.                 is_granted('ROLE_BROKER')
  60.             ",
  61.         ],
  62.     ],
  63.     collectionOperations: [
  64.         'get' => [
  65.             'security' => "
  66.                 is_granted('ROLE_ADMIN') or 
  67.                 is_granted('ROLE_OPERATOR') or
  68.                 is_granted('ROLE_OPERATIONS_COORDINATOR') or 
  69.                 is_granted('ROLE_RESEARCH_ANALYST') or
  70.                 is_granted('ROLE_BROKER') or 
  71.                 is_granted('ROLE_INTERN')
  72.             ",
  73.             'normalization_context' => [
  74.                 'groups' => [
  75.                     'cargo:collection:get'
  76.                     'createdAt'
  77.                 ], 
  78.                 'enable_max_depth' => true
  79.             ],
  80.         ],
  81.         'post' => [
  82.             'security' => "
  83.                 is_granted('ROLE_ADMIN') or 
  84.                 is_granted('ROLE_OPERATOR') or
  85.                 is_granted('ROLE_OPERATIONS_COORDINATOR') or 
  86.                 is_granted('ROLE_RESEARCH_ANALYST') or 
  87.                 is_granted('ROLE_BROKER')
  88.             ",
  89.             'normalization_context' => [
  90.                 'groups' => 'cargo:collection:post'
  91.                 'enable_max_depth' => true
  92.             ],
  93.             'denormalization_context' => [
  94.                 'groups' => 'cargo:collection:post'
  95.                 'enable_max_depth' => true
  96.             ],
  97.         ],
  98.     ],
  99. )]
  100. #[ApiFilter(
  101.     SearchFilter::class,
  102.     properties: [
  103.         'name' => 'partial'
  104.         'cargoType' => 'exact'
  105.         'subtype' => 'exact'
  106.         'createdAt' => 'start'
  107.     ],
  108. )]
  109. #[ApiFilter(
  110.     OrderFilter::class,
  111.     properties: [
  112.         'name'
  113.         'cargoType'
  114.         'subtype'
  115.         'createdAt'
  116.     ],
  117. )]
  118. #[ApiFilter(PropertyFilter::class)]
  119. class Cargo
  120. {
  121.     use TimestampableEntity;
  122.     public const CARGO_TYPE_LIQUID 'Liquid';
  123.     public const CARGO_TYPE_DRY 'Dry';
  124.     
  125.     public const CARGO_TYPES = [
  126.         self::CARGO_TYPE_LIQUID,
  127.         self::CARGO_TYPE_DRY,
  128.     ];
  129.     public const CARGO_SUBTYPE_LIQUID 'Bulk Cargo';
  130.     public const CARGO_SUBTYPE_DRY 'Bagged Cargo';
  131.     public const CARGO_SUBTYPE_GENERAL 'General Cargo';
  132.     public const CARGO_SUBTYPE_LIQUID_CPP 'Liquid Cargo / CPP - Clean Petroleum Products';
  133.     public const CARGO_SUBTYPE_LIQUID_CHEMICAL 'Liquid Cargo / Chemical';
  134.     public const CARGO_SUBTYPE_LIQUID_DPP 'Liquid Cargo / DPP - Dirty Petroleum Products';
  135.     public const CARGO_SUBTYPES = [
  136.         self::CARGO_SUBTYPE_LIQUID,
  137.         self::CARGO_SUBTYPE_DRY,
  138.         self::CARGO_SUBTYPE_GENERAL,
  139.         self::CARGO_SUBTYPE_LIQUID_CPP,
  140.         self::CARGO_SUBTYPE_LIQUID_CHEMICAL,
  141.         self::CARGO_SUBTYPE_LIQUID_DPP,
  142.     ];
  143.     
  144.     #[ORM\Id]
  145.     #[ORM\GeneratedValue(strategy'NONE')]
  146.     #[ORM\Column(type'uuid'uniquetrue)]
  147.     private ?UuidInterface $id null;
  148.     #[ORM\Column(type'string'nullablefalse)]
  149.     #[ApiProperty()]
  150.     #[Assert\NotBlank]
  151.     #[Assert\Type('string')]
  152.     #[Groups([
  153.         'cargo:collection:get'
  154.         'cargo:collection:post'
  155.         'cargo:item:get'
  156.         'cargo:item:put'
  157.         'contract:item:get'
  158.         'contract:item:put'
  159.         'contract:collection:get'
  160.         'contract:collection:post',
  161.         'market_rumor:item:get'
  162.         'market_rumor:item:put'
  163.         'market_rumor:collection:get'
  164.         'market_rumor:collection:post'
  165.     ])]
  166.     private ?string $name null;
  167.     #[ORM\Column(type'string'nullablefalse)]
  168.     #[Assert\NotNull]
  169.     #[Assert\Type('string')]
  170.     #[Assert\Choice(
  171.         choicesself::CARGO_TYPES
  172.         message'The type is not valid.'
  173.     )]
  174.     #[ApiProperty()]
  175.     #[Groups([
  176.         'cargo:collection:get'
  177.         'cargo:collection:post'
  178.         'cargo:item:get'
  179.         'cargo:item:put'
  180.     ])]
  181.     private ?string $cargoType null;
  182.     #[ORM\Column(type'string'nullabletrue)]
  183.     ##[Assert\NotNull]
  184.     ##[Assert\Type('string')]
  185.     #[Assert\Choice(
  186.         choicesself::CARGO_SUBTYPES
  187.         message'The subtype is not valid.'
  188.     )]
  189.     #[ApiProperty()]
  190.     #[Groups([
  191.         'cargo:collection:get'
  192.         'cargo:collection:post'
  193.         'cargo:item:get'
  194.         'cargo:item:put'
  195.     ])]
  196.     private ?string $subtype null;
  197.     #[ORM\Column(type'text'nullabletrue)]
  198.     #[ApiProperty()]
  199.     #[Groups([
  200.         // 'cargo:collection:get', 
  201.         'cargo:collection:post'
  202.         'cargo:item:get'
  203.         'cargo:item:put'
  204.     ])]
  205.     private ?string $observations null;
  206.     #[ORM\OneToMany(
  207.         targetEntityContractCargo::class, 
  208.         mappedBy'cargo'
  209.     )]
  210.     // #[ApiProperty()]
  211.     // #[Groups([
  212.     //     'cargo:collection:get', 
  213.     //     'cargo:collection:post', 
  214.     //     'cargo:item:get', 
  215.     //     'cargo:item:put'
  216.     // ])]
  217.     // #[MaxDepth(1)]
  218.     private ?Collection $contractCargos null;
  219.     #[ORM\OneToMany(
  220.         targetEntityMarketRumorCargo::class, 
  221.         mappedBy'cargo'
  222.     )]
  223.     // #[ApiProperty()]
  224.     // #[Groups([
  225.     //     'cargo:collection:get', 
  226.     //     'cargo:collection:post', 
  227.     //     'cargo:item:get', 
  228.     //     'cargo:item:put'
  229.     // ])]
  230.     // #[MaxDepth(1)]
  231.     private ?Collection $marketRumorCargos null;
  232.     public function __construct()
  233.     {
  234.         $this->id Uuid::uuid4();
  235.         $this->contractCargos = new ArrayCollection();
  236.         $this->marketRumorCargos = new ArrayCollection();
  237.     }
  238.     public function getId(): ?UuidInterface
  239.     {
  240.         return $this->id;
  241.     }
  242.     public function setName(?string $name): void
  243.     {
  244.         $this->name $name;
  245.     }
  246.     public function getName(): ?string
  247.     {
  248.         return $this->name;
  249.     }
  250.     public function setSubtype(?string $subtype): void
  251.     {
  252.         $this->subtype $subtype;
  253.     }
  254.     public function getSubtype(): ?string
  255.     {
  256.         return $this->subtype;
  257.     }
  258.     public function setCargoType(?string $cargoType): void
  259.     {
  260.         $this->cargoType $cargoType;
  261.     }
  262.     public function getCargoType(): ?string
  263.     {
  264.         return $this->cargoType;
  265.     }
  266.     public function setObservations(?string $observations): void
  267.     {
  268.         $this->observations $observations;
  269.     }
  270.     public function getObservations(): ?string
  271.     {
  272.         return $this->observations;
  273.     }
  274.     
  275.     public function addContractCargo(ContractCargo $contractCargo): void
  276.     {
  277.         $this->contractCargos[] = $contractCargo;
  278.     }
  279.     public function removeContractCargo(ContractCargo $contractCargo): void
  280.     {
  281.         $this->contractCargos->removeElement($contractCargo);
  282.     }
  283.     public function getContractCargos(): Collection
  284.     {
  285.         return $this->contractCargos;
  286.     }
  287.     public function addMarketRumorCargo(MarketRumorCargo $marketRumorCargo): void
  288.     {
  289.         $this->marketRumorCargos[] = $marketRumorCargo;
  290.     }
  291.     public function removeMarketRumorCargo(MarketRumorCargo $marketRumorCargo): void
  292.     {
  293.         $this->marketRumorCargos->removeElement($marketRumorCargo);
  294.     }
  295.     public function getMarketRumorCargos(): Collection
  296.     {
  297.         return $this->marketRumorCargos;
  298.     }
  299. }