Advertisement
Guest User

UUID Failing Denormalization

a guest
Nov 8th, 2022
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.12 KB | None | 0 0
  1. <?php
  2.  
  3. declare(strict_types=1);
  4.  
  5. namespace App\Entity;
  6.  
  7. use ApiPlatform\Metadata\Post;
  8. use ApiPlatform\Metadata\GetCollection;
  9. use ApiPlatform\Metadata\Get;
  10. use ApiPlatform\Metadata\ApiResource;
  11. use ApiPlatform\Metadata\ApiProperty;
  12. use Doctrine\ORM\Mapping as ORM;
  13. use Ramsey\Uuid\Uuid;
  14. use Ramsey\Uuid\UuidInterface;
  15. use Symfony\Component\Serializer\Annotation\Groups;
  16.  
  17. #[ApiResource(
  18.     operations: [
  19.         new Get(),
  20.         new GetCollection(),
  21.         new Post(),
  22.     ],
  23.     normalizationContext: ['groups' => ['message:read']],
  24.     denormalizationContext: ['groups' => ['message:write']]
  25. )]
  26. #[ORM\Entity]
  27. class Test
  28. {
  29.     #[ApiProperty(identifier: false)]
  30.     #[ORM\Id]
  31.     #[ORM\GeneratedValue]
  32.     #[ORM\Column(type: 'integer')]
  33.     private int $id;
  34.  
  35.     #[ApiProperty(identifier: true)]
  36.     #[Groups(['message:write'])]
  37.     #[ORM\Column(type: 'uuid', unique: true)]
  38.     private UuidInterface $uuid;
  39.  
  40.     public function __construct(?UuidInterface $uuid = null)
  41.     {
  42.         $this->uuid = $uuid ?? Uuid::uuid4();
  43.     }
  44.  
  45.     public function getId(): int
  46.     {
  47.         return $this->id;
  48.     }
  49.  
  50.     public function getUuid(): string
  51.     {
  52.         return $this->uuid->toString();
  53.     }
  54. }
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement