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
12 * The Simple PHP YAML Class.
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
23 * Dump YAML from PHP array statically
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.
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.
33 * Indent's default is 2 spaces, wordwrap's default is 40 characters. And
34 * you can turn off wordwrap by passing in 0.
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)
41 public static function YAMLDump( $array, $indent = false, $wordwrap = false ) {
43 return $spyc->dump( $array, $indent, $wordwrap );
47 * Dump PHP array to YAML
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.
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.
57 * Indent's default is 2 spaces, wordwrap's default is 40 characters. And
58 * you can turn off wordwrap by passing in 0.
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)
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.
69 // New features and options.
70 if ( $indent === false or !is_numeric( $indent ) ) {
71 $this->_dumpIndent
= 2;
73 $this->_dumpIndent
= $indent;
76 if ( $wordwrap === false or !is_numeric( $wordwrap ) ) {
77 $this->_dumpWordWrap
= 40;
79 $this->_dumpWordWrap
= $wordwrap;
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 );
92 /**** Private Properties ****/
102 private $_dumpIndent;
103 private $_dumpWordWrap;
105 /**** Private Methods ****/
108 * Attempts to convert a key / value array item to YAML
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
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 );
121 $indent +
= $this->_dumpIndent
;
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 );
132 * Attempts to convert an array to YAML
134 * @param $array Array: the array you want to convert
135 * @param $indent Integer: the indent of the current level
138 private function _yamlizeArray( $array, $indent ) {
139 if ( is_array( $array ) ) {
141 foreach ( $array as $key => $value ) {
142 $string .= $this->_yamlize( $key, $value, $indent );
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
154 * @param $value String: the string to check
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 ) ) );
169 * Returns YAML from a key and a value
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
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 );
181 $value = $this->_doFolding( $value, $indent );
184 $spaces = str_repeat( ' ', $indent );
186 if ( is_int( $key ) ) {
188 if ( $value !== '' && !is_null( $value ) )
189 $string = $spaces . '- ' . $value . "\n";
191 $string = $spaces . "-\n";
193 if ( $key == '*' ) // bug 21922 - Quote asterix used as keys
197 if ( $value !== '' && !is_null( $value ) )
198 $string = $spaces . $key . ': ' . $value . "\n";
200 $string = $spaces . $key . ":\n";
206 * Creates a literal block for dumping
208 * @param $value String
209 * @param $indent Integer: the value of the indent
212 private function _doLiteralBlock( $value, $indent ) {
213 $exploded = explode( "\n", $value );
215 $indent +
= $this->_dumpIndent
;
216 $spaces = str_repeat( ' ', $indent );
217 foreach ( $exploded as $line ) {
218 $newValue .= "\n" . $spaces . trim( $line );
224 * Folds a string of text, if necessary
226 * @param $value String: the string you wish to fold
227 * @param $indent Integer: the indent of the current node
230 private function _doFolding( $value, $indent ) {
231 // Don't do anything if wordwrap is set to 0
232 if ( $this->_dumpWordWrap
=== 0 ) {
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;