Skip to content

Commit 2430ab5

Browse files
authored
Improve Vulcain support (#3275)
* Improve Vulcain support * Add missing @id property
1 parent bf8afd3 commit 2430ab5

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

src/Hydra/Serializer/CollectionNormalizer.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,21 @@ final class CollectionNormalizer implements NormalizerInterface, NormalizerAware
3939
use NormalizerAwareTrait;
4040

4141
public const FORMAT = 'jsonld';
42+
public const IRI_ONLY = 'iri_only';
4243

4344
private $contextBuilder;
4445
private $resourceClassResolver;
4546
private $iriConverter;
47+
private $defaultContext = [
48+
self::IRI_ONLY => false,
49+
];
4650

47-
public function __construct(ContextBuilderInterface $contextBuilder, ResourceClassResolverInterface $resourceClassResolver, IriConverterInterface $iriConverter)
51+
public function __construct(ContextBuilderInterface $contextBuilder, ResourceClassResolverInterface $resourceClassResolver, IriConverterInterface $iriConverter, array $defaultContext = [])
4852
{
4953
$this->contextBuilder = $contextBuilder;
5054
$this->resourceClassResolver = $resourceClassResolver;
5155
$this->iriConverter = $iriConverter;
56+
$this->defaultContext = array_merge($this->defaultContext, $defaultContext);
5257
}
5358

5459
/**
@@ -83,8 +88,9 @@ public function normalize($object, $format = null, array $context = [])
8388
$data['@type'] = 'hydra:Collection';
8489

8590
$data['hydra:member'] = [];
91+
$iriOnly = $context[self::IRI_ONLY] ?? $this->defaultContext[self::IRI_ONLY];
8692
foreach ($object as $obj) {
87-
$data['hydra:member'][] = $this->normalizer->normalize($obj, $format, $context);
93+
$data['hydra:member'][] = $iriOnly ? ['@id' => $this->iriConverter->getIriFromItem($obj)] : $this->normalizer->normalize($obj, $format, $context);
8894
}
8995

9096
$paginated = null;

tests/Hydra/Serializer/CollectionNormalizerTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,4 +328,50 @@ private function normalizePaginator($partial = false)
328328
'resource_class' => 'Foo',
329329
]);
330330
}
331+
332+
public function testNormalizeIriOnlyResourceCollection(): void
333+
{
334+
$fooOne = new Foo();
335+
$fooOne->id = 1;
336+
$fooOne->bar = 'baz';
337+
338+
$fooThree = new Foo();
339+
$fooThree->id = 3;
340+
$fooThree->bar = 'bzz';
341+
342+
$data = [$fooOne, $fooThree];
343+
344+
$contextBuilderProphecy = $this->prophesize(ContextBuilderInterface::class);
345+
$contextBuilderProphecy->getResourceContextUri(Foo::class)->willReturn('/contexts/Foo');
346+
347+
$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
348+
$resourceClassResolverProphecy->getResourceClass($data, Foo::class)->willReturn(Foo::class);
349+
350+
$iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
351+
$iriConverterProphecy->getIriFromResourceClass(Foo::class)->willReturn('/foos');
352+
$iriConverterProphecy->getIriFromItem($fooOne)->willReturn('/foos/1');
353+
$iriConverterProphecy->getIriFromItem($fooThree)->willReturn('/foos/3');
354+
355+
$delegateNormalizerProphecy = $this->prophesize(NormalizerInterface::class);
356+
357+
$normalizer = new CollectionNormalizer($contextBuilderProphecy->reveal(), $resourceClassResolverProphecy->reveal(), $iriConverterProphecy->reveal(), [CollectionNormalizer::IRI_ONLY => true]);
358+
$normalizer->setNormalizer($delegateNormalizerProphecy->reveal());
359+
360+
$actual = $normalizer->normalize($data, CollectionNormalizer::FORMAT, [
361+
'collection_operation_name' => 'get',
362+
'operation_type' => OperationType::COLLECTION,
363+
'resource_class' => Foo::class,
364+
]);
365+
366+
$this->assertSame([
367+
'@context' => '/contexts/Foo',
368+
'@id' => '/foos',
369+
'@type' => 'hydra:Collection',
370+
'hydra:member' => [
371+
['@id' => '/foos/1'],
372+
['@id' => '/foos/3'],
373+
],
374+
'hydra:totalItems' => 2,
375+
], $actual);
376+
}
331377
}

0 commit comments

Comments
 (0)