Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #21716 [Serializer] Add Support for
object_to_populate
in C…
…ustomNormalizer (chrisguitarguy) This PR was squashed before being merged into the 3.4 branch (closes #21716). Discussion ---------- [Serializer] Add Support for `object_to_populate` in CustomNormalizer | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #21715 | License | MIT | Doc PR | n/a This pulls a trait out of `AbstractNormalizer` with a method to extract the object to populate and adds some tests for it. Then uses that trait in both `AbstractNormalizer` and `CustomNormalizer` so both can support the `object_to_populate` key. Commits ------- ec9242d [Serializer] Add Support for in CustomNormalizer
- Loading branch information
Showing
5 changed files
with
107 additions
and
9 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
41 changes: 41 additions & 0 deletions
41
src/Symfony/Component/Serializer/Normalizer/ObjectToPopulateTrait.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,41 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Serializer\Normalizer; | ||
|
||
trait ObjectToPopulateTrait | ||
{ | ||
/** | ||
* Extract the `object_to_populate` field from the context if it exists | ||
* and is an instance of the provided $class. | ||
* | ||
* @param string $class The class the object should be | ||
* @param $context The denormalization context | ||
* @param string $key They in which to look for the object to populate. | ||
* Keeps backwards compatibility with `AbstractNormalizer`. | ||
* | ||
* @return object|null an object if things check out, null otherwise | ||
*/ | ||
protected function extractObjectToPopulate($class, array $context, $key = null) | ||
{ | ||
$key = $key ?: 'object_to_populate'; | ||
|
||
if ( | ||
isset($context[$key]) && | ||
is_object($context[$key]) && | ||
$context[$key] instanceof $class | ||
) { | ||
return $context[$key]; | ||
} | ||
|
||
return 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
47 changes: 47 additions & 0 deletions
47
src/Symfony/Component/Serializer/Tests/Normalizer/ObjectToPopulateTraitTest.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,47 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\Serializer\Tests\Normalizer; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Serializer\Normalizer\ObjectToPopulateTrait; | ||
use Symfony\Component\Serializer\Tests\Fixtures\ProxyDummy; | ||
|
||
class ObjectToPopulateTraitTest extends TestCase | ||
{ | ||
use ObjectToPopulateTrait; | ||
|
||
public function testExtractObjectToPopulateReturnsNullWhenKeyIsMissing() | ||
{ | ||
$object = $this->extractObjectToPopulate(ProxyDummy::class, array()); | ||
|
||
$this->assertNull($object); | ||
} | ||
|
||
public function testExtractObjectToPopulateReturnsNullWhenNonObjectIsProvided() | ||
{ | ||
$object = $this->extractObjectToPopulate(ProxyDummy::class, array( | ||
'object_to_populate' => 'not an object', | ||
)); | ||
|
||
$this->assertNull($object); | ||
} | ||
|
||
public function testExtractObjectToPopulateReturnsNullWhenTheClassIsNotAnInstanceOfTheProvidedClass() | ||
{ | ||
$object = $this->extractObjectToPopulate(ProxyDummy::class, array( | ||
'object_to_populate' => new \stdClass(), | ||
)); | ||
|
||
$this->assertNull($object); | ||
} | ||
|
||
public function testExtractObjectToPopulateReturnsObjectWhenEverythingChecksOut() | ||
{ | ||
$expected = new ProxyDummy(); | ||
$object = $this->extractObjectToPopulate(ProxyDummy::class, array( | ||
'object_to_populate' => $expected, | ||
)); | ||
|
||
$this->assertSame($expected, $object); | ||
} | ||
} |