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.
37 * @param $array Array: PHP array
38 * @param $indent Integer: Pass in false to use the default, which is 2
39 * @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.
62 * @param $array Array: PHP array
63 * @param $indent Integer: Pass in false to use the default, which is 2
64 * @param $wordwrap Integer: Pass in 0 for no wordwrap, false for default (40)
66 function dump($array,$indent = false,$wordwrap = false) {
67 // Dumps to some very clean YAML. We'll have to add some more features
68 // and options soon. And better support for folding.
70 // New features and options.
71 if ($indent === false or !is_numeric($indent)) {
72 $this->_dumpIndent
= 2;
74 $this->_dumpIndent
= $indent;
77 if ($wordwrap === false or !is_numeric($wordwrap)) {
78 $this->_dumpWordWrap
= 40;
80 $this->_dumpWordWrap
= $wordwrap;
86 // Start at the base of the array and move through it.
87 foreach ($array as $key => $value) {
88 $string .= $this->_yamlize($key,$value,0);
93 /**** Private Properties ****/
101 private $_dumpIndent;
102 private $_dumpWordWrap;
104 /**** Private Methods ****/
107 * Attempts to convert a key / value array item to YAML
109 * @param $key The name of the key
110 * @param $value The value of the item
111 * @param $indent The indent of the current node
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);
119 $indent +
= $this->_dumpIndent
;
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);
130 * Attempts to convert an array to YAML
132 * @param $array The array you want to convert
133 * @param $indent The indent of the current level
135 private function _yamlizeArray($array,$indent) {
136 if (is_array($array)) {
138 foreach ($array as $key => $value) {
139 $string .= $this->_yamlize($key,$value,$indent);
148 * Find out whether a string needs to be output as a literal rather than in plain style.
149 * Added by Roan Kattouw 13-03-2008
150 * @param $value The string to check
153 function _needLiteral($value) {
154 # Check whether the string contains # or : or begins with any of:
155 # [ - ? , [ ] { } ! * & | > ' " % @ ` ]
156 # or is a number or contains newlines
157 return (bool)(gettype($value) == "string" &&
158 (is_numeric($value) ||
159 strpos($value, "\n") ||
160 preg_match("/[#:]/", $value) ||
161 preg_match("/^[-?,[\]{}!*&|>'\"%@`]/", $value)));
166 * Returns YAML from a key and a value
168 * @param $key The name of the key
169 * @param $value The value of the item
170 * @param $indent The indent of the current node
172 private function _dumpNode($key,$value,$indent) {
173 // do some folding here, for blocks
174 if ($this->_needLiteral($value)) {
175 $value = $this->_doLiteralBlock($value,$indent);
177 $value = $this->_doFolding($value,$indent);
180 $spaces = str_repeat(' ',$indent);
184 if ($value !== '' && !is_null($value))
185 $string = $spaces.'- '.$value."\n";
187 $string = $spaces . "-\n";
190 if ($value !== '' && !is_null($value))
191 $string = $spaces . $key . ': ' . $value . "\n";
193 $string = $spaces . $key . ":\n";
199 * Creates a literal block for dumping
202 * @param $indent int The value of the indent
204 private function _doLiteralBlock($value,$indent) {
205 $exploded = explode("\n",$value);
207 $indent +
= $this->_dumpIndent
;
208 $spaces = str_repeat(' ',$indent);
209 foreach ($exploded as $line) {
210 $newValue .= "\n" . $spaces . trim($line);
216 * Folds a string of text, if necessary
218 * @param $value The string you wish to fold
220 private function _doFolding($value,$indent) {
221 // Don't do anything if wordwrap is set to 0
222 if ($this->_dumpWordWrap
=== 0) {
226 if (strlen($value) > $this->_dumpWordWrap
) {
227 $indent +
= $this->_dumpIndent
;
228 $indent = str_repeat(' ',$indent);
229 $wrapped = wordwrap($value,$this->_dumpWordWrap
,"\n$indent");
230 $value = ">\n".$indent.$wrapped;