Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
11.11% |
1 / 9 |
|
33.33% |
1 / 3 |
CRAP | |
0.00% |
0 / 1 |
| DoctrineDenormalizer | |
11.11% |
1 / 9 |
|
33.33% |
1 / 3 |
31.28 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| supportsDenormalization | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
| denormalize | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Serializer; |
| 4 | |
| 5 | use Doctrine\ORM\EntityManagerInterface; |
| 6 | use Doctrine\ORM\EntityNotFoundException; |
| 7 | use Exception; |
| 8 | use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; |
| 9 | |
| 10 | class DoctrineDenormalizer implements DenormalizerInterface |
| 11 | { |
| 12 | |
| 13 | /** |
| 14 | * Instance de EntityManagerInterface |
| 15 | * |
| 16 | * @var EntityManagerInterface |
| 17 | */ |
| 18 | private $entityManagerInterface; |
| 19 | |
| 20 | /** |
| 21 | * Constructor |
| 22 | */ |
| 23 | public function __construct(EntityManagerInterface $entityManagerInterface) |
| 24 | { |
| 25 | $this->entityManagerInterface = $entityManagerInterface; |
| 26 | } |
| 27 | /** |
| 28 | * Appel quand on a besoin de denormaliser |
| 29 | * |
| 30 | * @param mixed $data : la valeur que l'on tente de denormaliser (dans notre cas un ID) |
| 31 | * @param string $type : le type que l'on veut obtenir (dans notre cas un entity) |
| 32 | * @param string|null $format |
| 33 | */ |
| 34 | public function supportsDenormalization($data, string $type, ?string $format = null): bool |
| 35 | { |
| 36 | //? je sais traiter le cas où $data est un ID |
| 37 | //? je sais traiter le cas où $type est un entity |
| 38 | $dataIsID = is_numeric($data); |
| 39 | // $type : App\Entity\Type, App\Entity\Person, etc ... |
| 40 | // @link https://www.php.net/manual/fr/function.strpos.php |
| 41 | // je cherche dans $type (App\Entity\Type) si on trouve 'App\Entity' au début (=== 0) |
| 42 | $typeIsEntity = (strpos($type, 'App\Entity') === 0); |
| 43 | |
| 44 | // on exclue les arrayCollection qui sont des App\Entity\xxx[] |
| 45 | $typeIsNotArray = !(strpos($type, ']') === (strlen($type) -1)); |
| 46 | |
| 47 | // je réponds Oui si les TROIS sont vrai |
| 48 | return $typeIsEntity && $dataIsID && $typeIsNotArray; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Si je suis dans le cas où $data est un ID ET $type est un Entity |
| 53 | * |
| 54 | * @param mixed $data : la valeur que l'on tente de denormaliser (dans notre cas un ID) |
| 55 | * @param string $type : le type que l'on veut obtenir (dans notre cas un entity) |
| 56 | * @param string|null $format |
| 57 | * @param array $context |
| 58 | * |
| 59 | * @return mixed |
| 60 | */ |
| 61 | public function denormalize($data, string $type, ?string $format = null, array $context = []) |
| 62 | { |
| 63 | //? ici on veut faire appel à Doctrine |
| 64 | // pour faire un find() avec l'ID fournit |
| 65 | // $type = App\Entity\Type |
| 66 | // $data = 13 |
| 67 | $denormalizedEntity = $this->entityManagerInterface->find($type, $data); |
| 68 | if ($denormalizedEntity === null) |
| 69 | { |
| 70 | // on a pas trouvé d'entité avec cet ID |
| 71 | throw new EntityNotFoundException($type . "#". $data ." not found"); |
| 72 | } |
| 73 | return $denormalizedEntity; |
| 74 | } |
| 75 | } |