Skip to content

Commit 9a0e4c2

Browse files
committed
DataTransformer supports with prenormalized data
1 parent 63716ce commit 9a0e4c2

File tree

25 files changed

+211
-187
lines changed

25 files changed

+211
-187
lines changed

src/Bridge/Symfony/Bundle/Resources/config/api.xml

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@
9999
<argument type="service" id="api_platform.item_data_provider" on-invalid="ignore" />
100100
<argument>%api_platform.allow_plain_identifiers%</argument>
101101
<argument>null</argument>
102-
<argument type="service" id="api_platform.data_transformer" on-invalid="ignore" />
102+
<argument type="tagged" tag="api_platform.data_transformer" on-invalid="ignore" />
103103
<argument type="service" id="api_platform.metadata.resource.metadata_factory" on-invalid="ignore" />
104104
<argument>true</argument>
105105

@@ -287,12 +287,6 @@
287287
<service id="api_platform.cache.subresource_operation_factory" parent="cache.system" public="false">
288288
<tag name="cache.pool" />
289289
</service>
290-
291-
<service id="api_platform.data_transformer.chain_transformer" class="ApiPlatform\Core\DataTransformer\ChainDataTransformer" public="false">
292-
<argument type="tagged" tag="api_platform.data_transformer" />
293-
</service>
294-
295-
<service id="api_platform.data_transformer" alias="api_platform.data_transformer.chain_transformer" />
296290
</services>
297291

298292
</container>

src/Bridge/Symfony/Bundle/Resources/config/graphql.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
<argument type="service" id="api_platform.item_data_provider" on-invalid="ignore" />
7878
<argument>%api_platform.allow_plain_identifiers%</argument>
7979
<argument>null</argument>
80-
<argument type="service" id="api_platform.data_transformer" on-invalid="ignore" />
80+
<argument type="tagged" tag="api_platform.data_transformer" on-invalid="ignore" />
8181
<argument type="service" id="api_platform.metadata.resource.metadata_factory" on-invalid="ignore" />
8282
<argument>true</argument>
8383

src/Bridge/Symfony/Bundle/Resources/config/hal.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
<argument>null</argument>
3838
<argument>false</argument>
3939
<argument type="collection"></argument>
40-
<argument type="service" id="api_platform.data_transformer" on-invalid="ignore" />
40+
<argument type="tagged" tag="api_platform.data_transformer" on-invalid="ignore" />
4141
<argument type="service" id="api_platform.metadata.resource.metadata_factory" on-invalid="ignore" />
4242
<argument>true</argument>
4343

src/Bridge/Symfony/Bundle/Resources/config/jsonapi.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
<argument type="service" id="api_platform.jsonapi.name_converter.reserved_attribute_name" />
4141
<argument type="service" id="api_platform.metadata.resource.metadata_factory" />
4242
<argument type="collection"></argument>
43-
<argument type="service" id="api_platform.data_transformer" on-invalid="ignore" />
43+
<argument type="tagged" tag="api_platform.data_transformer" on-invalid="ignore" />
4444
<argument>true</argument>
4545

4646
<!-- After the DataURINormalizer but before the object denormalizer -->

src/Bridge/Symfony/Bundle/Resources/config/jsonld.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
<argument type="service" id="api_platform.name_converter" on-invalid="ignore" />
2727
<argument type="service" id="serializer.mapping.class_metadata_factory" on-invalid="ignore" />
2828
<argument type="collection" />
29-
<argument type="service" id="api_platform.data_transformer" on-invalid="ignore" />
29+
<argument type="tagged" tag="api_platform.data_transformer" on-invalid="ignore" />
3030
<argument>true</argument>
3131

3232
<!-- After the DataURINormalizer but before the object denormalizer -->

src/DataTransformer/ChainDataTransformer.php

Lines changed: 0 additions & 57 deletions
This file was deleted.

src/GraphQl/Serializer/ItemNormalizer.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
use ApiPlatform\Core\Metadata\Property\PropertyMetadata;
1717
use ApiPlatform\Core\Serializer\ItemNormalizer as BaseItemNormalizer;
18-
use ApiPlatform\Core\Util\ClassInfoTrait;
1918

