-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Serializer][FrameworkBundle] Add a YAML encoder #19326
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
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
424002b
[Serializer] Add a YAML encoder
dunglas 2ecebea
[FrameworkBundle] Register the YAML encoder in FrameworkBundle
dunglas f46a498
Require YAML >=3.1
dunglas 8032750
Check that the YAML component is installed
dunglas 4cecb35
Add missing typehint
dunglas 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 |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Serializer\Encoder; | ||
|
||
use Symfony\Component\Yaml\Dumper; | ||
use Symfony\Component\Yaml\Parser; | ||
use Symfony\Component\Yaml\Yaml; | ||
|
||
/** | ||
* Encodes YAML data. | ||
* | ||
* @author Kévin Dunglas <dunglas@gmail.com> | ||
*/ | ||
class YamlEncoder implements EncoderInterface, DecoderInterface | ||
{ | ||
const FORMAT = 'yaml'; | ||
|
||
private $dumper; | ||
private $parser; | ||
private $defaultContext = array('yaml_inline' => 0, 'yaml_indent' => 0, 'yaml_flags' => 0); | ||
|
||
public function __construct(Dumper $dumper = null, Parser $parser = null, array $defaultContext = array()) | ||
{ | ||
$this->dumper = $dumper ?: new Dumper(); | ||
$this->parser = $parser ?: new Parser(); | ||
$this->defaultContext = array_merge($this->defaultContext, $defaultContext); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function encode($data, $format, array $context = array()) | ||
{ | ||
$context = array_merge($this->defaultContext, $context); | ||
|
||
return $this->dumper->dump($data, $context['yaml_inline'], $context['yaml_indent'], $context['yaml_flags']); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supportsEncoding($format) | ||
{ | ||
return self::FORMAT === $format; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function decode($data, $format, array $context = array()) | ||
{ | ||
$context = array_merge($this->defaultContext, $context); | ||
|
||
return Yaml::parse($data, $context['yaml_flags']); | ||
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. Shouldn't you use the injected parser instead of performing a static call? 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. I agree (see #20006). |
||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supportsDecoding($format) | ||
{ | ||
return self::FORMAT === $format; | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
src/Symfony/Component/Serializer/Tests/Encoder/YamlEncoderTest.php
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 |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Serializer\Tests\Encoder; | ||
|
||
use Symfony\Component\Serializer\Encoder\YamlEncoder; | ||
use Symfony\Component\Yaml\Dumper; | ||
use Symfony\Component\Yaml\Parser; | ||
use Symfony\Component\Yaml\Yaml; | ||
|
||
/** | ||
* @author Kévin Dunglas <dunglas@gmail.com> | ||
*/ | ||
class YamlEncoderTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testEncode() | ||
{ | ||
$encoder = new YamlEncoder(); | ||
|
||
$this->assertEquals('foo', $encoder->encode('foo', 'yaml')); | ||
$this->assertEquals('{ foo: 1 }', $encoder->encode(array('foo' => 1), 'yaml')); | ||
} | ||
|
||
public function testSupportsEncoding() | ||
{ | ||
$encoder = new YamlEncoder(); | ||
|
||
$this->assertTrue($encoder->supportsEncoding('yaml')); | ||
$this->assertFalse($encoder->supportsEncoding('json')); | ||
} | ||
|
||
public function testDecode() | ||
{ | ||
$encoder = new YamlEncoder(); | ||
|
||
$this->assertEquals('foo', $encoder->decode('foo', 'yaml')); | ||
$this->assertEquals(array('foo' => 1), $encoder->decode('{ foo: 1 }', 'yaml')); | ||
} | ||
|
||
public function testSupportsDecoding() | ||
{ | ||
$encoder = new YamlEncoder(); | ||
|
||
$this->assertTrue($encoder->supportsDecoding('yaml')); | ||
$this->assertFalse($encoder->supportsDecoding('json')); | ||
} | ||
|
||
public function testContext() | ||
{ | ||
$encoder = new YamlEncoder(new Dumper(), new Parser(), array('yaml_inline' => 1, 'yaml_indent' => 4, 'yaml_flags' => Yaml::DUMP_OBJECT | Yaml::PARSE_OBJECT)); | ||
|
||
$obj = new \stdClass(); | ||
$obj->bar = 2; | ||
|
||
$this->assertEquals(" foo: !php/object:O:8:\"stdClass\":1:{s:3:\"bar\";i:2;}\n", $encoder->encode(array('foo' => $obj), 'yaml')); | ||
$this->assertEquals(' { foo: null }', $encoder->encode(array('foo' => $obj), 'yaml', array('yaml_inline' => 0, 'yaml_indent' => 2, 'yaml_flags' => 0))); | ||
$this->assertEquals(array('foo' => $obj), $encoder->decode('foo: !php/object:O:8:"stdClass":1:{s:3:"bar";i:2;}', 'yaml')); | ||
$this->assertEquals(array('foo' => null), $encoder->decode('foo: !php/object:O:8:"stdClass":1:{s:3:"bar";i:2;}', 'yaml', array('yaml_flags' => 0))); | ||
} | ||
} |
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
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.
Here you do Encode and Decode both process inside this class. So you class name should be change like
YamlDataConverter
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.
@bhavin-nakrani check https://github.com/symfony/symfony/tree/master/src/Symfony/Component/Serializer/Encoder. All existing "encoders" follow this convention. It should be the same here for consistency.