8000 [Serializer] Allow to use easily static constructors · symfony/symfony@9be6484 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9be6484

Browse files
committed
[Serializer] Allow to use easily static constructors
1 parent 06f5c86 commit 9be6484

File tree

4 files changed

+98
-2
lines changed

4 files changed

+98
-2
lines changed

src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,23 @@ protected function prepareForDenormalization($data)
251251
return (array) $data;
252252
}
253253

254+
/**
255+
* Returns the method to use to construct an object. This method must be either
256+
* the object constructor or static.
257+
*
258+
* @param array $data
259+
* @param string $class
260+
* @param array $context
261+
* @param \ReflectionClass $reflectionClass
262+
* @param array|bool $allowedAttributes
263+
*
264+
* @return \ReflectionMethod|null
265+
*/
266+
protected function getConstructor(array &$data, $class, array &$context, \ReflectionClass $reflectionClass, $allowedAttributes)
267+
{
268+
return $reflectionClass->getConstructor();
269+
}
270+
254271
/**
255272
* Instantiates an object using constructor parameters when needed.
256273
*
@@ -282,7 +299,7 @@ protected function instantiateObject(array &$data, $class, array &$context, \Ref
282299
return $object;
283300
}
284301

285-
$constructor = $reflectionClass->getConstructor();
302+
$constructor = $this->getConstructor($data, $class, $context, $reflectionClass, $allowedAttributes);
286303
if ($constructor) {
287304
$constructorParameters = $constructor->getParameters();
288305

@@ -318,7 +335,11 @@ protected function instantiateObject(array &$data, $class, array &$context, \Ref
318335
}
319336
}
320337

321-
return $reflectionClass->newInstanceArgs($params);
338+
if ($constructor->isConstructor()) {
339+
return $reflectionClass->newInstanceArgs($params);
340+
} else {
341+
return $constructor->invokeArgs(null, $params);
342+
}
322343
}
323344

324345
return new $class();
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Serializer\Tests\Fixtures;
13+
14+
class StaticConstructorDummy
15+
{
16+
public $foo;
17+
public $bar;
18+
public $quz;
19+
20+
public static function create($foo)
21+
{
22+
$dummy = new self();
23+
$dummy->quz = $foo;
24+
25+
return $dummy;
26+
}
27+
28+
private function __construct()
29+
{
30+
}
31+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Serializer\Tests\Fixtures;
13+
14+
use Symfony\Component\Serializ 6D40 er\Normalizer\ObjectNormalizer;
15+
16+
/**
17+
* @author Guilhem N. <egetick@gmail.com>
18+
*/
19+
class StaticConstructorNormalizer extends ObjectNormalizer
20+
{
21+
/**
22+
* {@inheritdoc}
23+
*/
24+
protected function getConstructor(array &$data, $class, array &$context, \ReflectionClass $reflectionClass, $allowedAttributes)
25+
{
26+
if (is_a($class, StaticConstructorDummy::class, true)) {
27+
return new \ReflectionMethod($class, 'create');
28+
}
29+
30+
return parent::getConstructor($data, $class, $context, $reflectionClass, $allowedAttributes);
31+
}
32+
}

src/Symfony/Component/Serializer/Tests/Normalizer/AbstractNormalizerTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
1010
use Symfony\Component\Serializer\Tests\Fixtures\AbstractNormalizerDummy;
1111
use Symfony\Component\Serializer\Tests\Fixtures\ProxyDummy;
12+
use Symfony\Component\Serializer\Tests\Fixtures\StaticConstructorDummy;
13+
use Symfony\Component\Serializer\Tests\Fixtures\StaticConstructorNormalizer;
1214

1315
/**
1416
* Provides a dummy Normalizer which extends the AbstractNormalizer.
@@ -103,4 +105,14 @@ public function testObjectToPopulateWithProxy()
103105

104106
$this->assertSame('bar', $proxyDummy->getFoo());
105107
}
108+
109+
public function testObjectWithStaticConstructor()
110+
{
111+
$normalizer = new StaticConstructorNormalizer();
112+
$dummy = $normalizer->denormalize(array('foo' => 'baz'), StaticConstructorDummy::class);
113+
114+
$this->assertInstanceOf(StaticConstructorDummy::class, $dummy);
115+
$this->assertEquals('baz', $dummy->quz);
116+
$this->assertNull($dummy->foo);
117+
}
106118
}

0 commit comments

Comments
 (0)
0