-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[YAML] Added support for object-maps #10114
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
polyfractal
wants to merge
6
commits into
symfony:2.3
from
polyfractal:features/supportEmptyMapsInYamlParser
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f32705e
[YAML] Added support for object maps
polyfractal 3e8adee
Coding standards to comply with Fabbot
polyfractal 244b50b
Cast to stdClass on return instead of each iteration
polyfractal ed43239
Formatting fixes
polyfractal 53e6992
More formatting
polyfractal e5d2f80
Formatting
polyfractal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
---|---|---|
|
@@ -22,6 +22,23 @@ public function testParse() | |
} | ||
} | ||
|
||
public function testParseWithMapObjects() | ||
{ | ||
foreach ($this->getTestsForMapObjectParse() as $yaml => $value) { | ||
$actual = Inline::parse($yaml, false, false, true); | ||
if (true === is_object($value)) { | ||
$this->assertInstanceOf(get_class($value), $actual); | ||
$this->assertEquals(get_object_vars($value), get_object_vars($actual)); | ||
} elseif (true === is_array($value)) { | ||
$this->assertEquals($value, $actual); | ||
$this->assertMixedArraysSame($value, $actual); | ||
} else { | ||
$this->assertSame($value, $actual); | ||
} | ||
} | ||
|
||
} | ||
|
||
public function testDump() | ||
{ | ||
$testsForDump = $this->getTestsForDump(); | ||
|
@@ -182,6 +199,85 @@ protected function getTestsForParse() | |
); | ||
} | ||
|
||
protected function getTestsForMapObjectParse() | ||
{ | ||
return array( | ||
'' => '', | ||
'null' => null, | ||
'false' => false, | ||
'true' => true, | ||
'12' => 12, | ||
'-12' => -12, | ||
'"quoted string"' => 'quoted string', | ||
"'quoted string'" => 'quoted string', | ||
'12.30e+02' => 12.30e+02, | ||
'0x4D2' => 0x4D2, | ||
'02333' => 02333, | ||
'.Inf' => -log(0), | ||
'-.Inf' => log(0), | ||
"'686e444'" => '686e444', | ||
'686e444' => 646e444, | ||
'123456789123456789123456789123456789' => '123456789123456789123456789123456789', | ||
'"foo\r\nbar"' => "foo\r\nbar", | ||
"'foo#bar'" => 'foo#bar', | ||
"'foo # bar'" => 'foo # bar', | ||
"'#cfcfcf'" => '#cfcfcf', | ||
'::form_base.html.twig' => '::form_base.html.twig', | ||
|
||
'2007-10-30' => mktime(0, 0, 0, 10, 30, 2007), | ||
'2007-10-30T02:59:43Z' => gmmktime(2, 59, 43, 10, 30, 2007), | ||
'2007-10-30 02:59:43 Z' => gmmktime(2, 59, 43, 10, 30, 2007), | ||
'1960-10-30 02:59:43 Z' => gmmktime(2, 59, 43, 10, 30, 1960), | ||
'1730-10-30T02:59:43Z' => gmmktime(2, 59, 43, 10, 30, 1730), | ||
|
||
'"a \\"string\\" with \'quoted strings inside\'"' => 'a "string" with \'quoted strings inside\'', | ||
"'a \"string\" with ''quoted strings inside'''" => 'a "string" with \'quoted strings inside\'', | ||
|
||
// sequences | ||
// urls are no key value mapping. see #3609. Valid yaml "key: value" mappings require a space after the colon | ||
'[foo, http://urls.are/no/mappings, false, null, 12]' => array('foo', 'http://urls.are/no/mappings', false, null, 12), | ||
'[ foo , bar , false , null , 12 ]' => array('foo', 'bar', false, null, 12), | ||
'[\'foo,bar\', \'foo bar\']' => array('foo,bar', 'foo bar'), | ||
|
||
// mappings | ||
'{foo:bar,bar:foo,false:false,null:null,integer:12}' => (object) array('foo' => 'bar', 'bar' => 'foo', 'false' => false, 'null' => null, 'integer' => 12), | ||
'{ foo : bar, bar : foo, false : false, null : null, integer : 12 }' => (object) array('foo' => 'bar', 'bar' => 'foo', 'false' => false, 'null' => null, 'integer' => 12), | ||
'{foo: \'bar\', bar: \'foo: bar\'}' => (object) array('foo' => 'bar', 'bar' => 'foo: bar'), | ||
'{\'foo\': \'bar\', "bar": \'foo: bar\'}' => (object) array('foo' => 'bar', 'bar' => 'foo: bar'), | ||
'{\'foo\'\'\': \'bar\', "bar\"": \'foo: bar\'}' => (object) array('foo\'' => 'bar', "bar\"" => 'foo: bar'), | ||
'{\'foo: \': \'bar\', "bar: ": \'foo: bar\'}' => (object) array('foo: ' => 'bar', "bar: " => 'foo: bar'), | ||
|
||
// nested sequences and mappings | ||
'[foo, [bar, foo]]' => array('foo', array('bar', 'foo')), | ||
'[foo, {bar: foo}]' => array('foo', (object) array('bar' => 'foo')), | ||
'{ foo: {bar: foo} }' => (object) array('foo' => (object) array('bar' => 'foo')), | ||
'{ foo: [bar, foo] }' => (object) array('foo' => array('bar', 'foo')), | ||
|
||
'[ foo, [ bar, foo ] ]' => array('foo', array('bar', 'foo')), | ||
|
||
'[{ foo: {bar: foo} }]' => array((object) array('foo' => (object) array('bar' => 'foo'))), | ||
|
||
'[foo, [bar, [foo, [bar, foo]], foo]]' => array('foo', array('bar', array('foo', array('bar', 'foo')), 'foo')), | ||
|
||
'[foo, {bar: foo, foo: [foo, {bar: foo}]}, [foo, {bar: foo}]]' => array('foo', (object) array('bar' => 'foo', 'foo' => array('foo', (object) array('bar' => 'foo'))), array('foo', (object) array('bar' => 'foo'))), | ||
|
||
'[foo, bar: { foo: bar }]' => array('foo', '1' => (object) array('bar' => (object) array('foo' => 'bar'))), | ||
'[foo, \'@foo.baz\', { \'%foo%\': \'foo is %foo%\', bar: \'%foo%\' }, true, \'@service_container\']' => array('foo', '@foo.baz', (object) array('%foo%' => 'foo is %foo%', 'bar' => '%foo%',), true, '@service_container',), | ||
|
||
'{}' => new \stdClass(), | ||
'{ foo : bar, bar : {} }' => (object) array('foo' => 'bar', 'bar' => new \stdClass()), | ||
'{ foo : [], bar : {} }' => (object) array('foo' => array(), 'bar' => new \stdClass()), | ||
'{foo: \'bar\', bar: {} }' => (object) array('foo' => 'bar', 'bar' => new \stdClass()), | ||
'{\'foo\': \'bar\', "bar": {}}' => (object) array('foo' => 'bar', 'bar' => new \stdClass()), | ||
'{\'foo\': \'bar\', "bar": \'{}\'}' => (object) array('foo' => 'bar', 'bar' => '{}'), | ||
|
||
'[foo, [{}, {}]]' => array('foo', array(new \stdClass(), new \stdClass())), | ||
'[foo, [[], {}]]' => array('foo', array(array(), new \stdClass())), | ||
'[foo, [[{}, {}], {}]]' => array('foo', array(array(new \stdClass(), new \stdClass()), new \stdClass())), | ||
'[foo, {bar: {}}]' => array('foo', '1' => (object) array('bar' => new \stdClass())), | ||
); | ||
} | ||
|
||
protected function getTestsForDump() | ||
{ | ||
return array( | ||
|
@@ -229,4 +325,27 @@ protected function getTestsForDump() | |
'[foo, \'@foo.baz\', { \'%foo%\': \'foo is %foo%\', bar: \'%foo%\' }, true, \'@service_container\']' => array('foo', '@foo.baz', array('%foo%' => 'foo is %foo%', 'bar' => '%foo%',), true, '@service_container',), | ||
); | ||
} | ||
|
||
protected function assertMixedArraysSame($a, $b) | ||
{ | ||
|
||
foreach ($a as $key => $value) { | ||
if (array_key_exists($key, $b)) { | ||
if (is_array($value)) { | ||
$this->assertMixedArraysSame($value, $b[$key]); | ||
} else { | ||
if (true === is_object($value)) { | ||
$this->assertEquals($value, $b[$key]); | ||
$this->assertInstanceOf(get_class($value), $b[$key]); | ||
$this->assertEquals(get_object_vars($value), get_object_vars($b[$key])); | ||
} else { | ||
$this->assertSame($value, $b[$key]); | ||
} | ||
} | ||
} else { | ||
$this->assertFail(); | ||
} | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Argh, yep. Lemme look through the whole PR and see if I've done this anywhere else :) |
||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The j variable is a bit useless here, why don't you use 0 as argument?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately, it is required. :(
parseMapping()
requires the second parameter to be passed by reference:PHP disallows passing constants by reference because the method may mutate the value (and indeed it does). So you must create a temporary variable and pass that instead. PHP doesn't mind if you omit the parameter because of the default on the function, but if you specify arguments that come after the referenced parameter, you must supply one.
I could do the variable assignment inline, but it's effectively the same thing:
Open to alternatives, but I did it this way because alternative approaches (removing the reference) would require fairly drastic restructuring of how the parsing works for what is ultimately just a little semantic blemish :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sorry, missed that. Sad it has to be done this way :(