From 704760b276481da2bba0e4543ef47107585aa31f Mon Sep 17 00:00:00 2001 From: Christophe Coevoet Date: Sat, 1 Aug 2015 21:55:55 +0200 Subject: [PATCH 1/3] Add support for variadic arguments in the GetSetNormalizer --- .../Normalizer/GetSetMethodNormalizer.php | 12 +++++++-- .../Fixtures/VariadicConstructorArgsDummy.php | 27 +++++++++++++++++++ .../Normalizer/GetSetMethodNormalizerTest.php | 22 +++++++++++++++ 3 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 src/Symfony/Component/Serializer/Tests/Fixtures/VariadicConstructorArgsDummy.php diff --git a/src/Symfony/Component/Serializer/Normalizer/GetSetMethodNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/GetSetMethodNormalizer.php index ac1c54def9..4adca3fe59 100644 --- a/src/Symfony/Component/Serializer/Normalizer/GetSetMethodNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/GetSetMethodNormalizer.php @@ -136,11 +136,19 @@ class GetSetMethodNormalizer extends SerializerAwareNormalizer implements Normal foreach ($constructorParameters as $constructorParameter) { $paramName = lcfirst($this->formatAttribute($constructorParameter->name)); - if (isset($normalizedData[$paramName])) { + if (method_exists($constructorParameter, 'isVariadic') && $constructorParameter->isVariadic()) { + if (isset($normalizedData[$paramName])) { + if (!is_array($normalizedData[$paramName])) { + throw new RuntimeException(sprintf('Cannot create an instance of %s from serialized data because the variadic parameter %s can only accept an array.', $class, $constructorParameter->name)); + } + + $params = array_merge($params, $normalizedData[$paramName]); + } + } elseif (isset($normalizedData[$paramName])) { $params[] = $normalizedData[$paramName]; // don't run set for a parameter passed to the constructor unset($normalizedData[$paramName]); - } elseif ($constructorParameter->isOptional()) { + } elseif ($constructorParameter->isDefaultValueAvailable()) { $params[] = $constructorParameter->getDefaultValue(); } else { throw new RuntimeException( diff --git a/src/Symfony/Component/Serializer/Tests/Fixtures/VariadicConstructorArgsDummy.php b/src/Symfony/Component/Serializer/Tests/Fixtures/VariadicConstructorArgsDummy.php new file mode 100644 index 0000000000..c04aeba0c4 --- /dev/null +++ b/src/Symfony/Component/Serializer/Tests/Fixtures/VariadicConstructorArgsDummy.php @@ -0,0 +1,27 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Serializer\Tests\Fixtures; + +class VariadicConstructorArgsDummy +{ + private $foo; + + public function __construct(...$foo) + { + $this->foo = $foo; + } + + public function getFoo() + { + return $this->foo; + } +} diff --git a/src/Symfony/Component/Serializer/Tests/Normalizer/GetSetMethodNormalizerTest.php b/src/Symfony/Component/Serializer/Tests/Normalizer/GetSetMethodNormalizerTest.php index b22ccb5319..934b81b2e7 100644 --- a/src/Symfony/Component/Serializer/Tests/Normalizer/GetSetMethodNormalizerTest.php +++ b/src/Symfony/Component/Serializer/Tests/Normalizer/GetSetMethodNormalizerTest.php @@ -117,6 +117,28 @@ class GetSetMethodNormalizerTest extends \PHPUnit_Framework_TestCase $this->assertEquals(array(1, 2, 3), $obj->getBaz()); } + /** + * @requires PHP 5.6 + */ + public function testConstructorDenormalizeWithVariadicArgument() + { + $obj = $this->normalizer->denormalize( + array('foo' => array(1, 2, 3)), + 'Symfony\Component\Serializer\Tests\Fixtures\VariadicConstructorArgsDummy', 'any'); + $this->assertEquals(array(1, 2, 3), $obj->getFoo()); + } + + /** + * @requires PHP 5.6 + */ + public function testConstructorDenormalizeWithMissingVariadicArgument() + { + $obj = $this->normalizer->denormalize( + array(), + 'Symfony\Component\Serializer\Tests\Fixtures\VariadicConstructorArgsDummy', 'any'); + $this->assertEquals(array(), $obj->getFoo()); + } + public function testConstructorWithObjectDenormalize() { $data = new \stdClass(); From 500c57e6df33434a1a4657e0a0bb880be4d3cbf3 Mon Sep 17 00:00:00 2001 From: Mathieu Rochette Date: Wed, 8 Jul 2015 17:38:36 +0200 Subject: [PATCH 2/3] [Yaml] Nested merge keys --- src/Symfony/Component/Yaml/Parser.php | 10 ++++++---- .../Component/Yaml/Tests/Fixtures/sfMergeKey.yml | 13 ++++++++++++- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/Symfony/Component/Yaml/Parser.php b/src/Symfony/Component/Yaml/Parser.php index fc46ab3caf..53d52a869d 100644 --- a/src/Symfony/Component/Yaml/Parser.php +++ b/src/Symfony/Component/Yaml/Parser.php @@ -113,6 +113,9 @@ class Parser $data[] = $this->parseValue($values['value'], $exceptionOnInvalidType, $objectSupport); } } + if ($isRef) { + $this->refs[$isRef] = end($data); + } } elseif (preg_match('#^(?P'.Inline::REGEX_QUOTED_STRING.'|[^ \'"\[\{].*?) *\:(\s+(?P.+?))?\s*$#u', $this->currentLine, $values) && (false === strpos($values['key'], ' #') || in_array($values['key'][0], array('"', "'")))) { if ($context && 'sequence' == $context) { throw new ParseException('You cannot define a mapping item when in a sequence'); @@ -191,6 +194,9 @@ class Parser $data[$key] = $this->parseValue($values['value'], $exceptionOnInvalidType, $objectSupport); } } + if ($isRef) { + $this->refs[$isRef] = $data[$key]; + } } else { // multiple documents are not supported if ('---' === $this->currentLine) { @@ -248,10 +254,6 @@ class Parser throw new ParseException($error, $this->getRealCurrentLineNb() + 1, $this->currentLine); } - - if ($isRef) { - $this->refs[$isRef] = end($data); - } } if (isset($mbEncoding)) { diff --git a/src/Symfony/Component/Yaml/Tests/Fixtures/sfMergeKey.yml b/src/Symfony/Component/Yaml/Tests/Fixtures/sfMergeKey.yml index 3eec4f877d..541ae6abd2 100644 --- a/src/Symfony/Component/Yaml/Tests/Fixtures/sfMergeKey.yml +++ b/src/Symfony/Component/Yaml/Tests/Fixtures/sfMergeKey.yml @@ -23,5 +23,16 @@ yaml: | isit: tested head: <<: [ *foo , *dong , *foo2 ] + taz: &taz + a: Steve + w: + p: 1234 + nested: + <<: *taz + d: Doug + w: &nestedref + p: 12345 + z: + <<: *nestedref php: | - array('foo' => array('a' => 'Steve', 'b' => 'Clark', 'c' => 'Brian'), 'bar' => array('a' => 'Steve', 'b' => 'Clark', 'c' => 'Brian', 'x' => 'Oren'), 'foo2' => array('a' => 'Ballmer'), 'ding' => array('fi', 'fei', 'fo', 'fam'), 'check' => array('a' => 'Steve', 'b' => 'Clark', 'c' => 'Brian', 'fi', 'fei', 'fo', 'fam', 'isit' => 'tested'), 'head' => array('a' => 'Ballmer', 'b' => 'Clark', 'c' => 'Brian', 'fi', 'fei', 'fo', 'fam')) + array('foo' => array('a' => 'Steve', 'b' => 'Clark', 'c' => 'Brian'), 'bar' => array('a' => 'Steve', 'b' => 'Clark', 'c' => 'Brian', 'x' => 'Oren'), 'foo2' => array('a' => 'Ballmer'), 'ding' => array('fi', 'fei', 'fo', 'fam'), 'check' => array('a' => 'Steve', 'b' => 'Clark', 'c' => 'Brian', 'fi', 'fei', 'fo', 'fam', 'isit' => 'tested'), 'head' => array('a' => 'Ballmer', 'b' => 'Clark', 'c' => 'Brian', 'fi', 'fei', 'fo', 'fam'), 'taz' => array('a' => 'Steve', 'w' => array('p' => 1234)), 'nested' => array('a' => 'Steve', 'w' => array('p' => 12345), 'd' => 'Doug', 'z' => array('p' => 12345)),) From 0692ca97cdfb46e0be1c5996df1a2485bc126b02 Mon Sep 17 00:00:00 2001 From: Veres Lajos Date: Sun, 9 Aug 2015 00:21:10 +0100 Subject: [PATCH 3/3] typofix - https://github.com/vlajos/misspell_fixer --- .../Component/Validator/Tests/Mapping/ClassMetadataTest.php | 2 +- .../Component/Yaml/Tests/Fixtures/YtsDocumentSeparator.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Validator/Tests/Mapping/ClassMetadataTest.php b/src/Symfony/Component/Validator/Tests/Mapping/ClassMetadataTest.php index 45cfba0ad9..87298f8895 100644 --- a/src/Symfony/Component/Validator/Tests/Mapping/ClassMetadataTest.php +++ b/src/Symfony/Component/Validator/Tests/Mapping/ClassMetadataTest.php @@ -113,7 +113,7 @@ class ClassMetadataTest extends \PHPUnit_Framework_TestCase $this->metadata->addPropertyConstraint('firstName', new ConstraintA()); $this->assertTrue($this->metadata->hasMemberMetadatas('firstName')); - $this->assertFalse($this->metadata->hasMemberMetadatas('non_existant_field')); + $this->assertFalse($this->metadata->hasMemberMetadatas('non_existent_field')); } public function testMergeConstraintsKeepsPrivateMembersSeparate() diff --git a/src/Symfony/Component/Yaml/Tests/Fixtures/YtsDocumentSeparator.yml b/src/Symfony/Component/Yaml/Tests/Fixtures/YtsDocumentSeparator.yml index f8501ddc29..d98810256e 100644 --- a/src/Symfony/Component/Yaml/Tests/Fixtures/YtsDocumentSeparator.yml +++ b/src/Symfony/Component/Yaml/Tests/Fixtures/YtsDocumentSeparator.yml @@ -21,7 +21,7 @@ ruby: | test: Leading Document Separator todo: true brief: > - You can explicity give an opening + You can explicitly give an opening document separator to your YAML stream. yaml: | ---