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

Skip to content

Commit 731a554

Browse files
committed
[Serializer] Allow to use easily static constructors
1 parent fce0299 commit 731a554

File tree

3 files changed

+67
-2
lines changed

3 files changed

+67
-2
lines changed

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,20 @@ 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 string $class a class name
259+
* @param \ReflectionClass $reflectionClass
260+
*
261+
* @return \ReflectionMethod|null
262+
*/
263+
protected function getConstructor($class, \ReflectionClass $reflectionClass)
264+
{
265+
return $reflectionClass->getConstructor();
266+
}
267+
254268
/**
255269
* Instantiates an object using constructor parameters when needed.
256270
*
@@ -282,7 +296,7 @@ protected function instantiateObject(array &$data, $class, array &$context, \Ref
282296
return $object;
283297
}
284298

285-
$constructor = $reflectionClass->getConstructor();
299+
$constructor = $this->getConstructor($class, $reflectionClass);
286300
if ($constructor) {
287301
$constructorParameters = $constructor->getParameters();
288302

@@ -318,7 +332,11 @@ protected function instantiateObject(array &$data, $class, array &$context, \Ref
318332
}
319333
}
320334

321-
return $reflectionClass->newInstanceArgs($params);
335+
if ($constructor->isConstructor()) {
336+
return $reflectionClass->newInstanceArgs($params);
337+
} else {
338+
return $constructor->invokeArgs(null, $params);
339+
}
322340
}
323341

324342
return new $class();
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
$dummy = new self();
22+
$dummy->quz = $foo;
23+
24+
return $dummy;
25+
}
26+
27+
private function __construct()
28+
{
29+
}
30+
}

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
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;
1213

1314
/**
1415
* Provides a dummy Normalizer which extends the AbstractNormalizer.
@@ -103,4 +104,20 @@ public function testObjectToPopulateWithProxy()
103104

104105
$this->assertSame('bar', $proxyDummy->getFoo());
105106
}
107+
108+
public function testObjectWithStaticConstructor()
109+
{
110+
$normalizer = $this->getMockBuilder(ObjectNormalizer::class)
111+
->setMethods(array('getConstructor'))
112+
->getMock();
113+
$normalizer->expects($this->once())
114+
->method('getConstructor')
115+
->with(StaticConstructorDummy::class)
116+
->willReturn(new \ReflectionMethod(StaticConstructorDummy::class, 'create'));
117+
$dummy = $normalizer->denormalize(array('foo' => 'baz'), StaticConstructorDummy::class);
118+
119+
$this->assertInstanceOf(StaticConstructorDummy::class, $dummy);
120+
$this->assertEquals('baz', $dummy->quz);
121+
$this->assertNull($dummy->foo);
122+
}
106123
}

0 commit comments

Comments
 (0)
0