@@ -95,16 +95,20 @@ acts as a thin wrapper that simplifies common uses.
95
95
Reading YAML Files
96
96
~~~~~~~~~~~~~~~~~~
97
97
98
- The :method: `Symfony\\ Component\\ Yaml\\ Parser ::parse ` method parses a YAML
98
+ The :method: `Symfony\\ Component\\ Yaml\\ Yaml ::parse ` method parses a YAML
99
99
string and converts it to a PHP array:
100
100
101
101
.. code-block :: php
102
102
103
- use Symfony\Component\Yaml\Parser;
103
+ use Symfony\Component\Yaml\Yaml;
104
+
105
+ $value = Yaml::parse(file_get_contents('/path/to/file.yml'));
104
106
105
- $yaml = new Parser();
107
+ .. caution ::
106
108
107
- $value = $yaml->parse(file_get_contents('/path/to/file.yml'));
109
+ Because it is currently possible to pass a filename to this method, you
110
+ must validate the input first. Passing a filename is deprecated in
111
+ Symfony 2.2, and was removed in Symfony 3.0.
108
112
109
113
If an error occurs during parsing, the parser throws a
110
114
:class: `Symfony\\ Component\\ Yaml\\ Exception\\ ParseException ` exception
@@ -116,64 +120,49 @@ error occurred:
116
120
use Symfony\Component\Yaml\Exception\ParseException;
117
121
118
122
try {
119
- $value = $yaml-> parse(file_get_contents('/path/to/file.yml'));
123
+ $value = Yaml:: parse(file_get_contents('/path/to/file.yml'));
120
124
} catch (ParseException $e) {
121
125
printf("Unable to parse the YAML string: %s", $e->getMessage());
122
126
}
123
127
124
- .. tip ::
125
-
126
- As the parser is re-entrant, you can use the same parser object to load
127
- different YAML strings.
128
-
129
- It may also be convenient to use the
130
- :method: `Symfony\\ Component\\ Yaml\\ Yaml::parse ` wrapper method:
128
+ .. _components-yaml-dump :
131
129
132
- .. code-block :: php
130
+ Objects for Mappings
131
+ ....................
133
132
134
- use Symfony\Component\Yaml\Yaml;
133
+ .. versionadded :: 2.7
134
+ Support for parsing mappings as objects was introduced in Symfony 2.7.
135
135
136
- $yaml = Yaml::parse(file_get_contents('/path/to/file.yml'));
136
+ Yaml :ref: `mappings <yaml-format-collections >` are basically associative
137
+ arrays. You can instruct the parser to return mappings as objects (i.e.
138
+ ``\stdClass `` instances) by setting the fourth argument to ``true ``::
137
139
138
- The :method: `Symfony\\ Component\\ Yaml\\ Yaml::parse ` static method takes a YAML string.
139
- Internally, it constructs a :class: `Symfony\\ Component\\ Yaml\\ Parser ` object and calls
140
- the :method: `Symfony\\ Component\\ Yaml\\ Parser::parse ` method.
141
-
142
- .. _components-yaml-dump :
140
+ $object = Yaml::parse('{"foo": "bar"}', false, false, true);
141
+ echo get_class($object); // stdClass
142
+ echo $object->foo; // bar
143
143
144
144
Writing YAML Files
145
145
~~~~~~~~~~~~~~~~~~
146
146
147
- The :method: `Symfony\\ Component\\ Yaml\\ Dumper ::dump ` method dumps any PHP
147
+ The :method: `Symfony\\ Component\\ Yaml\\ Yaml ::dump ` method dumps any PHP
148
148
array to its YAML representation:
149
149
150
150
.. code-block :: php
151
151
152
- use Symfony\Component\Yaml\Dumper ;
152
+ use Symfony\Component\Yaml\Yaml ;
153
153
154
154
$array = array(
155
155
'foo' => 'bar',
156
156
'bar' => array('foo' => 'bar', 'bar' => 'baz'),
157
157
);
158
158
159
- $dumper = new Dumper();
160
-
161
- $yaml = $dumper->dump($array);
159
+ $yaml = Yaml::dump($array);
162
160
163
161
file_put_contents('/path/to/file.yml', $yaml);
164
162
165
163
If an error occurs during the dump, the parser throws a
166
164
:class: `Symfony\\ Component\\ Yaml\\ Exception\\ DumpException ` exception.
167
165
168
- If you only need to dump one array, you can use the
169
- :method: `Symfony\\ Component\\ Yaml\\ Yaml::dump ` static method shortcut:
170
-
171
- .. code-block :: php
172
-
173
- use Symfony\Component\Yaml\Yaml;
174
-
175
- $yaml = Yaml::dump($array);
176
-
177
166
Array Expansion and Inlining
178
167
............................
179
168
@@ -185,7 +174,7 @@ representation:
185
174
186
175
{ foo: bar, bar: { foo: bar, bar: baz } }
187
176
188
- The second argument of the :method: `Symfony\\ Component\\ Yaml\\ Dumper ::dump `
177
+ The second argument of the :method: `Symfony\\ Component\\ Yaml\\ Yaml ::dump `
189
178
method customizes the level at which the output switches from the expanded
190
179
representation to the inline one:
191
180
0 commit comments