Stylize API up to date
[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 /**
95 private $_haveRefs;
96 private $_allNodes;
97 private $_lastIndent;
98 private $_lastNode;
99 private $_inBlock;
100 private $_isInline;
101 **/
102 private $_dumpIndent;
103 private $_dumpWordWrap;
104
105 /**** Private Methods ****/
106
107 /**
108 * Attempts to convert a key / value array item to YAML
109 *
110 * @param $key Mixed: the name of the key
111 * @param $value Mixed: the value of the item
112 * @param $indent Integer: the indent of the current node
113 * @return String
114 */
115 private function _yamlize( $key, $value, $indent ) {
116 if ( is_array( $value ) ) {
117 // It has children. What to do?
118 // Make it the right kind of item
119 $string = $this->_dumpNode( $key, null, $indent );
120 // Add the indent
121 $indent += $this->_dumpIndent;
122 // Yamlize the array
123 $string .= $this->_yamlizeArray( $value, $indent );
124 } elseif ( !is_array( $value ) ) {
125 // It doesn't have children. Yip.
126 $string = $this->_dumpNode( $key, $value, $indent );
127 }
128 return $string;
129 }
130
131 /**
132 * Attempts to convert an array to YAML
133 *
134 * @param $array Array: the array you want to convert
135 * @param $indent Integer: the indent of the current level
136 * @return String
137 */
138 private function _yamlizeArray( $array, $indent ) {
139 if ( is_array( $array ) ) {
140 $string = '';
141 foreach ( $array as $key => $value ) {
142 $string .= $this->_yamlize( $key, $value, $indent );
143 }
144 return $string;
145 } else {
146 return false;
147 }
148 }
149
150 /**
151 * Find out whether a string needs to be output as a literal rather than in plain style.
152 * Added by Roan Kattouw 13-03-2008
153 *
154 * @param $value String: the string to check
155 * @return Boolean
156 */
157 function _needLiteral( $value ) {
158 // Check whether the string contains # or : or begins with any of:
159 // [ - ? , [ ] { } ! * & | > ' " % @ ` ]
160 // or is a number or contains newlines
161 return (bool)( gettype( $value ) == "string" &&
162 ( is_numeric( $value ) ||
163 strpos( $value, "\n" ) ||
164 preg_match( "/[#:]/", $value ) ||
165 preg_match( "/^[-?,[\]{}!*&|>'\"%@`]/", $value ) ) );
166 }
167
168 /**
169 * Returns YAML from a key and a value
170 *
171 * @param $key Mixed: the name of the key
172 * @param $value Mixed: the value of the item
173 * @param $indent Integer: the indent of the current node
174 * @return String
175 */
176 private function _dumpNode( $key, $value, $indent ) {
177 // do some folding here, for blocks
178 if ( $this->_needLiteral( $value ) ) {
179 $value = $this->_doLiteralBlock( $value, $indent );
180 } else {
181 $value = $this->_doFolding( $value, $indent );
182 }
183
184 $spaces = str_repeat( ' ', $indent );
185
186 if ( is_int( $key ) ) {
187 // It's a sequence
188 if ( $value !== '' && !is_null( $value ) )
189 $string = $spaces . '- ' . $value . "\n";
190 else
191 $string = $spaces . "-\n";
192 } else {
193 if ( $key == '*' ) // bug 21922 - Quote asterix used as keys
194 $key = "'*'";
195
196 // It's mapped
197 if ( $value !== '' && !is_null( $value ) )
198 $string = $spaces . $key . ': ' . $value . "\n";
199 else
200 $string = $spaces . $key . ":\n";
201 }
202 return $string;
203 }
204
205 /**
206 * Creates a literal block for dumping
207 *
208 * @param $value String
209 * @param $indent Integer: the value of the indent
210 * @return String
211 */
212 private function _doLiteralBlock( $value, $indent ) {
213 $exploded = explode( "\n", $value );
214 $newValue = '|-';
215 $indent += $this->_dumpIndent;
216 $spaces = str_repeat( ' ', $indent );
217 foreach ( $exploded as $line ) {
218 $newValue .= "\n" . $spaces . trim( $line );
219 }
220 return $newValue;
221 }
222
223 /**
224 * Folds a string of text, if necessary
225 *
226 * @param $value String: the string you wish to fold
227 * @param $indent Integer: the indent of the current node
228 * @return String
229 */
230 private function _doFolding( $value, $indent ) {
231 // Don't do anything if wordwrap is set to 0
232 if ( $this->_dumpWordWrap === 0 ) {
233 return $value;
234 }
235
236 if ( strlen( $value ) > $this->_dumpWordWrap ) {
237 $indent += $this->_dumpIndent;
238 $indent = str_repeat( ' ', $indent );
239 $wrapped = wordwrap( $value, $this->_dumpWordWrap, "\n$indent" );
240 $value = ">-\n" . $indent . $wrapped;
241 }
242 return $value;
243 }
244 }