19
19
use ApiPlatform \Core \Api \OperationType ;
20
20
use ApiPlatform \Core \Api \UrlGeneratorInterface ;
21
21
use ApiPlatform \Core \DataProvider \ItemDataProviderInterface ;
22
+ use ApiPlatform \Core \DataProvider \OperationDataProvider ;
22
23
use ApiPlatform \Core \Exception \InvalidArgumentException ;
23
24
use ApiPlatform \Core \Exception \ItemNotFoundException ;
24
25
use ApiPlatform \Core \Exception \RuntimeException ;
25
- use ApiPlatform \Core \Identifier \Normalizer \ChainIdentifierDenormalizer ;
26
26
use ApiPlatform \Core \Metadata \Property \Factory \PropertyMetadataFactoryInterface ;
27
27
use ApiPlatform \Core \Metadata \Property \Factory \PropertyNameCollectionFactoryInterface ;
28
28
use ApiPlatform \Core \Util \ClassInfoTrait ;
@@ -44,24 +44,23 @@ final class IriConverter implements IriConverterInterface
44
44
private $ routeNameResolver ;
45
45
private $ router ;
46
46
private $ identifiersExtractor ;
47
- private $ identifierDenormalizer ;
47
+ private $ operationDataProvider ;
48
48
49
- public function __construct (PropertyNameCollectionFactoryInterface $ propertyNameCollectionFactory , PropertyMetadataFactoryInterface $ propertyMetadataFactory , ItemDataProviderInterface $ itemDataProvider , RouteNameResolverInterface $ routeNameResolver , RouterInterface $ router , PropertyAccessorInterface $ propertyAccessor = null , IdentifiersExtractorInterface $ identifiersExtractor = null , ChainIdentifierDenormalizer $ identifierDenormalizer = null )
49
+ public function __construct (PropertyNameCollectionFactoryInterface $ propertyNameCollectionFactory , PropertyMetadataFactoryInterface $ propertyMetadataFactory , ItemDataProviderInterface $ itemDataProvider , RouteNameResolverInterface $ routeNameResolver , RouterInterface $ router , PropertyAccessorInterface $ propertyAccessor = null , IdentifiersExtractorInterface $ identifiersExtractor = null , OperationDataProvider $ operationDataProvider = null )
50
50
{
51
51
$ this ->itemDataProvider = $ itemDataProvider ;
52
52
$ this ->routeNameResolver = $ routeNameResolver ;
53
53
$ this ->router = $ router ;
54
- $ this ->identifierDenormalizer = $ identifierDenormalizer ;
54
+ $ this ->identifiersExtractor = $ identifiersExtractor ;
55
+ $ this ->operationDataProvider = $ operationDataProvider ;
55
56
56
57
if (null === $ identifiersExtractor ) {
57
58
@trigger_error ('Not injecting ItemIdentifiersExtractor is deprecated since API Platform 2.1 and will not be possible anymore in API Platform 3 ' , E_USER_DEPRECATED );
58
59
$ this ->identifiersExtractor = new IdentifiersExtractor ($ propertyNameCollectionFactory , $ propertyMetadataFactory , $ propertyAccessor ?? PropertyAccess::createPropertyAccessor ());
59
- } else {
60
- $ this ->identifiersExtractor = $ identifiersExtractor ;
61
60
}
62
61
63
- if (null === $ identifierDenormalizer ) {
64
- @trigger_error (sprintf ('Not injecting "%s" is deprecated since API Platform 2.2 and will not be possible anymore in API Platform 3. ' , ChainIdentifierDenormalizer ::class), E_USER_DEPRECATED );
62
+ if (null === $ operationDataProvider ) {
63
+ @trigger_error (sprintf ('Not injecting "%s" is deprecated since API Platform 2.2 and will not be possible anymore in API Platform 3. ' , OperationDataProvider ::class), E_USER_DEPRECATED );
65
64
}
66
65
}
67
66
@@ -76,18 +75,20 @@ public function getItemFromIri(string $iri, array $context = [])
76
75
throw new InvalidArgumentException (sprintf ('No route matches "%s". ' , $ iri ), $ e ->getCode (), $ e );
77
76
}
78
77
79
- if (!isset ($ parameters ['_api_resource_class ' ], $ parameters ['id ' ])) {
80
- throw new InvalidArgumentException (sprintf ('No resource associated to "%s". ' , $ iri ));
81
- }
78
+ if ($ this ->operationDataProvider ) {
79
+ if ($ item = $ this ->operationDataProvider ->getDataFromRouteParameters ($ iri , $ parameters , $ context )) {
80
+ return $ item ;
81
+ }
82
82
83
- $ identifiers = $ parameters ['id ' ];
83
+ throw new ItemNotFoundException (sprintf ('Item not found for "%s". ' , $ iri ));
84
+ }
84
85
85
- if ( $ this -> identifierDenormalizer ) {
86
- $ identifiers = $ this -> identifierDenormalizer -> denormalize (( string ) $ parameters ['id ' ], $ parameters ['_api_resource_class ' ]);
87
- $ context [ChainIdentifierDenormalizer:: HAS_IDENTIFIER_DENORMALIZER ] = true ;
86
+ // Legacy code below
87
+ if (! isset ( $ parameters ['_api_resource_class ' ]) || ! isset ( $ parameters ['id ' ])) {
88
+ throw new InvalidArgumentException ( sprintf ( ' No resource associated to "%s". ' , $ iri )) ;
88
89
}
89
90
90
- if ($ item = $ this ->itemDataProvider ->getItem ($ parameters ['_api_resource_class ' ], $ identifiers , $ parameters ['_api_item_operation_name ' ] ?? null , $ context )) {
91
+ if ($ item = $ this ->itemDataProvider ->getItem ($ parameters ['_api_resource_class ' ], $ parameters [ ' id ' ] , $ parameters ['_api_item_operation_name ' ] ?? null , $ context )) {
91
92
return $ item ;
92
93
}
93
94
0 commit comments