1 <?php
2 /**
3 * Part of the Joomla Framework Registry Package
4 *
5 * @copyright Copyright (C) 2005 - 2016 Open Source Matters, Inc. All rights reserved.
6 * @license GNU General Public License version 2 or later; see LICENSE
7 */
8
9 namespace Joomla\Registry\Format;
10
11 use Joomla\Registry\AbstractRegistryFormat;
12 use Symfony\Component\Yaml\Parser as SymfonyYamlParser;
13 use Symfony\Component\Yaml\Dumper as SymfonyYamlDumper;
14
15 /**
16 * YAML format handler for Registry.
17 *
18 * @since 1.0
19 */
20 class Yaml extends AbstractRegistryFormat
21 {
22 /**
23 * The YAML parser class.
24 *
25 * @var \Symfony\Component\Yaml\Parser
26 * @since 1.0
27 */
28 private $parser;
29
30 /**
31 * The YAML dumper class.
32 *
33 * @var \Symfony\Component\Yaml\Dumper
34 * @since 1.0
35 */
36 private $dumper;
37
38 /**
39 * Construct to set up the parser and dumper
40 *
41 * @since 1.0
42 */
43 public function __construct()
44 {
45 $this->parser = new SymfonyYamlParser;
46 $this->dumper = new SymfonyYamlDumper;
47 }
48
49 /**
50 * Converts an object into a YAML formatted string.
51 * We use json_* to convert the passed object to an array.
52 *
53 * @param object $object Data source object.
54 * @param array $options Options used by the formatter.
55 *
56 * @return string YAML formatted string.
57 *
58 * @since 1.0
59 */
60 public function objectToString($object, $options = array())
61 {
62 $array = json_decode(json_encode($object), true);
63
64 return $this->dumper->dump($array, 2, 0);
65 }
66
67 /**
68 * Parse a YAML formatted string and convert it into an object.
69 * We use the json_* methods to convert the parsed YAML array to an object.
70 *
71 * @param string $data YAML formatted string to convert.
72 * @param array $options Options used by the formatter.
73 *
74 * @return object Data object.
75 *
76 * @since 1.0
77 */
78 public function stringToObject($data, array $options = array())
79 {
80 $array = $this->parser->parse(trim($data));
81
82 return json_decode(json_encode($array));
83 }
84 }
85