1: <?php
2:
3: namespace Mapbender\ManagerBundle\Form\DataTransformer;
4:
5: use Symfony\Component\Form\DataTransformerInterface;
6: use Symfony\Component\Form\Exception\TransformationFailedException;
7: use Symfony\Component\Yaml\Parser;
8: use Symfony\Component\Yaml\Dumper;
9: use Symfony\Component\Yaml\Exception\DumpException;
10:
11: /**
12: * YAML <-> Array data transformer
13: *
14: * @author Christian Wygoda
15: */
16: class YAMLDataTransformer implements DataTransformerInterface
17: {
18: /**
19: * Transforms array to YAML
20: *
21: * @param array $array
22: * @return string
23: */
24: public function transform($array)
25: {
26: $dumper = new Dumper();
27:
28: try {
29: $yaml = $dumper->dump($array, 2);
30: } catch(DumpException $e) {
31: throw new TransformationFailedException();
32: }
33:
34: return $yaml;
35: }
36:
37: /**
38: * Transforms YAML to array
39: *
40: * @param string $yaml
41: * @return array
42: */
43: public function reverseTransform($yaml)
44: {
45: $parser = new Parser();
46:
47: try {
48: $array = $parser->parse($yaml);
49: } catch(ParseException $e) {
50: throw new TransformationFailedException();
51: }
52:
53: return $array;
54: }
55: }
56:
57: