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
00155
00156
00157 return (bool)( gettype( $value ) == "string" &&
00158 ( is_numeric( $value ) ||
00159 strpos( $value, "\n" ) ||
00160 preg_match( "/[#:]/", $value ) ||
00161 preg_match( "/^[-?,[\]{}!*&|>'\"%@`]/", $value ) ) );
00162 }
00163
00171 private function _dumpNode( $key, $value, $indent ) {
00172
00173 if ( $this->_needLiteral( $value ) ) {
00174 $value = $this->_doLiteralBlock( $value, $indent );
00175 } else {
00176 $value = $this->_doFolding( $value, $indent );
00177 }
00178
00179 $spaces = str_repeat( ' ', $indent );
00180
00181 if ( is_int( $key ) ) {
00182
00183 if ( $value !== '' && !is_null( $value ) )
00184 $string = $spaces . '- ' . $value . "\n";
00185 else
00186 $string = $spaces . "-\n";
00187 } else {
00188 if ($key == '*')
00189 $key = "'*'";
00190
00191
00192 if ( $value !== '' && !is_null( $value ) )
00193 $string = $spaces . $key . ': ' . $value . "\n";
00194 else
00195 $string = $spaces . $key . ":\n";
00196 }
00197 return $string;
00198 }
00199
00206 private function _doLiteralBlock( $value, $indent ) {
00207 $exploded = explode( "\n", $value );
00208 $newValue = '|-';
00209 $indent += $this->_dumpIndent;
00210 $spaces = str_repeat( ' ', $indent );
00211 foreach ( $exploded as $line ) {
00212 $newValue .= "\n" . $spaces . trim( $line );
00213 }
00214 return $newValue;
00215 }
00216
00222 private function _doFolding( $value, $indent ) {
00223
00224 if ( $this->_dumpWordWrap === 0 ) {
00225 return $value;
00226 }
00227
00228 if ( strlen( $value ) > $this->_dumpWordWrap ) {
00229 $indent += $this->_dumpIndent;
00230 $indent = str_repeat( ' ', $indent );
00231 $wrapped = wordwrap( $value, $this->_dumpWordWrap, "\n$indent" );
00232 $value = ">-\n" . $indent . $wrapped;
00233 }
00234 return $value;
00235 }
00236 }