8000 Support "!!binary" syntax (decodes base64 string) by johnnytemp · Pull Request #17599 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Support "!!binary" syntax (decodes base64 string) #17599

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 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions src/Symfony/Component/Yaml/Inline.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
*/
class Inline
{
const REGEX_TAG_PATTERN = '((?P<tag>![\w!.\/:-]+) +)?';
Copy link
Member

Choose a reason for hiding this comment

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

This should be moved to the Parser class.

const REGEX_QUOTED_STRING = '(?:"([^"\\\\]*(?:\\\\.[^"\\\\]*)*)"|\'([^\']*(?:\'\'[^\']*)*)\')';

private static $exceptionOnInvalidType = false;
Expand Down Expand Up @@ -500,6 +501,8 @@ private static function evaluateScalar($scalar, $references = array())
return;
case 0 === strpos($scalar, '!!float '):
return (float) substr($scalar, 8);
case 0 === strncmp($scalar, '!!binary ', 9):
return base64_decode(self::parseScalar(substr($scalar, 9)));
case ctype_digit($scalar):
$raw = $scalar;
$cast = (int) $scalar;
Expand Down
8 changes: 6 additions & 2 deletions src/Symfony/Component/Yaml/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -488,10 +488,14 @@ private function parseValue($value, $exceptionOnInvalidType, $objectSupport, $ob
return $this->refs[$value];
}

if (preg_match('/^'.self::BLOCK_SCALAR_HEADER_PATTERN.'$/', $value, $matches)) {
if (preg_match('/^'.Inline::REGEX_TAG_PATTERN.self::BLOCK_SCALAR_HEADER_PATTERN.'$/', $value, $matches)) {
$modifiers = isset($matches['modifiers']) ? $matches['modifiers'] : '';

return $this->parseBlockScalar($matches['separator'], preg_replace('#\d+#', '', $modifiers), (int) abs($modifiers));
$output = $this->parseBlockScalar($matches['separator'], preg_replace('#\d+#', '', $modifiers), (int) abs($modifiers));
if (isset($matches['tag']) && $matches['tag'] == '!!binary') {
Copy link
Member

Choose a reason for hiding this comment

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

Please use strict comparison (===) here.

Copy link
Contributor

Choose a reason for hiding this comment

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

This condition should be in Yoda stlye.

Copy link
Member

Choose a reason for hiding this comment

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

indeed

$output = base64_decode($output);
}
return $output;
}

try {
Expand Down
3 changes: 3 additions & 0 deletions src/Symfony/Component/Yaml/Tests/InlineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,9 @@ public function getTestsForParseWithMapObjects()
array('-12', -12),
array('"quoted string"', 'quoted string'),
array("'quoted string'", 'quoted string'),
array('!!binary "SGVsbG8sIHdvcmxkLg=="', 'Hello, world.'),
array('!!float 1230', 1230.0),
array('!!unknown "string"', '!!unknown "string"'),
Copy link
Member

Choose a reason for hiding this comment

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

indentation is broken here

array('12.30e+02', 12.30e+02),
array('0x4D2', 0x4D2),
array('02333', 02333),
Expand Down
12 changes: 12 additions & 0 deletions src/Symfony/Component/Yaml/Tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,18 @@ public function testNestedFoldedStringBlockWithComments()
));
}

public function testFoldedBinaryBlock()
Copy link
Member

Choose a reason for hiding this comment

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

testBinaryTag sounds more reasonable to me and I would pass the different YAML strings through a data provider.

{
$this->assertEquals(array(array('name' => 'Hello, world.', 'desc' => 'Hello, world.')), Yaml::parse(<<<'EOF'
-
name: !!binary "SGVsbG8sIHdvcmxkLg=="
desc: !!binary |
SGVsbG8sIH
dvcmxkLg==
EOF
));
}

public function testReferenceResolvingInInlineStrings()
{
$this->assertEquals(array(
Expand Down
0