00001 <?php
00020 class Spyc {
00021
00041 public static function YAMLDump($array,$indent = false,$wordwrap = false) {
00042 $spyc = new Spyc;
00043 return $spyc->dump($array,$indent,$wordwrap);
00044 }
00045
00066 function dump($array,$indent = false,$wordwrap = false) {
00067
00068
00069
00070
00071 if ($indent === false or !is_numeric($indent)) {
00072 $this->_dumpIndent = 2;
00073 } else {
00074 $this->_dumpIndent = $indent;
00075 }
00076
00077 if ($wordwrap === false or !is_numeric($wordwrap)) {
00078 $this->_dumpWordWrap = 40;
00079 } else {
00080 $this->_dumpWordWrap = $wordwrap;
00081 }
00082
00083
00084 $string = "---\n";
00085
00086
00087 foreach ($array as $key => $value) {
00088 $string .= $this->_yamlize($key,$value,0);
00089 }
00090 return $string;
00091 }
00092
00093
00094
00095 private $_haveRefs;
00096 private $_allNodes;
00097 private $_lastIndent;
00098 private $_lastNode;
00099 private $_inBlock;
00100 private $_isInline;
00101 private $_dumpIndent;
00102 private $_dumpWordWrap;
00103
00104
00105
00113 private function _yamlize($key,$value,$indent) {
00114 if (is_array($value)) {
00115
00116
00117 $string = $this->_dumpNode($key,NULL,$indent);
00118
00119 $indent += $this->_dumpIndent;
00120
00121 $string .= $this->_yamlizeArray($value,$indent);
00122 } elseif (!is_array($value)) {
00123
00124 $string = $this->_dumpNode($key,$value,$indent);
00125 }
00126 return $string;
00127 }
00128
00135 private function _yamlizeArray($array,$indent) {
00136 if (is_array($array)) {
00137 $string = '';
00138 foreach ($array as $key => $value) {
00139 $string .= $this->_yamlize($key,$value,$indent);
00140 }
00141 return $string;
00142 } else {
00143 return false;
00144 }
00145 }
00146
00153 function _needLiteral($value) {
00154 # Check whether the string contains # or : or begins with any of:
00155 # [ - ? , [ ] { } ! * & | > ' " % @ ` ]
00156 # or is a number or contains newlines
00157 return (bool)(gettype($value) == "string" &&
00158 (is_numeric($value) ||
00159 strpos($value, "\n") ||
00160 preg_match("/[#:]/", $value) ||
00161 preg_match("/^[-?,[\]{}!*&|>'\"%@`]/", $value)));
00162
00163 }
00164
00172 private function _dumpNode($key,$value,$indent) {
00173
00174 if ($this->_needLiteral($value)) {
00175 $value = $this->_doLiteralBlock($value,$indent);
00176 } else {
00177 $value = $this->_doFolding($value,$indent);
00178 }
00179
00180 $spaces = str_repeat(' ',$indent);
00181
00182 if (is_int($key)) {
00183
00184 if ($value !== '' && !is_null($value))
00185 $string = $spaces.'- '.$value."\n";
00186 else
00187 $string = $spaces . "-\n";
00188 } else {
00189
00190 if ($value !== '' && !is_null($value))
00191 $string = $spaces . $key . ': ' . $value . "\n";
00192 else
00193 $string = $spaces . $key . ":\n";
00194 }
00195 return $string;
00196 }
00197
00204 private function _doLiteralBlock($value,$indent) {
00205 $exploded = explode("\n",$value);
00206 $newValue = '|';
00207 $indent += $this->_dumpIndent;
00208 $spaces = str_repeat(' ',$indent);
00209 foreach ($exploded as $line) {
00210 $newValue .= "\n" . $spaces . trim($line);
00211 }
00212 return $newValue;
00213 }
00214
00220 private function _doFolding($value,$indent) {
00221
00222 if ($this->_dumpWordWrap === 0) {
00223 return $value;
00224 }
00225
00226 if (strlen($value) > $this->_dumpWordWrap) {
00227 $indent += $this->_dumpIndent;
00228 $indent = str_repeat(' ',$indent);
00229 $wrapped = wordwrap($value,$this->_dumpWordWrap,"\n$indent");
00230 $value = ">\n".$indent.$wrapped;
00231 }
00232 return $value;
00233 }
00234 }