Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pre hydrate input before denormalize (#3701)
* Add possibility to hydrate input before denormalize * Fix @soyuka review * Add test * Remove useless set context * Add deep object to populate to avoid missing data * change name to inputClass to be more explicit * Fix test * Rename PreHydrateInput to DataTransformerInitializer * Add behat test data transformer initializer * fix tests Co-authored-by: Alexandre Vinet <contact@alexandrevinet.fr>
- Loading branch information
1 parent
32249e9
commit 6e2538b
Showing
12 changed files
with
277 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
src/DataTransformer/DataTransformerInitializerInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?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. | ||
* | ||
* @return object|null | ||
*/ | ||
public function initialize(string $inputClass, array $context = []); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
tests/Fixtures/TestBundle/DataTransformer/InitializeInputDataTransformer.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?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\Tests\Fixtures\TestBundle\DataTransformer; | ||
|
||
use ApiPlatform\Core\DataTransformer\DataTransformerInitializerInterface; | ||
use ApiPlatform\Core\Serializer\AbstractItemNormalizer; | ||
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\InitializeInput as InitializeInputDocument; | ||
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Dto\InitializeInputDto; | ||
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\InitializeInput; | ||
|
||
final class InitializeInputDataTransformer implements DataTransformerInitializerInterface | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function transform($object, string $to, array $context = []) | ||
{ | ||
/** @var InitializeInputDto */ | ||
$data = $object; | ||
|
||
/** @var InitializeInput|InitializeInputDocument */ | ||
$resourceObject = $context[AbstractItemNormalizer::OBJECT_TO_POPULATE] ?? new $context['resource_class'](); | ||
$resourceObject->name = $data->name; | ||
|
||
return $resourceObject; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function initialize(string $inputClass, array $context = []) | ||
{ | ||
$currentResource = $context[AbstractItemNormalizer::OBJECT_TO_POPULATE] ?? null; | ||
if (!$currentResource) { | ||
return new InitializeInputDto(); | ||
} | ||
|
||
$dto = new InitializeInputDto(); | ||
$dto->manager = $currentResource->manager; | ||
|
||
return $dto; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supportsTransformation($data, string $to, array $context = []): bool | ||
{ | ||
return (InitializeInput::class === $to || InitializeInputDocument::class === $to) && InitializeInputDto::class === ($context['input']['class'] ?? null); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?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\Tests\Fixtures\TestBundle\Document; | ||
|
||
use ApiPlatform\Core\Annotation\ApiResource; | ||
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Dto\InitializeInputDto; | ||
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM; | ||
|
||
/** | ||
* @ApiResource(input=InitializeInputDto::class) | ||
* @ODM\Document | ||
*/ | ||
class InitializeInput | ||
{ | ||
/** | ||
* @ODM\Id(strategy="NONE", type="integer") | ||
*/ | ||
public $id; | ||
|
||
/** | ||
* @ODM\Field | ||
*/ | ||
public $manager; | ||
|
||
/** | ||
* @ODM\Field | ||
*/ | ||
public $name; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?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\Tests\Fixtures\TestBundle\Dto; | ||
|
||
class InitializeInputDto | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
public $name; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
public $manager; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?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\Tests\Fixtures\TestBundle\Entity; | ||
|
||
use ApiPlatform\Core\Annotation\ApiResource; | ||
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Dto\InitializeInputDto; | ||
use Doctrine\ORM\Mapping as ORM; | ||
|
||
/** | ||
* @ApiResource(input=InitializeInputDto::class) | ||
* @ORM\Entity | ||
*/ | ||
class InitializeInput | ||
{ | ||
/** | ||
* @ORM\Id | ||
* @ORM\Column(type="integer") | ||
*/ | ||
public $id; | ||
|
||
/** | ||
* @ORM\Column | ||
*/ | ||
public $manager; | ||
|
||
/** | ||
* @ORM\Column | ||
*/ | ||
public $name; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.