Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the Symfony package.
|
Chris@0
|
5 *
|
Chris@0
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Symfony\Component\Yaml;
|
Chris@0
|
13
|
Chris@0
|
14 use Symfony\Component\Yaml\Exception\ParseException;
|
Chris@0
|
15
|
Chris@0
|
16 /**
|
Chris@0
|
17 * Yaml offers convenience methods to load and dump YAML.
|
Chris@0
|
18 *
|
Chris@0
|
19 * @author Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
20 */
|
Chris@0
|
21 class Yaml
|
Chris@0
|
22 {
|
Chris@0
|
23 const DUMP_OBJECT = 1;
|
Chris@0
|
24 const PARSE_EXCEPTION_ON_INVALID_TYPE = 2;
|
Chris@0
|
25 const PARSE_OBJECT = 4;
|
Chris@0
|
26 const PARSE_OBJECT_FOR_MAP = 8;
|
Chris@0
|
27 const DUMP_EXCEPTION_ON_INVALID_TYPE = 16;
|
Chris@0
|
28 const PARSE_DATETIME = 32;
|
Chris@0
|
29 const DUMP_OBJECT_AS_MAP = 64;
|
Chris@0
|
30 const DUMP_MULTI_LINE_LITERAL_BLOCK = 128;
|
Chris@0
|
31 const PARSE_CONSTANT = 256;
|
Chris@0
|
32
|
Chris@0
|
33 /**
|
Chris@0
|
34 * Parses YAML into a PHP value.
|
Chris@0
|
35 *
|
Chris@0
|
36 * Usage:
|
Chris@0
|
37 * <code>
|
Chris@0
|
38 * $array = Yaml::parse(file_get_contents('config.yml'));
|
Chris@0
|
39 * print_r($array);
|
Chris@0
|
40 * </code>
|
Chris@0
|
41 *
|
Chris@0
|
42 * @param string $input A string containing YAML
|
Chris@0
|
43 * @param int $flags A bit field of PARSE_* constants to customize the YAML parser behavior
|
Chris@0
|
44 *
|
Chris@0
|
45 * @return mixed The YAML converted to a PHP value
|
Chris@0
|
46 *
|
Chris@0
|
47 * @throws ParseException If the YAML is not valid
|
Chris@0
|
48 */
|
Chris@0
|
49 public static function parse($input, $flags = 0)
|
Chris@0
|
50 {
|
Chris@0
|
51 if (is_bool($flags)) {
|
Chris@0
|
52 @trigger_error('Passing a boolean flag to toggle exception handling is deprecated since version 3.1 and will be removed in 4.0. Use the PARSE_EXCEPTION_ON_INVALID_TYPE flag instead.', E_USER_DEPRECATED);
|
Chris@0
|
53
|
Chris@0
|
54 if ($flags) {
|
Chris@0
|
55 $flags = self::PARSE_EXCEPTION_ON_INVALID_TYPE;
|
Chris@0
|
56 } else {
|
Chris@0
|
57 $flags = 0;
|
Chris@0
|
58 }
|
Chris@0
|
59 }
|
Chris@0
|
60
|
Chris@0
|
61 if (func_num_args() >= 3) {
|
Chris@0
|
62 @trigger_error('Passing a boolean flag to toggle object support is deprecated since version 3.1 and will be removed in 4.0. Use the PARSE_OBJECT flag instead.', E_USER_DEPRECATED);
|
Chris@0
|
63
|
Chris@0
|
64 if (func_get_arg(2)) {
|
Chris@0
|
65 $flags |= self::PARSE_OBJECT;
|
Chris@0
|
66 }
|
Chris@0
|
67 }
|
Chris@0
|
68
|
Chris@0
|
69 if (func_num_args() >= 4) {
|
Chris@0
|
70 @trigger_error('Passing a boolean flag to toggle object for map support is deprecated since version 3.1 and will be removed in 4.0. Use the Yaml::PARSE_OBJECT_FOR_MAP flag instead.', E_USER_DEPRECATED);
|
Chris@0
|
71
|
Chris@0
|
72 if (func_get_arg(3)) {
|
Chris@0
|
73 $flags |= self::PARSE_OBJECT_FOR_MAP;
|
Chris@0
|
74 }
|
Chris@0
|
75 }
|
Chris@0
|
76
|
Chris@0
|
77 $yaml = new Parser();
|
Chris@0
|
78
|
Chris@0
|
79 return $yaml->parse($input, $flags);
|
Chris@0
|
80 }
|
Chris@0
|
81
|
Chris@0
|
82 /**
|
Chris@0
|
83 * Dumps a PHP value to a YAML string.
|
Chris@0
|
84 *
|
Chris@0
|
85 * The dump method, when supplied with an array, will do its best
|
Chris@0
|
86 * to convert the array into friendly YAML.
|
Chris@0
|
87 *
|
Chris@0
|
88 * @param mixed $input The PHP value
|
Chris@0
|
89 * @param int $inline The level where you switch to inline YAML
|
Chris@0
|
90 * @param int $indent The amount of spaces to use for indentation of nested nodes
|
Chris@0
|
91 * @param int $flags A bit field of DUMP_* constants to customize the dumped YAML string
|
Chris@0
|
92 *
|
Chris@0
|
93 * @return string A YAML string representing the original PHP value
|
Chris@0
|
94 */
|
Chris@0
|
95 public static function dump($input, $inline = 2, $indent = 4, $flags = 0)
|
Chris@0
|
96 {
|
Chris@0
|
97 if (is_bool($flags)) {
|
Chris@0
|
98 @trigger_error('Passing a boolean flag to toggle exception handling is deprecated since version 3.1 and will be removed in 4.0. Use the DUMP_EXCEPTION_ON_INVALID_TYPE flag instead.', E_USER_DEPRECATED);
|
Chris@0
|
99
|
Chris@0
|
100 if ($flags) {
|
Chris@0
|
101 $flags = self::DUMP_EXCEPTION_ON_INVALID_TYPE;
|
Chris@0
|
102 } else {
|
Chris@0
|
103 $flags = 0;
|
Chris@0
|
104 }
|
Chris@0
|
105 }
|
Chris@0
|
106
|
Chris@0
|
107 if (func_num_args() >= 5) {
|
Chris@0
|
108 @trigger_error('Passing a boolean flag to toggle object support is deprecated since version 3.1 and will be removed in 4.0. Use the DUMP_OBJECT flag instead.', E_USER_DEPRECATED);
|
Chris@0
|
109
|
Chris@0
|
110 if (func_get_arg(4)) {
|
Chris@0
|
111 $flags |= self::DUMP_OBJECT;
|
Chris@0
|
112 }
|
Chris@0
|
113 }
|
Chris@0
|
114
|
Chris@0
|
115 $yaml = new Dumper($indent);
|
Chris@0
|
116
|
Chris@0
|
117 return $yaml->dump($input, $inline, 0, $flags);
|
Chris@0
|
118 }
|
Chris@0
|
119 }
|