@@ -62,6 +62,9 @@ public function getIdentifiersFromItem(object $item, ?Operation $operation = nul
6262 return $ this ->getIdentifiersFromOperation ($ item , $ operation , $ context );
6363 }
6464
65+ /**
66+ * @param array<string, mixed> $context
67+ */
6568 private function getIdentifiersFromOperation (object $ item , Operation $ operation , array $ context = []): array
6669 {
6770 if ($ operation instanceof HttpOperation) {
@@ -75,24 +78,26 @@ private function getIdentifiersFromOperation(object $item, Operation $operation,
7578 if (1 < (is_countable ($ link ->getIdentifiers ()) ? \count ($ link ->getIdentifiers ()) : 0 )) {
7679 $ compositeIdentifiers = [];
7780 foreach ($ link ->getIdentifiers () as $ identifier ) {
78- $ compositeIdentifiers [$ identifier ] = $ this ->getIdentifierValue ($ item , $ link ->getFromClass () ?? $ operation ->getClass (), $ identifier , $ link ->getParameterName ());
81+ $ compositeIdentifiers [$ identifier ] = $ this ->getIdentifierValue ($ item , $ link ->getFromClass () ?? $ operation ->getClass (), $ identifier , $ link ->getParameterName (), null , $ context , $ operation );
7982 }
8083
8184 $ identifiers [$ link ->getParameterName ()] = CompositeIdentifierParser::stringify ($ compositeIdentifiers );
8285 continue ;
8386 }
8487
8588 $ parameterName = $ link ->getParameterName ();
86- $ identifiers [$ parameterName ] = $ this ->getIdentifierValue ($ item , $ link ->getFromClass () ?? $ operation ->getClass (), $ link ->getIdentifiers ()[0 ] ?? $ k , $ parameterName , $ link ->getToProperty ());
89+ $ identifiers [$ parameterName ] = $ this ->getIdentifierValue ($ item , $ link ->getFromClass () ?? $ operation ->getClass (), $ link ->getIdentifiers ()[0 ] ?? $ k , $ parameterName , $ link ->getToProperty (), $ context , $ operation );
8790 }
8891
8992 return $ identifiers ;
9093 }
9194
9295 /**
9396 * Gets the value of the given class property.
97+ *
98+ * @param array<string, mixed> $context
9499 */
95- private function getIdentifierValue (object $ item , string $ class , string $ property , string $ parameterName , ?string $ toProperty = null ): float |bool |int |string
100+ private function getIdentifierValue (object $ item , string $ class , string $ property , string $ parameterName , ?string $ toProperty, array $ context , Operation $ operation ): float |bool |int |string
96101 {
97102 if ($ item instanceof $ class ) {
98103 try {
@@ -102,6 +107,15 @@ private function getIdentifierValue(object $item, string $class, string $propert
102107 }
103108 }
104109
110+ // ItemUriTemplate is defined on a collection and we read the identifier alghough the PHP class may be different
111+ if (isset ($ context ['item_uri_template ' ]) && $ operation ->getClass () === $ class ) {
112+ try {
113+ return $ this ->resolveIdentifierValue ($ this ->propertyAccessor ->getValue ($ item , $ property ), $ parameterName );
114+ } catch (NoSuchPropertyException $ e ) {
115+ throw new RuntimeException (\sprintf ('Could not retrieve identifier "%s" for class "%s" using itemUriTemplate "%s". Check that the property exists and is accessible. ' , $ property , $ class , $ context ['item_uri_template ' ]), $ e ->getCode (), $ e );
116+ }
117+ }
118+
105119 if ($ toProperty ) {
106120 return $ this ->resolveIdentifierValue ($ this ->propertyAccessor ->getValue ($ item , "$ toProperty. $ property " ), $ parameterName );
107121 }
0 commit comments