Skip to content

Commit 3bed037

Browse files
committed
fix
1 parent bc38744 commit 3bed037

3 files changed

Lines changed: 57 additions & 36 deletions

File tree

src/Hydra/Serializer/CollectionNormalizer.php

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -73,24 +73,11 @@ protected function getPaginationData(iterable $object, array $context = []): arr
7373
}
7474

7575
if (null !== $this->resourceMetadataCollectionFactory && ($context['hydra_operations'] ?? $this->defaultContext['hydra_operations'] ?? false)) {
76-
$allHydraOperations = [];
77-
$operationNames = [];
78-
foreach ($this->resourceMetadataCollectionFactory->create($resourceClass) as $resourceMetadata) {
79-
$hydraOperations = $this->getHydraOperations(
80-
true,
81-
$resourceMetadata,
82-
$hydraPrefix
83-
);
84-
if (!empty($hydraOperations)) {
85-
foreach ($hydraOperations as $operation) {
86-
$operationName = $operation['method'];
87-
if (!\in_array($operationName, $operationNames, true)) {
88-
$operationNames[] = $operationName;
89-
$allHydraOperations[] = $operation;
90-
}
91-
}
92-
}
93-
}
76+
$allHydraOperations = $this->getHydraOperationsFromResourceMetadatas(
77+
$resourceClass,
78+
true,
79+
$hydraPrefix
80+
);
9481

9582
if (!empty($allHydraOperations)) {
9683
$data[$hydraPrefix.'operation'] = $allHydraOperations;

src/Hydra/Serializer/HydraOperationsTrait.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,53 @@
2525
*/
2626
trait HydraOperationsTrait
2727
{
28+
/**
29+
* Gets Hydra operations from all resource metadata.
30+
*/
31+
private function getHydraOperationsFromResourceMetadatas(string $resourceClass, bool $collection, string $hydraPrefix): array
32+
{
33+
$allHydraOperations = [];
34+
$operationNames = [];
35+
36+
foreach ($this->resourceMetadataCollectionFactory->create($resourceClass) as $resourceMetadata) {
37+
$hydraOperations = $this->getHydraOperationsFromResourceMetadata(
38+
$collection,
39+
$resourceMetadata,
40+
$hydraPrefix,
41+
$operationNames
42+
);
43+
44+
$allHydraOperations = array_merge($allHydraOperations, $hydraOperations);
45+
}
46+
47+
return $allHydraOperations;
48+
}
49+
50+
/**
51+
* Gets Hydra operations from a single resource metadata.
52+
*/
53+
private function getHydraOperationsFromResourceMetadata(bool $collection, ApiResource $resourceMetadata, string $hydraPrefix, array &$operationNames): array
54+
{
55+
$operations = [];
56+
$hydraOperations = $this->getHydraOperations(
57+
$collection,
58+
$resourceMetadata,
59+
$hydraPrefix
60+
);
61+
62+
if (!empty($hydraOperations)) {
63+
foreach ($hydraOperations as $operation) {
64+
$operationName = $operation['method'];
65+
if (!\in_array($operationName, $operationNames, true)) {
66+
$operationNames[] = $operationName;
67+
$operations[] = $operation;
68+
}
69+
}
70+
}
71+
72+
return $operations;
73+
}
74+
2875
/**
2976
* Gets Hydra operations.
3077
*/

src/JsonLd/Serializer/ItemNormalizer.php

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -191,24 +191,11 @@ public function normalize(mixed $data, ?string $format = null, array $context =
191191
}
192192

193193
if ($isResourceClass && ($context['hydra_operations'] ?? $this->itemNormalizerDefaultContext['hydra_operations'] ?? false)) {
194-
$allHydraOperations = [];
195-
$operationNames = [];
196-
foreach ($this->resourceMetadataCollectionFactory->create($resourceClass) as $resourceMetadata) {
197-
$hydraOperations = $this->getHydraOperations(
198-
false,
199-
$resourceMetadata,
200-
$this->getHydraPrefix($context + $this->itemNormalizerDefaultContext)
201-
);
202-
if (!empty($hydraOperations)) {
203-
foreach ($hydraOperations as $operation) {
204-
$operationName = $operation['method'];
205-
if (!\in_array($operationName, $operationNames, true)) {
206-
$operationNames[] = $operationName;
207-
$allHydraOperations[] = $operation;
208-
}
209-
}
210-
}
211-
}
194+
$allHydraOperations = $this->getHydraOperationsFromResourceMetadatas(
195+
$resourceClass,
196+
false,
197+
$this->getHydraPrefix($context + $this->itemNormalizerDefaultContext)
198+
);
212199

213200
if (!empty($allHydraOperations)) {
214201
$metadata['operation'] = $allHydraOperations;

0 commit comments

Comments
 (0)