10000 [Serializer] Add Support for `object_to_populate` in CustomNormalizer by chrisguitarguy · Pull Request #21716 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Serializer] Add Support for object_to_populate in CustomNormalizer #21716

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Pull Object to Populate Extraction Into a Trait
And use that trait in `AbstractNormalizer`.
  • Loading branch information
chrisguitarguy committed Sep 27, 2017
commit ffb1e40f20ae40636b04a327b1d41438d9195a30
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
*/
abstract class AbstractNormalizer extends SerializerAwareNormalizer implements NormalizerInterface, DenormalizerInterface, SerializerAwareInterface
{
use ObjectToPopulateTrait;

const CIRCULAR_REFERENCE_LIMIT = 'circular_reference_limit';
const OBJECT_TO_POPULATE = 'object_to_populate';
const GROUPS = 'groups';
Expand Down Expand Up @@ -317,14 +319,8 @@ protected function instantiateObject(array &$data, $class, array &$context, \Ref
$format = null;
}

if (
isset($context[static::OBJECT_TO_POPULATE]) &&
is_object($context[static::OBJECT_TO_POPULATE]) &&
$context[static::OBJECT_TO_POPULATE] instanceof $class
) {
$object = $context[static::OBJECT_TO_POPULATE];
if (null != $object = $this->extractObjectToPopulate($class, $context, static::OBJECT_TO_POPULATE)) {
unset($context[static::OBJECT_TO_POPULATE]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this line be moved in in the trait too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would involve passing context by reference into the trait. That's the only reason I didn't do it.


return $object;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?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 compatability 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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Useless, can be removed.

}
}
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, []);

$this->assertNull($object);
}

public function testExtractObjectToPopulateReturnsNullWhenNonObjectIsProvided()
{
$object = $this->extractObjectToPopulate(ProxyDummy::class, [
'object_to_populate' => 'not an object',
]);

$this->assertNull($object);
}

public function testExtractObjectToPopulateReturnsNullWhenTheClassIsNotAnInstanceOfTheProvidedClass()
{
$object = $this->extractObjectToPopulate(ProxyDummy::class, [
'object_to_populate' => new \stdClass(),
]);

$this->assertNull($object);
}

public function testExtractObjectToPopulateReturnsObjectWhenEverythingChecksOut()
{
$expected = new ProxyDummy();
$object = $this->extractObjectToPopulate(ProxyDummy::class, [
'object_to_populate' => $expected,
]);

$this->assertSame($expected, $object);
}
}
0