27
27
use ApiPlatform \Metadata \Util \ClassInfoTrait ;
28
28
use ApiPlatform \Symfony \Security \ResourceAccessCheckerInterface ;
29
29
use ApiPlatform \Util \CloneTrait ;
30
+ use Symfony \Component \EventDispatcher \EventDispatcher ;
30
31
use Symfony \Component \PropertyAccess \Exception \NoSuchPropertyException ;
31
32
use Symfony \Component \PropertyAccess \PropertyAccess ;
32
33
use Symfony \Component \PropertyAccess \PropertyAccessorInterface ;
@@ -61,7 +62,7 @@ abstract class AbstractItemNormalizer extends AbstractObjectNormalizer
61
62
protected array $ localCache = [];
62
63
protected array $ localFactoryOptionsCache = [];
63
64
64
- public function __construct (protected PropertyNameCollectionFactoryInterface $ propertyNameCollectionFactory , protected PropertyMetadataFactoryInterface $ propertyMetadataFactory , protected IriConverterInterface $ iriConverter , protected ResourceClassResolverInterface $ resourceClassResolver , PropertyAccessorInterface $ propertyAccessor = null , NameConverterInterface $ nameConverter = null , ClassMetadataFactoryInterface $ classMetadataFactory = null , array $ defaultContext = [], ResourceMetadataCollectionFactoryInterface $ resourceMetadataCollectionFactory = null , protected ?ResourceAccessCheckerInterface $ resourceAccessChecker = null )
65
+ public function __construct (protected PropertyNameCollectionFactoryInterface $ propertyNameCollectionFactory , protected PropertyMetadataFactoryInterface $ propertyMetadataFactory , protected IriConverterInterface $ iriConverter , protected ResourceClassResolverInterface $ resourceClassResolver , PropertyAccessorInterface $ propertyAccessor = null , NameConverterInterface $ nameConverter = null , ClassMetadataFactoryInterface $ classMetadataFactory = null , array $ defaultContext = [], ResourceMetadataCollectionFactoryInterface $ resourceMetadataCollectionFactory = null , protected ?ResourceAccessCheckerInterface $ resourceAccessChecker = null , protected ? EventDispatcher $ eventDispatcher = null )
65
66
{
66
67
if (!isset ($ defaultContext ['circular_reference_handler ' ])) {
67
68
$ defaultContext ['circular_reference_handler ' ] = fn ($ object ): ?string => $ this ->iriConverter ->getIriFromResource ($ object );
@@ -159,13 +160,19 @@ public function normalize(mixed $object, string $format = null, array $context =
159
160
*/
160
161
$ emptyResourceAsIri = $ context ['api_empty_resource_as_iri ' ] ?? false ;
161
162
unset($ context ['api_empty_resource_as_iri ' ]);
162
-
163
- if (isset ($ context ['resources ' ])) {
164
- $ context ['resources ' ][$ iri ] = $ iri ;
163
+
164
+ if ($ this ->eventDispatcher ) {
165
+ $ event = new NormalizeItemEvent ($ object , $ format , $ context , $ iri , null );
166
+ $ this ->eventDispatcher ->dispatch ($ event , NormalizeItemEvent::NORMALIZE_ITEM_PRE );
165
167
}
166
168
167
169
$ data = parent ::normalize ($ object , $ format , $ context );
168
170
171
+ if ($ this ->eventDispatcher ) {
172
+ $ event = new NormalizeItemEvent ($ object , $ format , $ context , $ iri , $ data );
173
+ $ this ->eventDispatcher ->dispatch ($ event , NormalizeItemEvent::NORMALIZE_ITEM_POST );
174
+ }
175
+
169
176
if ($ emptyResourceAsIri && \is_array ($ data ) && 0 === \count ($ data )) {
170
177
return $ iri ;
171
178
}
@@ -635,7 +642,14 @@ protected function getAttributeValue(object $object, string $attribute, string $
635
642
$ resourceClass = $ this ->resourceClassResolver ->getResourceClass ($ attributeValue , $ className );
636
643
$ childContext = $ this ->createChildContext ($ this ->createOperationContext ($ context , $ resourceClass ), $ attribute , $ format );
637
644
638
- return $ this ->normalizeCollectionOfRelations ($ propertyMetadata , $ attributeValue , $ resourceClass , $ format , $ childContext );
645
+ $ data = $ this ->normalizeCollectionOfRelations ($ propertyMetadata , $ attributeValue , $ resourceClass , $ format , $ childContext );
646
+
647
+ if ($ this ->eventDispatcher ) {
648
+ $ event = new NormalizeAttributeEvent ($ object , $ format , $ context , $ context ['iri ' ], $ data , $ attribute , $ propertyMetadata , $ type , $ childContext );
649
+ $ this ->eventDispatcher ->dispatch ($ event , NormalizeAttributeEvent::NORMALIZE_ATTRIBUTE );
650
+ }
651
+
652
+ return $ data ;
639
653
}
640
654
641
655
if (
@@ -650,7 +664,13 @@ protected function getAttributeValue(object $object, string $attribute, string $
650
664
$ resourceClass = $ this ->resourceClassResolver ->getResourceClass ($ attributeValue , $ className );
651
665
$ childContext = $ this ->createChildContext ($ this ->createOperationContext ($ context , $ resourceClass ), $ attribute , $ format );
652
666
653
- return $ this ->normalizeRelation ($ propertyMetadata , $ attributeValue , $ resourceClass , $ format , $ childContext );
667
+ $ data = $ this ->normalizeRelation ($ propertyMetadata , $ attributeValue , $ resourceClass , $ format , $ childContext );
668
+ if ($ this ->eventDispatcher ) {
669
+ $ event = new NormalizeAttributeEvent ($ object , $ format , $ context , $ context ['iri ' ], $ data , $ attribute , $ propertyMetadata , $ type , $ childContext );
670
+ $ this ->eventDispatcher ->dispatch ($ event , NormalizeAttributeEvent::NORMALIZE_ATTRIBUTE );
671
+ }
672
+
673
+ return $ data ;
654
674
}
655
675
656
676
if (!$ this ->serializer instanceof NormalizerInterface) {
@@ -728,8 +748,9 @@ protected function normalizeRelation(ApiProperty $propertyMetadata, ?object $rel
728
748
729
749
$ iri = $ this ->iriConverter ->getIriFromResource ($ relatedObject );
730
750
731
- if (isset ($ context ['resources ' ])) {
732
- $ context ['resources ' ][$ iri ] = $ iri ;
751
+ if ($ this ->eventDispatcher ) {
752
+ $ event = new NormalizeItemEvent ($ relatedObject , $ format , $ context , $ iri , $ iri );
753
+ $ this ->eventDispatcher ->dispatch ($ event , NormalizeItemEvent::NORMALIZE_RELATION );
733
754
}
734
755
735
756
$ push = $ propertyMetadata ->getPush () ?? false ;
0 commit comments