2019
/**
2120
* GraphQL normalizer.
@@ -24,8 +23,6 @@
2423
*/
2524
final class ItemNormalizer extends BaseItemNormalizer
2625
{
27-
use ClassInfoTrait;
28-
2926
const FORMAT = 'graphql';
3027
const ITEM_KEY = '#item';
3128

@@ -42,11 +39,7 @@ public function supportsNormalization($data, $format = null)
4239
*/
4340
public function normalize($object, $format = null, array $context = [])
4441
{
45-
$outputClass = $this->getOutputClass($this->getObjectClass($object), $context);
46-
if (null !== $outputClass && null !== $this->dataTransformer && $this->dataTransformer->supportsTransformation($object, $outputClass, $context)) {
47-
$object = $this->dataTransformer->transform($object, $outputClass, $context);
48-
}
49-
42+
$object = $this->transformOutput($object, $context);
5043
$data = parent::normalize($object, $format, $context);
5144
$data[self::ITEM_KEY] = serialize($object); // calling serialize prevent weird normalization process done by Webonyx's GraphQL PHP
5245

src/Hal/Serializer/ItemNormalizer.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,7 @@ public function normalize($object, $format = null, array $context = [])
5252
$context['cache_key'] = $this->getHalCacheKey($format, $context);
5353
}
5454

55-
$outputClass = $this->getOutputClass($this->getObjectClass($object), $context);
56-
if (null !== $outputClass && null !== $this->dataTransformer && $this->dataTransformer->supportsTransformation($object, $outputClass, $context)) {
57-
$object = $this->dataTransformer->transform($object, $outputClass, $context);
58-
}
55+
$object = $this->transformOutput($object, $context);
5956

6057
try {
6158
$resourceClass = $this->resourceClassResolver->getResourceClass($object, $context['resource_class'] ?? null, true);

src/JsonApi/Serializer/ItemNormalizer.php

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,13 @@
1616
use ApiPlatform\Core\Api\IriConverterInterface;
1717
use ApiPlatform\Core\Api\OperationType;
1818
use ApiPlatform\Core\Api\ResourceClassResolverInterface;
19-
use ApiPlatform\Core\DataTransformer\DataTransformerInterface;
2019
use ApiPlatform\Core\Exception\InvalidArgumentException;
2120
use ApiPlatform\Core\Exception\ItemNotFoundException;
2221
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
2322
use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
2423
use ApiPlatform\Core\Metadata\Property\PropertyMetadata;
2524
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
2625
use ApiPlatform\Core\Serializer\AbstractItemNormalizer;
27-
use ApiPlatform\Core\Util\ClassInfoTrait;
2826
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
2927
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
3028
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
@@ -39,15 +37,13 @@
3937
*/
4038
final class ItemNormalizer extends AbstractItemNormalizer
4139
{
42-
use ClassInfoTrait;
43-
4440
const FORMAT = 'jsonapi';
4541

4642
private $componentsCache = [];
4743

48-
public function __construct(PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, IriConverterInterface $iriConverter, ResourceClassResolverInterface $resourceClassResolver, PropertyAccessorInterface $propertyAccessor = null, NameConverterInterface $nameConverter = null, ResourceMetadataFactoryInterface $resourceMetadataFactory, array $defaultContext = [], DataTransformerInterface $dataTransformer = null, bool $allowUnmappedClass = false)
44+
public function __construct(PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, IriConverterInterface $iriConverter, ResourceClassResolverInterface $resourceClassResolver, PropertyAccessorInterface $propertyAccessor = null, NameConverterInterface $nameConverter = null, ResourceMetadataFactoryInterface $resourceMetadataFactory, array $defaultContext = [], iterable $dataTransformers = [], bool $allowUnmappedClass = false)
4945
{
50-
parent::__construct($propertyNameCollectionFactory, $propertyMetadataFactory, $iriConverter, $resourceClassResolver, $propertyAccessor, $nameConverter, null, null, false, $defaultContext, $dataTransformer, $resourceMetadataFactory, $allowUnmappedClass);
46+
parent::__construct($propertyNameCollectionFactory, $propertyMetadataFactory, $iriConverter, $resourceClassResolver, $propertyAccessor, $nameConverter, null, null, false, $defaultContext, $dataTransformers, $resourceMetadataFactory, $allowUnmappedClass);
5147
}
5248

5349
/**
@@ -67,10 +63,7 @@ public function normalize($object, $format = null, array $context = [])
6763
$context['cache_key'] = $this->getJsonApiCacheKey($format, $context);
6864
}
6965

70-
$outputClass = $this->getOutputClass($this->getObjectClass($object), $context);
71-
if (null !== $outputClass && null !== $this->dataTransformer && $this->dataTransformer->supportsTransformation($object, $outputClass, $context)) {
72-
$object = $this->dataTransformer->transform($object, $outputClass, $context);
73-
}
66+
$object = $this->transformOutput($object, $context);
7467

7568
// Get and populate attributes data
7669
$objectAttributesData = parent::normalize($object, $format, $context);

src/JsonLd/Serializer/ItemNormalizer.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
use ApiPlatform\Core\Api\IriConverterInterface;
1717
use ApiPlatform\Core\Api\ResourceClassResolverInterface;
18-
use ApiPlatform\Core\DataTransformer\DataTransformerInterface;
1918
use ApiPlatform\Core\Exception\InvalidArgumentException;
2019
use ApiPlatform\Core\JsonLd\ContextBuilderInterface;
2120
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
@@ -43,9 +42,9 @@ final class ItemNormalizer extends AbstractItemNormalizer
4342

4443
private $contextBuilder;
4544

46-
public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFactory, PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, IriConverterInterface $iriConverter, ResourceClassResolverInterface $resourceClassResolver, ContextBuilderInterface $contextBuilder, PropertyAccessorInterface $propertyAccessor = null, NameConverterInterface $nameConverter = null, ClassMetadataFactoryInterface $classMetadataFactory = null, array $defaultContext = [], DataTransformerInterface $dataTransformer = null, bool $allowUnmappedClass = false)
45+
public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFactory, PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, IriConverterInterface $iriConverter, ResourceClassResolverInterface $resourceClassResolver, ContextBuilderInterface $contextBuilder, PropertyAccessorInterface $propertyAccessor = null, NameConverterInterface $nameConverter = null, ClassMetadataFactoryInterface $classMetadataFactory = null, array $defaultContext = [], iterable $dataTransformers = [], bool $allowUnmappedClass = false)
4746
{
48-
parent::__construct($propertyNameCollectionFactory, $propertyMetadataFactory, $iriConverter, $resourceClassResolver, $propertyAccessor, $nameConverter, $classMetadataFactory, null, false, $defaultContext, $dataTransformer, $resourceMetadataFactory, $allowUnmappedClass);
47+
parent::__construct($propertyNameCollectionFactory, $propertyMetadataFactory, $iriConverter, $resourceClassResolver, $propertyAccessor, $nameConverter, $classMetadataFactory, null, false, $defaultContext, $dataTransformers, $resourceMetadataFactory, $allowUnmappedClass);
4948

5049
$this->contextBuilder = $contextBuilder;
5150
}
@@ -63,10 +62,7 @@ public function supportsNormalization($data, $format = null)
6362
*/
6463
public function normalize($object, $format = null, array $context = [])
6564
{
66-
$outputClass = $this->getOutputClass($this->getObjectClass($object), $context);
67-
if (null !== $outputClass && null !== $this->dataTransformer && $this->dataTransformer->supportsTransformation($object, $outputClass, $context)) {
68-
$object = $this->dataTransformer->transform($object, $outputClass, $context);
69-
}
65+
$object = $this->transformOutput($object, $context);
7066

7167
try {
7268
$resourceClass = $this->resourceClassResolver->getResourceClass($object, $context['resource_class'] ?? null, true);

0 commit comments

Comments
 (0)