4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Serializer\Encoder;
14 use Symfony\Component\Yaml\Dumper;
15 use Symfony\Component\Yaml\Parser;
20 * @author Kévin Dunglas <dunglas@gmail.com>
22 class YamlEncoder implements EncoderInterface, DecoderInterface
24 const FORMAT = 'yaml';
28 private $defaultContext = array('yaml_inline' => 0, 'yaml_indent' => 0, 'yaml_flags' => 0);
30 public function __construct(Dumper $dumper = null, Parser $parser = null, array $defaultContext = array())
32 $this->dumper = $dumper ?: new Dumper();
33 $this->parser = $parser ?: new Parser();
34 $this->defaultContext = array_merge($this->defaultContext, $defaultContext);
40 public function encode($data, $format, array $context = array())
42 $context = array_merge($this->defaultContext, $context);
44 return $this->dumper->dump($data, $context['yaml_inline'], $context['yaml_indent'], $context['yaml_flags']);
50 public function supportsEncoding($format)
52 return self::FORMAT === $format;
58 public function decode($data, $format, array $context = array())
60 $context = array_merge($this->defaultContext, $context);
62 return $this->parser->parse($data, $context['yaml_flags']);
68 public function supportsDecoding($format)
70 return self::FORMAT === $format;