Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 424002b

Browse filesBrowse files
committed
[Serializer] Add a YAML encoder
1 parent 473263a commit 424002b
Copy full SHA for 424002b

File tree

2 files changed

+141
-0
lines changed
Filter options

2 files changed

+141
-0
lines changed
+73Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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\Encoder;
13+
14+
use Symfony\Component\Yaml\Dumper;
15+
use Symfony\Component\Yaml\Parser;
16+
use Symfony\Component\Yaml\Yaml;
17+
18+
/**
19+
* Encodes YAML data.
20+
*
21+
* @author Kévin Dunglas <dunglas@gmail.com>
22+
*/
23+
class YamlEncoder implements EncoderInterface, DecoderInterface
24+
{
25+
const FORMAT = 'yaml';
26+
27+
private $dumper;
28+
private $parser;
29+
private $defaultContext;
30+
31+
public function __construct(Dumper $dumper = null, Parser $parser = null, $defaultContext = array('yaml_inline' => 0, 'yaml_indent' => 0, 'yaml_flags' => 0))
32+
{
33+
$this->dumper = $dumper ?: new Dumper();
34+
$this->parser = $parser ?: new Parser();
35+
$this->defaultContext = $defaultContext;
36+
}
37+
38+
/**
39+
* {@inheritdoc}
40+
*/
41+
public function encode($data, $format, array $context = array())
42+
{
43+
$context = array_merge($this->defaultContext, $context);
44+
45+
return $this->dumper->dump($data, $context['yaml_inline'], $context['yaml_indent'], $context['yaml_flags']);
46+
}
47+
48+
/**
49+
* {@inheritdoc}
50+
*/
51+
public function supportsEncoding($format)
52+
{
53+
return self::FORMAT === $format;
54+
}
55+
56+
/**
57+
* {@inheritdoc}
58+
*/
59+
public function decode($data, $format, array $context = array())
60+
{
61+
$context = array_merge($this->defaultContext, $context);
62+
63+
return Yaml::parse($data, $context['yaml_flags']);
64+
}
65+
66+
/**
67+
* {@inheritdoc}
68+
*/
69+
public function supportsDecoding($format)
70+
{
71+
return self::FORMAT === $format;
72+
}
73+
}
+68Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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\Encoder;
13+
14+
use Symfony\Component\Serializer\Encoder\YamlEncoder;
15+
use Symfony\Component\Yaml\Dumper;
16+
use Symfony\Component\Yaml\Parser;
17+
use Symfony\Component\Yaml\Yaml;
18+
19+
/**
20+
* @author Kévin Dunglas <dunglas@gmail.com>
21+
*/
22+
class YamlEncoderTest extends \PHPUnit_Framework_TestCase
23+
{
24+
public function testEncode()
25+
{
26+
$encoder = new YamlEncoder();
27+
28+
$this->assertEquals('foo', $encoder->encode('foo', 'yaml'));
29+
$this->assertEquals('{ foo: 1 }', $encoder->encode(array('foo' => 1), 'yaml'));
30+
}
31+
32+
public function testSupportsEncoding()
33+
{
34+
$encoder = new YamlEncoder();
35+
36+
$this->assertTrue($encoder->supportsEncoding('yaml'));
37+
$this->assertFalse($encoder->supportsEncoding('json'));
38+
}
39+
40+
public function testDecode()
41+
{
42+
$encoder = new YamlEncoder();
43+
44+
$this->assertEquals('foo', $encoder->decode('foo', 'yaml'));
45+
$this->assertEquals(array('foo' => 1), $encoder->decode('{ foo: 1 }', 'yaml'));
46+
}
47+
48+
public function testSupportsDecoding()
49+
{
50+
$encoder = new YamlEncoder();
51+
52+
$this->assertTrue($encoder->supportsDecoding('yaml'));
53+
$this->assertFalse($encoder->supportsDecoding('json'));
54+
}
55+
56+
public function testContext()
57+
{
58+
$encoder = new YamlEncoder(new Dumper(), new Parser(), array('yaml_inline' => 1, 'yaml_indent' => 4, 'yaml_flags' => Yaml::DUMP_OBJECT | Yaml::PARSE_OBJECT));
59+
60+
$obj = new \stdClass();
61+
$obj->bar = 2;
62+
63+
$this->assertEquals(" foo: !php/object:O:8:\"stdClass\":1:{s:3:\"bar\";i:2;}\n", $encoder->encode(array('foo' => $obj), 'yaml'));
64+
$this->assertEquals(' { foo: null }', $encoder->encode(array('foo' => $obj), 'yaml', array('yaml_inline' => 0, 'yaml_indent' => 2, 'yaml_flags' => 0)));
65+
$this->assertEquals(array('foo' => $obj), $encoder->decode('foo: !php/object:O:8:"stdClass":1:{s:3:"bar";i:2;}', 'yaml'));
66+
$this->assertEquals(array('foo' => null), $encoder->decode('foo: !php/object:O:8:"stdClass":1:{s:3:"bar";i:2;}', 'yaml', array('yaml_flags' => 0)));
67+
}
68+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.