1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15:
16:
17: 18: 19: 20: 21: 22:
23: class Horde_Yaml_Dumper
24: {
25: protected $_options = array();
26:
27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42:
43: public function dump($value, $options = array())
44: {
45:
46: if (!is_array($options)) {
47: throw new InvalidArgumentException('Options must be an array');
48: }
49:
50: $defaults = array('indent' => 2,
51: 'wordwrap' => 40);
52: $this->_options = array_merge($defaults, $options);
53:
54: if (! is_int($this->_options['indent'])) {
55: throw new InvalidArgumentException('Indent must be an integer');
56: }
57:
58: if (! is_int($this->_options['wordwrap'])) {
59: throw new InvalidArgumentException('Wordwrap column must be an integer');
60: }
61:
62:
63: $dump = "---\n";
64:
65:
66: foreach ($value as $key => $value) {
67: $dump .= $this->_yamlize($key, $value, 0);
68: }
69: return $dump;
70: }
71:
72: 73: 74: 75: 76: 77: 78: 79:
80: protected function _yamlize($key, $value, $indent)
81: {
82: if ($value instanceof Serializable) {
83:
84: $data = '!php/object::' . get_class($value) . ' ' . $value->serialize();
85: $string = $this->_dumpNode($key, $data, $indent);
86: } elseif (is_array($value) || $value instanceof Traversable) {
87:
88: $string = $this->_dumpNode($key, null, $indent);
89:
90:
91: $indent += $this->_options['indent'];
92:
93:
94: $string .= $this->_yamlizeArray($value, $indent);
95: } elseif (!is_array($value)) {
96:
97: $string = $this->_dumpNode($key, $value, $indent);
98: }
99:
100: return $string;
101: }
102:
103: 104: 105: 106: 107: 108: 109:
110: protected function _yamlizeArray($array, $indent)
111: {
112: if (!is_array($array)) {
113: return false;
114: }
115:
116: $string = '';
117: foreach ($array as $key => $value) {
118: $string .= $this->_yamlize($key, $value, $indent);
119: }
120: return $string;
121: }
122:
123: 124: 125: 126: 127: 128: 129: 130:
131: protected function _dumpNode($key, $value, $indent)
132: {
133: $literal = false;
134:
135: if (strpos($value, "\n") !== false
136: || strpos($value, ': ') !== false
137: || strpos($value, '- ') !== false) {
138: $value = $this->_doLiteralBlock($value, $indent);
139: $literal = true;
140: } else {
141: $value = $this->_fold($value, $indent);
142: }
143:
144: if (is_bool($value)) {
145: $value = ($value) ? 'true' : 'false';
146: } elseif (is_float($value)) {
147: if (is_nan($value)) {
148: $value = '.NAN';
149: } elseif ($value === INF) {
150: $value = '.INF';
151: } elseif ($value === -INF) {
152: $value = '-.INF';
153: }
154: }
155:
156: $spaces = str_repeat(' ', $indent);
157:
158:
159: if (!$literal && strpos($value, "\n") === false && strchr($value, '#')) {
160: $value = "'{$value}'";
161: }
162:
163: if (is_int($key)) {
164:
165: $string = $spaces . '- ' . $value . "\n";
166: } else {
167:
168: $string = $spaces . $key . ': ' . $value . "\n";
169: }
170:
171: return $string;
172: }
173:
174: 175: 176: 177: 178: 179: 180:
181: protected function _doLiteralBlock($value, $indent)
182: {
183: $exploded = explode("\n", $value);
184: $newValue = '|';
185: $indent += $this->_options['indent'];
186: $spaces = str_repeat(' ', $indent);
187: foreach ($exploded as $line) {
188: $newValue .= "\n" . $spaces . trim($line);
189: }
190: return $newValue;
191: }
192:
193: 194: 195: 196: 197: 198:
199: protected function _fold($value, $indent)
200: {
201:
202: if (! $this->_options['wordwrap']) {
203: return $value;
204: }
205:
206: if (strlen($value) > $this->_options['wordwrap']) {
207: $indent += $this->_options['indent'];
208: $indent = str_repeat(' ', $indent);
209: $wrapped = wordwrap($value, $this->_options['wordwrap'], "\n$indent");
210: $value = ">\n" . $indent . $wrapped;
211: }
212:
213: return $value;
214: }
215:
216: }
217: