Skip to content

Commit

Permalink
Rename PreHydrateInput to DataTransformerInitializer
Browse files Browse the repository at this point in the history
  • Loading branch information
soyuka committed Sep 1, 2020
1 parent fcfb8e7 commit bae152d
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 30 deletions.
Expand Up @@ -683,6 +683,9 @@ private function registerDataTransformerConfiguration(ContainerBuilder $containe
{
$container->registerForAutoconfiguration(DataTransformerInterface::class)
->addTag('api_platform.data_transformer');

$container->registerForAutoconfiguration(DataTransformerInitializerInterface::class)
->addTag('api_platform.data_transformer');
}

private function registerSecurityConfiguration(ContainerBuilder $container, XmlFileLoader $loader): void
Expand Down
24 changes: 24 additions & 0 deletions src/DataTransformer/DataTransformerInitializerInterface.php
@@ -0,0 +1,24 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Core\DataTransformer;

interface DataTransformerInitializerInterface extends DataTransformerInterface
{
/**
* Creates a new DTO object that the data will then be serialized into (using object_to_populate).
*
* This is useful to "initialize" the DTO object based on the current resource's data.
*/
public function initialize(string $inputClass, array $context = []): ?object;
}
24 changes: 0 additions & 24 deletions src/DataTransformer/PreHydrateInputInterface.php

This file was deleted.

6 changes: 3 additions & 3 deletions src/Serializer/AbstractItemNormalizer.php
Expand Up @@ -16,8 +16,8 @@
use ApiPlatform\Core\Api\IriConverterInterface;
use ApiPlatform\Core\Api\ResourceClassResolverInterface;
use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
use ApiPlatform\Core\DataTransformer\DataTransformerInitializerInterface;
use ApiPlatform\Core\DataTransformer\DataTransformerInterface;
use ApiPlatform\Core\DataTransformer\PreHydrateInputInterface;
use ApiPlatform\Core\Exception\InvalidArgumentException;
use ApiPlatform\Core\Exception\InvalidValueException;
use ApiPlatform\Core\Exception\ItemNotFoundException;
Expand Down Expand Up @@ -198,8 +198,8 @@ public function denormalize($data, $class, $format = null, array $context = [])
if (!$this->serializer instanceof DenormalizerInterface) {
throw new LogicException('Cannot denormalize the input because the injected serializer is not a denormalizer');
}
if ($dataTransformer instanceof PreHydrateInputInterface) {
$context[AbstractObjectNormalizer::OBJECT_TO_POPULATE] = $dataTransformer->createInput($inputClass, $context);
if ($dataTransformer instanceof DataTransformerInitializerInterface) {
$context[AbstractObjectNormalizer::OBJECT_TO_POPULATE] = $dataTransformer->initialize($inputClass, $context);
$context[AbstractObjectNormalizer::DEEP_OBJECT_TO_POPULATE] = true;
}
$denormalizedInput = $this->serializer->denormalize($data, $inputClass, $format, $context);
Expand Down
Expand Up @@ -63,6 +63,7 @@
use ApiPlatform\Core\DataProvider\Pagination;
use ApiPlatform\Core\DataProvider\PaginationOptions;
use ApiPlatform\Core\DataProvider\SubresourceDataProviderInterface;
use ApiPlatform\Core\DataTransformer\DataTransformerInitializerInterface;
use ApiPlatform\Core\DataTransformer\DataTransformerInterface;
use ApiPlatform\Core\Exception\FilterValidationException;
use ApiPlatform\Core\Exception\InvalidArgumentException;
Expand Down Expand Up @@ -1141,6 +1142,10 @@ private function getBaseContainerBuilderProphecy(array $doctrineIntegrationsToLo
->willReturn($this->childDefinitionProphecy)->shouldBeCalledTimes(1);
$this->childDefinitionProphecy->addTag('api_platform.data_transformer')->shouldBeCalledTimes(1);

$containerBuilderProphecy->registerForAutoconfiguration(DataTransformerInitializerInterface::class)
->willReturn($this->childDefinitionProphecy)->shouldBeCalledTimes(1);
$this->childDefinitionProphecy->addTag('api_platform.data_transformer')->shouldBeCalledTimes(1);

$containerBuilderProphecy->addResource(Argument::type(DirectoryResource::class))->shouldBeCalled();

$parameters = [
Expand Down
6 changes: 3 additions & 3 deletions tests/Serializer/AbstractItemNormalizerTest.php
Expand Up @@ -16,8 +16,8 @@
use ApiPlatform\Core\Api\IriConverterInterface;
use ApiPlatform\Core\Api\ResourceClassResolverInterface;
use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
use ApiPlatform\Core\DataTransformer\DataTransformerInitializerInterface;
use ApiPlatform\Core\DataTransformer\DataTransformerInterface;
use ApiPlatform\Core\DataTransformer\PreHydrateInputInterface;
use ApiPlatform\Core\Exception\InvalidArgumentException;
use ApiPlatform\Core\Exception\ItemNotFoundException;
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
Expand Down Expand Up @@ -430,8 +430,8 @@ public function testCanDenormalizeInputClassWithDifferentFieldsThanResourceClass
$resourceClassResolverProphecy->getResourceClass(null, DummyForAdditionalFields::class)->willReturn(DummyForAdditionalFields::class);

$inputDataTransformerProphecy = $this->prophesize(DataTransformerInterface::class);
$inputDataTransformerProphecy->willImplement(PreHydrateInputInterface::class);
$inputDataTransformerProphecy->createInput(DummyForAdditionalFieldsInput::class, $cleanedContext)->willReturn($preHydratedDummy);
$inputDataTransformerProphecy->willImplement(DataTransformerInitializerInterface::class);
$inputDataTransformerProphecy->initialize(DummyForAdditionalFieldsInput::class, $cleanedContext)->willReturn($preHydratedDummy);
$inputDataTransformerProphecy->supportsTransformation($data, DummyForAdditionalFields::class, $augmentedContext)->willReturn(true);
$inputDataTransformerProphecy->transform($dummyInputDto, DummyForAdditionalFields::class, $augmentedContext)->willReturn($dummy);

Expand Down

0 comments on commit bae152d

Please sign in to comment.