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 ****/
95 * Unused variables, but just commented rather than deleting
96 * to save altering the library
104 private $_dumpIndent;
105 private $_dumpWordWrap;
107 /**** Private Methods ****/
110 * Attempts to convert a key / value array item to YAML
112 * @param $key Mixed: the name of the key
113 * @param $value Mixed: the value of the item
114 * @param $indent Integer: the indent of the current node
117 private function _yamlize( $key, $value, $indent ) {
118 if ( is_array( $value ) ) {
119 // It has children. What to do?
120 // Make it the right kind of item
121 $string = $this->_dumpNode( $key, null, $indent );
123 $indent +
= $this->_dumpIndent
;
125 $string .= $this->_yamlizeArray( $value, $indent );
126 } elseif ( !is_array( $value ) ) {
127 // It doesn't have children. Yip.
128 $string = $this->_dumpNode( $key, $value, $indent );
134 * Attempts to convert an array to YAML
136 * @param $array Array: the array you want to convert
137 * @param $indent Integer: the indent of the current level
140 private function _yamlizeArray( $array, $indent ) {
141 if ( is_array( $array ) ) {
143 foreach ( $array as $key => $value ) {
144 $string .= $this->_yamlize( $key, $value, $indent );
153 * Find out whether a string needs to be output as a literal rather than in plain style.
154 * Added by Roan Kattouw 13-03-2008
156 * @param $value String: the string to check
159 function _needLiteral( $value ) {
160 // Check whether the string contains # or : or begins with any of:
161 // [ - ? , [ ] { } ! * & | > ' " % @ ` ]
162 // or is a number or contains newlines
163 return (bool)( gettype( $value ) == "string" &&
164 ( is_numeric( $value ) ||
165 strpos( $value, "\n" ) ||
166 preg_match( "/[#:]/", $value ) ||
167 preg_match( "/^[-?,[\]{}!*&|>'\"%@`]/", $value ) ) );
171 * Returns YAML from a key and a value
173 * @param $key Mixed: the name of the key
174 * @param $value Mixed: the value of the item
175 * @param $indent Integer: the indent of the current node
178 private function _dumpNode( $key, $value, $indent ) {
179 // do some folding here, for blocks
180 if ( $this->_needLiteral( $value ) ) {
181 $value = $this->_doLiteralBlock( $value, $indent );
183 $value = $this->_doFolding( $value, $indent );
186 $spaces = str_repeat( ' ', $indent );
188 if ( is_int( $key ) ) {
190 if ( $value !== '' && !is_null( $value ) )
191 $string = $spaces . '- ' . $value . "\n";
193 $string = $spaces . "-\n";
195 if ( $key == '*' ) // bug 21922 - Quote asterix used as keys
199 if ( $value !== '' && !is_null( $value ) )
200 $string = $spaces . $key . ': ' . $value . "\n";
202 $string = $spaces . $key . ":\n";
208 * Creates a literal block for dumping
210 * @param $value String
211 * @param $indent Integer: the value of the indent
214 private function _doLiteralBlock( $value, $indent ) {
215 $exploded = explode( "\n", $value );
217 $indent +
= $this->_dumpIndent
;
218 $spaces = str_repeat( ' ', $indent );
219 foreach ( $exploded as $line ) {
220 $newValue .= "\n" . $spaces . trim( $line );
226 * Folds a string of text, if necessary
228 * @param $value String: the string you wish to fold
229 * @param $indent Integer: the indent of the current node
232 private function _doFolding( $value, $indent ) {
233 // Don't do anything if wordwrap is set to 0
234 if ( $this->_dumpWordWrap
=== 0 ) {
238 if ( strlen( $value ) > $this->_dumpWordWrap
) {
239 $indent +
= $this->_dumpIndent
;
240 $indent = str_repeat( ' ', $indent );
241 $wrapped = wordwrap( $value, $this->_dumpWordWrap
, "\n$indent" );
242 $value = ">-\n" . $indent . $wrapped;