Fixed some doxygen warnings
[lhc/web/wiklou.git] / includes / api / ApiFormatYaml_spyc.php
1 <?php
2 /**
3 * Spyc -- A Simple PHP YAML Class
4 * @version 0.2.3 -- 2006-02-04
5 * @author Chris Wanstrath <chris@ozmm.org>
6 * @see http://spyc.sourceforge.net/
7 * @copyright Copyright 2005-2006 Chris Wanstrath
8 * @license http://www.opensource.org/licenses/mit-license.php MIT License
9 */
10
11 /**
12 * The Simple PHP YAML Class.
13 *
14 * This class can be used to read a YAML file and convert its contents
15 * into a PHP array. It currently supports a very limited subsection of
16 * the YAML spec.
17 *
18 * @ingroup API
19 */
20 class Spyc {
21
22 /**
23 * Dump YAML from PHP array statically
24 *
25 * The dump method, when supplied with an array, will do its best
26 * to convert the array into friendly YAML. Pretty simple. Feel free to
27 * save the returned string as nothing.yml and pass it around.
28 *
29 * Oh, and you can decide how big the indent is and what the wordwrap
30 * for folding is. Pretty cool -- just pass in 'false' for either if
31 * you want to use the default.
32 *
33 * Indent's default is 2 spaces, wordwrap's default is 40 characters. And
34 * you can turn off wordwrap by passing in 0.
35 *
36 * @param $array Array: PHP array
37 * @param $indent Integer: Pass in false to use the default, which is 2
38 * @param $wordwrap Integer: Pass in 0 for no wordwrap, false for default (40)
39 * @return String
40 */
41 public static function YAMLDump( $array, $indent = false, $wordwrap = false ) {
42 $spyc = new Spyc;
43 return $spyc->dump( $array, $indent, $wordwrap );
44 }
45
46 /**
47 * Dump PHP array to YAML
48 *
49 * The dump method, when supplied with an array, will do its best
50 * to convert the array into friendly YAML. Pretty simple. Feel free to
51 * save the returned string as tasteful.yml and pass it around.
52 *
53 * Oh, and you can decide how big the indent is and what the wordwrap
54 * for folding is. Pretty cool -- just pass in 'false' for either if
55 * you want to use the default.
56 *
57 * Indent's default is 2 spaces, wordwrap's default is 40 characters. And
58 * you can turn off wordwrap by passing in 0.
59 *
60 * @param $array Array: PHP array
61 * @param $indent Integer: Pass in false to use the default, which is 2
62 * @param $wordwrap Integer: Pass in 0 for no wordwrap, false for default (40)
63 * @return String
64 */
65 public function dump( $array, $indent = false, $wordwrap = false ) {
66 // Dumps to some very clean YAML. We'll have to add some more features
67 // and options soon. And better support for folding.
68
69 // New features and options.
70 if ( $indent === false or !is_numeric( $indent ) ) {
71 $this->_dumpIndent = 2;
72 } else {
73 $this->_dumpIndent = $indent;
74 }
75
76 if ( $wordwrap === false or !is_numeric( $wordwrap ) ) {
77 $this->_dumpWordWrap = 40;
78 } else {
79 $this->_dumpWordWrap = $wordwrap;
80 }
81
82 // New YAML document
83 $string = "---\n";
84
85 // Start at the base of the array and move through it.
86 foreach ( $array as $key => $value ) {
87 $string .= $this->_yamlize( $key, $value, 0 );
88 }
89 return $string;
90 }
91
92 /**** Private Properties ****/
93
94 private $_haveRefs;
95 private $_allNodes;
96 private $_lastIndent;
97 private $_lastNode;
98 private $_inBlock;
99 private $_isInline;
100 private $_dumpIndent;
101 private $_dumpWordWrap;
102
103 /**** Private Methods ****/
104
105 /**
106 * Attempts to convert a key / value array item to YAML
107 *
108 * @param $key Mixed: the name of the key
109 * @param $value Mixed: the value of the item
110 * @param $indent Integer: the indent of the current node
111 * @return String
112 */
113 private function _yamlize( $key, $value, $indent ) {
114 if ( is_array( $value ) ) {
115 // It has children. What to do?
116 // Make it the right kind of item
117 $string = $this->_dumpNode( $key, null, $indent );
118 // Add the indent
119 $indent += $this->_dumpIndent;
120 // Yamlize the array
121 $string .= $this->_yamlizeArray( $value, $indent );
122 } elseif ( !is_array( $value ) ) {
123 // It doesn't have children. Yip.
124 $string = $this->_dumpNode( $key, $value, $indent );
125 }
126 return $string;
127 }
128
129 /**
130 * Attempts to convert an array to YAML
131 *
132 * @param $array Array: the array you want to convert
133 * @param $indent Integer: the indent of the current level
134 * @return String
135 */
136 private function _yamlizeArray( $array, $indent ) {
137 if ( is_array( $array ) ) {
138 $string = '';
139 foreach ( $array as $key => $value ) {
140 $string .= $this->_yamlize( $key, $value, $indent );
141 }
142 return $string;
143 } else {
144 return false;
145 }
146 }
147
148 /**
149 * Find out whether a string needs to be output as a literal rather than in plain style.
150 * Added by Roan Kattouw 13-03-2008
151 *
152 * @param $value String: the string to check
153 * @return Boolean
154 */
155 function _needLiteral( $value ) {
156 // Check whether the string contains # or : or begins with any of:
157 // [ - ? , [ ] { } ! * & | > ' " % @ ` ]
158 // or is a number or contains newlines
159 return (bool)( gettype( $value ) == "string" &&
160 ( is_numeric( $value ) ||
161 strpos( $value, "\n" ) ||
162 preg_match( "/[#:]/", $value ) ||
163 preg_match( "/^[-?,[\]{}!*&|>'\"%@`]/", $value ) ) );
164 }
165
166 /**
167 * Returns YAML from a key and a value
168 *
169 * @param $key Mixed: the name of the key
170 * @param $value Mixed: the value of the item
171 * @param $indent Integer: the indent of the current node
172 * @return String
173 */
174 private function _dumpNode( $key, $value, $indent ) {
175 // do some folding here, for blocks
176 if ( $this->_needLiteral( $value ) ) {
177 $value = $this->_doLiteralBlock( $value, $indent );
178 } else {
179 $value = $this->_doFolding( $value, $indent );
180 }
181
182 $spaces = str_repeat( ' ', $indent );
183
184 if ( is_int( $key ) ) {
185 // It's a sequence
186 if ( $value !== '' && !is_null( $value ) )
187 $string = $spaces . '- ' . $value . "\n";
188 else
189 $string = $spaces . "-\n";
190 } else {
191 if ( $key == '*' ) // bug 21922 - Quote asterix used as keys
192 $key = "'*'";
193
194 // It's mapped
195 if ( $value !== '' && !is_null( $value ) )
196 $string = $spaces . $key . ': ' . $value . "\n";
197 else
198 $string = $spaces . $key . ":\n";
199 }
200 return $string;
201 }
202
203 /**
204 * Creates a literal block for dumping
205 *
206 * @param $value String
207 * @param $indent Integer: the value of the indent
208 * @return String
209 */
210 private function _doLiteralBlock( $value, $indent ) {
211 $exploded = explode( "\n", $value );
212 $newValue = '|-';
213 $indent += $this->_dumpIndent;
214 $spaces = str_repeat( ' ', $indent );
215 foreach ( $exploded as $line ) {
216 $newValue .= "\n" . $spaces . trim( $line );
217 }
218 return $newValue;
219 }
220
221 /**
222 * Folds a string of text, if necessary
223 *
224 * @param $value String: the string you wish to fold
225 * @param $indent Integer: the indent of the current node
226 * @return String
227 */
228 private function _doFolding( $value, $indent ) {
229 // Don't do anything if wordwrap is set to 0
230 if ( $this->_dumpWordWrap === 0 ) {
231 return $value;
232 }
233
234 if ( strlen( $value ) > $this->_dumpWordWrap ) {
235 $indent += $this->_dumpIndent;
236 $indent = str_repeat( ' ', $indent );
237 $wrapped = wordwrap( $value, $this->_dumpWordWrap, "\n$indent" );
238 $value = ">-\n" . $indent . $wrapped;
239 }
240 return $value;
241 }
242 }