Skip to content

Commit a535803

Browse files
committed
temp
1 parent 03df771 commit a535803

File tree

4 files changed

+48
-3
lines changed

4 files changed

+48
-3
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\JsonLd\JsonStreamer\ValueTransformer;
15+
16+
use ApiPlatform\Hydra\Collection;
17+
use Symfony\Component\JsonStreamer\ValueTransformer\ValueTransformerInterface;
18+
use Symfony\Component\TypeInfo\Type;
19+
20+
final class TypeValueTransformer implements ValueTransformerInterface
21+
{
22+
public function transform(mixed $value, array $options = []): mixed
23+
{
24+
if ($options['_current_object'] instanceof Collection) {
25+
return 'Collection';
26+
}
27+
28+
return $options['operation']->getShortName();
29+
}
30+
31+
public static function getStreamValueType(): Type
32+
{
33+
return Type::string();
34+
}
35+
}

src/JsonLd/JsonStreamer/WritePropertyMetadataLoader.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,18 @@ public function load(string $className, array $options = [], array $context = []
4747
return $properties;
4848
}
4949

50-
// Missing @type => $operation->getShortName
51-
5250
$properties['@id'] = new PropertyMetadata(
5351
'id', // virtual property
5452
Type::mixed(), // virtual property
5553
['api_platform.jsonld.json_streamer.write.value_transformer.iri'],
5654
);
5755

56+
$properties['@type'] = new PropertyMetadata(
57+
'id', // virtual property
58+
Type::mixed(), // virtual property
59+
['api_platform.jsonld.json_streamer.write.value_transformer.type'],
60+
);
61+
5862
$originalClassName = TypeHelper::getClassName($context['original_type']);
5963

6064
if (Collection::class === $originalClassName || ($this->resourceClassResolver->isResourceClass($originalClassName) && !isset($context['generated_classes'][Collection::class]))) {

src/Symfony/Bundle/Resources/config/json_streamer.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
<service id="api_platform.json_streamer.stream_reader" class="Symfony\Component\JsonStreamer\JsonStreamReader">
1313
<argument type="tagged_locator" tag="json_streamer.value_transformer" />
14-
<argument type="service" id="api_platform.jsonld.json_streamer.read.property_metadata_loader" />
14+
<argument type="service" id="json_streamer.read.property_metadata_loader" />
1515
<argument>%.json_streamer.stream_readers_dir%</argument>
1616
<argument>%.json_streamer.lazy_ghosts_dir%</argument>
1717
</service>
@@ -26,6 +26,10 @@
2626
<tag name="json_streamer.value_transformer"/>
2727
</service>
2828

29+
<service id="api_platform.jsonld.json_streamer.write.value_transformer.type" class="ApiPlatform\JsonLd\JsonStreamer\ValueTransformer\TypeValueTransformer">
30+
<tag name="json_streamer.value_transformer"/>
31+
</service>
32+
2933
<service id="api_platform.jsonld.json_streamer.write.value_transformer.context" class="ApiPlatform\JsonLd\JsonStreamer\ValueTransformer\ContextValueTransformer">
3034
<argument type="service" id="api_platform.router" />
3135
<tag name="json_streamer.value_transformer"/>

tests/Functional/JsonStreamerTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ public function testJsonStreamer(): void
117117
$this->assertIsBool($res['isFeatured']);
118118
$this->assertIsString($res['price']);
119119
$this->assertEquals('/json_stream_resources/1', $res['@id']);
120+
$this->assertEquals('JsonStreamResource', $res['@type']);
120121
$this->assertEquals('/contexts/JsonStreamResource', $res['@context']);
121122
}
122123

@@ -145,6 +146,7 @@ public function testJsonStreamerCollection(): void
145146
$this->assertEquals('Collection', $res['@type']);
146147
$this->assertArrayHasKey('member', $res);
147148
$this->assertIsArray($res['member']);
149+
$this->assertEquals('JsonStreamResource', $res['member'][0]['@type']);
148150
$this->assertArrayHasKey('totalItems', $res);
149151
$this->assertIsInt($res['totalItems']);
150152
}

0 commit comments

Comments
 (0)