forked from symfony/symfony
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature symfony#19277 [Serializer] Argument objects (theofidry, dunglas)
This PR was merged into the 3.2-dev branch. Discussion ---------- [Serializer] Argument objects | Q | A | ------------- | --- | Branch? | 3.1 | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | TODO | Fixed tickets | none | License | MIT | Doc PR | TODO Assuming with have the two following entities: ```php namespace AppBundle\Entity; class Dummy { public function __construct(int $id, string $name, string $email, AnotherDummy $anotherDummy) { $this->id = $id; $this->name = $name; $this->email = $email; $this->anotherDummy = $anotherDummy; } } class AnotherDummy { public function __construct(int $id, string $uuid, bool $isEnabled) { $this->id = $id; $this->uuid = $uuid; $this->isEnabled = $isEnabled; } } ``` Doing the following will fail: ```php $serializer->denormalize( [ 'id' => $i, 'name' => 'dummy', 'email' => '[email protected]', 'another_dummy' => [ 'id' => 1000 + $i, 'uuid' => 'azerty', 'is_enabled' => true, ], ], \AppBundle\Entity\Dummy::class ); ``` with a type error, because the 4th argument passed to `Dummy::__construct()` will be an array. The following patch checks if the type of the argument is an object, and if it is tries to denormalize that object as well. I'm not sure if it's me missing something or this is a use case that has been omitted (willingly or not), but if it's a valuable patch I would be happy to work on finishing it. Commits ------- 988eba1 fix tests 98bcb91 Merge pull request #1 from dunglas/theofidry-feature/param-object 7b5d55d Prevent BC in instantiateObject e437e04 fix reflection type 3fe9802 revert CS 5556fa5 fix d4cdb00 fix CS 93608dc Add deprecation message f46a176 Apply patch f361e52 fix tests 4884a2e f1 e64e999 Address comments e99a90b Add tests 7bd4ac5 Test
- Loading branch information
Showing
6 changed files
with
152 additions
and
5 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
43 changes: 43 additions & 0 deletions
43
src/Symfony/Component/Serializer/Tests/Fixtures/DenormalizerDecoratorSerializer.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,43 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\Serializer\Tests\Fixtures; | ||
|
||
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; | ||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; | ||
use Symfony\Component\Serializer\SerializerInterface; | ||
|
||
/** | ||
* @author Théo FIDRY <[email protected]> | ||
*/ | ||
class DenormalizerDecoratorSerializer implements SerializerInterface | ||
{ | ||
private $normalizer; | ||
|
||
/** | ||
* @param NormalizerInterface|DenormalizerInterface $normalizer | ||
*/ | ||
public function __construct($normalizer) | ||
{ | ||
if (false === $normalizer instanceof NormalizerInterface && false === $normalizer instanceof DenormalizerInterface) { | ||
throw new \InvalidArgumentException(); | ||
} | ||
|
||
$this->normalizer = $normalizer; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function serialize($data, $format, array $context = array()) | ||
{ | ||
return $this->normalizer->normalize($data, $format, $context); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function deserialize($data, $type, $format, array $context = array()) | ||
{ | ||
return $this->normalizer->denormalize($data, $type, $format, $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