@@ -408,6 +408,54 @@ class Person
408
408
409
409
` ` `
410
410
411
+ # ## Plain Identifiers
412
+
413
+ Instead of sending an IRI to set a relation, you may want to send a plain identifier. To do so, you must create your own denormalizer :
414
+
415
+ ` ` ` php
416
+ <?php
417
+ // api/src/Serializer/PlainIdentifierDenormalizer
418
+
419
+ namespace App\S erializer;
420
+
421
+ use ApiPlatform\C ore\A pi\I riConverterInterface;
422
+ use App\E ntity\D ummy;
423
+ use App\E ntity\R elatedDummy;
424
+ use Symfony\C omponent\S erializer\N ormalizer\C ontextAwareDenormalizerInterface;
425
+ use Symfony\C omponent\S erializer\N ormalizer\D enormalizerAwareInterface;
426
+ use Symfony\C omponent\S erializer\N ormalizer\D enormalizerAwareTrait;
427
+
428
+ class PlainIdentifierDenormalizer implements ContextAwareDenormalizerInterface, DenormalizerAwareInterface
429
+ {
430
+ use DenormalizerAwareTrait;
431
+
432
+ private $iriConverter;
433
+
434
+ public function __construct(IriConverterInterface $iriConverter)
435
+ {
436
+ $this->iriConverter = $iriConverter;
437
+ }
438
+
439
+ /**
440
+ * {@inheritdoc}
441
+ */
442
+ public function denormalize($data, $class, $format = null, array $context = [])
443
+ {
444
+ $data['relatedDummy'] = $this->iriConverter->getItemIriFromResourceClass(RelatedDummy::class, ['id' => $data['relatedDummy']]);
445
+
446
+ return $this->denormalizer->denormalize($data, $class, $format, $context + [__CLASS__ => true]);
447
+ }
448
+
449
+ /**
450
+ * {@inheritdoc}
451
+ */
452
+ public function supportsDenormalization($data, $type, $format = null, array $context = []): bool
453
+ {
454
+ return \i n_array($format, ['json', 'jsonld'], true) && is_a($type, Dummy::class, true) && !empty($data['relatedDummy']) && !isset($context[__CLASS__]);
455
+ }
456
+ }
457
+ ` ` `
458
+
411
459
# # Calculated Field
412
460
413
461
Sometimes you need to expose calculated fields. This can be done by leveraging the groups. This time not on a property, but on a method.
0 commit comments