PHPDocumentor [http://en.wikipedia.org/wiki/PhpDocumentor] documentation tweaking...
[lhc/web/wiklou.git] / includes / api / ApiFormatYaml_spyc.php
1 <?php
2 /**
3 * Spyc -- A Simple PHP YAML Class
4 * @version 0.2.3 -- 2006-02-04
5 * @author Chris Wanstrath <chris@ozmm.org>
6 * @link http://spyc.sourceforge.net/
7 * @copyright Copyright 2005-2006 Chris Wanstrath
8 * @license http://www.opensource.org/licenses/mit-license.php MIT License
9 * @addtogroup Spyc
10 */
11
12 /**
13 * A node, used by Spyc for parsing YAML.
14 * @addtogroup Spyc
15 */
16 class YAMLNode {
17 /**
18 * @access public
19 * @var string
20 */
21 var $parent;
22 var $id;
23 /**
24 * @access public
25 * @var mixed
26 */
27 var $data;
28 /**
29 * @access public
30 * @var int
31 */
32 var $indent;
33 /**
34 * @access public
35 * @var bool
36 */
37 var $children = false;
38
39 /**
40 * The constructor assigns the node a unique ID.
41 * @access public
42 * @return void
43 */
44 function YAMLNode() {
45 $this->id = uniqid('');
46 }
47 }
48
49 /**
50 * The Simple PHP YAML Class.
51 *
52 * This class can be used to read a YAML file and convert its contents
53 * into a PHP array. It currently supports a very limited subsection of
54 * the YAML spec.
55 *
56 * Usage:
57 * <code>
58 * $parser = new Spyc;
59 * $array = $parser->load($file);
60 * </code>
61 * @addtogroup Spyc
62 */
63 class Spyc {
64
65 /**
66 * Load YAML into a PHP array statically
67 *
68 * The load method, when supplied with a YAML stream (string or file),
69 * will do its best to convert YAML in a file into a PHP array. Pretty
70 * simple.
71 * Usage:
72 * <code>
73 * $array = Spyc::YAMLLoad('lucky.yml');
74 * print_r($array);
75 * </code>
76 * @access public
77 * @return array
78 * @param string $input Path of YAML file or string containing YAML
79 */
80 function YAMLLoad($input) {
81 $spyc = new Spyc;
82 return $spyc->load($input);
83 }
84
85 /**
86 * Dump YAML from PHP array statically
87 *
88 * The dump method, when supplied with an array, will do its best
89 * to convert the array into friendly YAML. Pretty simple. Feel free to
90 * save the returned string as nothing.yml and pass it around.
91 *
92 * Oh, and you can decide how big the indent is and what the wordwrap
93 * for folding is. Pretty cool -- just pass in 'false' for either if
94 * you want to use the default.
95 *
96 * Indent's default is 2 spaces, wordwrap's default is 40 characters. And
97 * you can turn off wordwrap by passing in 0.
98 *
99 * @access public
100 * @static
101 * @return string
102 * @param array $array PHP array
103 * @param int $indent Pass in false to use the default, which is 2
104 * @param int $wordwrap Pass in 0 for no wordwrap, false for default (40)
105 */
106 public static function YAMLDump($array,$indent = false,$wordwrap = false) {
107 $spyc = new Spyc;
108 return $spyc->dump($array,$indent,$wordwrap);
109 }
110
111 /**
112 * Load YAML into a PHP array from an instantiated object
113 *
114 * The load method, when supplied with a YAML stream (string or file path),
115 * will do its best to convert the YAML into a PHP array. Pretty simple.
116 * Usage:
117 * <code>
118 * $parser = new Spyc;
119 * $array = $parser->load('lucky.yml');
120 * print_r($array);
121 * </code>
122 * @access public
123 * @return array
124 * @param string $input Path of YAML file or string containing YAML
125 */
126 function load($input) {
127 // See what type of input we're talking about
128 // If it's not a file, assume it's a string
129 if (!empty($input) && (strpos($input, "\n") === false)
130 && file_exists($input)) {
131 $yaml = file($input);
132 } else {
133 $yaml = explode("\n",$input);
134 }
135 // Initiate some objects and values
136 $base = new YAMLNode;
137 $base->indent = 0;
138 $this->_lastIndent = 0;
139 $this->_lastNode = $base->id;
140 $this->_inBlock = false;
141 $this->_isInline = false;
142
143 foreach ($yaml as $linenum => $line) {
144 $ifchk = trim($line);
145
146 // If the line starts with a tab (instead of a space), throw a fit.
147 if (preg_match('/^(\t)+(\w+)/', $line)) {
148 $err = 'ERROR: Line '. ($linenum + 1) .' in your input YAML begins'.
149 ' with a tab. YAML only recognizes spaces. Please reformat.';
150 die($err);
151 }
152
153 if ($this->_inBlock === false && empty($ifchk)) {
154 continue;
155 } elseif ($this->_inBlock == true && empty($ifchk)) {
156 $last =& $this->_allNodes[$this->_lastNode];
157 $last->data[key($last->data)] .= "\n";
158 } elseif ($ifchk{0} != '#' && substr($ifchk,0,3) != '---') {
159 // Create a new node and get its indent
160 $node = new YAMLNode;
161 $node->indent = $this->_getIndent($line);
162
163 // Check where the node lies in the hierarchy
164 if ($this->_lastIndent == $node->indent) {
165 // If we're in a block, add the text to the parent's data
166 if ($this->_inBlock === true) {
167 $parent =& $this->_allNodes[$this->_lastNode];
168 $parent->data[key($parent->data)] .= trim($line).$this->_blockEnd;
169 } else {
170 // The current node's parent is the same as the previous node's
171 if (isset($this->_allNodes[$this->_lastNode])) {
172 $node->parent = $this->_allNodes[$this->_lastNode]->parent;
173 }
174 }
175 } elseif ($this->_lastIndent < $node->indent) {
176 if ($this->_inBlock === true) {
177 $parent =& $this->_allNodes[$this->_lastNode];
178 $parent->data[key($parent->data)] .= trim($line).$this->_blockEnd;
179 } elseif ($this->_inBlock === false) {
180 // The current node's parent is the previous node
181 $node->parent = $this->_lastNode;
182
183 // If the value of the last node's data was > or | we need to
184 // start blocking i.e. taking in all lines as a text value until
185 // we drop our indent.
186 $parent =& $this->_allNodes[$node->parent];
187 $this->_allNodes[$node->parent]->children = true;
188 if (is_array($parent->data)) {
189 $chk = $parent->data[key($parent->data)];
190 if ($chk === '>') {
191 $this->_inBlock = true;
192 $this->_blockEnd = ' ';
193 $parent->data[key($parent->data)] =
194 str_replace('>','',$parent->data[key($parent->data)]);
195 $parent->data[key($parent->data)] .= trim($line).' ';
196 $this->_allNodes[$node->parent]->children = false;
197 $this->_lastIndent = $node->indent;
198 } elseif ($chk === '|') {
199 $this->_inBlock = true;
200 $this->_blockEnd = "\n";
201 $parent->data[key($parent->data)] =
202 str_replace('|','',$parent->data[key($parent->data)]);
203 $parent->data[key($parent->data)] .= trim($line)."\n";
204 $this->_allNodes[$node->parent]->children = false;
205 $this->_lastIndent = $node->indent;
206 }
207 }
208 }
209 } elseif ($this->_lastIndent > $node->indent) {
210 // Any block we had going is dead now
211 if ($this->_inBlock === true) {
212 $this->_inBlock = false;
213 if ($this->_blockEnd = "\n") {
214 $last =& $this->_allNodes[$this->_lastNode];
215 $last->data[key($last->data)] =
216 trim($last->data[key($last->data)]);
217 }
218 }
219
220 // We don't know the parent of the node so we have to find it
221 // foreach ($this->_allNodes as $n) {
222 foreach ($this->_indentSort[$node->indent] as $n) {
223 if ($n->indent == $node->indent) {
224 $node->parent = $n->parent;
225 }
226 }
227 }
228
229 if ($this->_inBlock === false) {
230 // Set these properties with information from our current node
231 $this->_lastIndent = $node->indent;
232 // Set the last node
233 $this->_lastNode = $node->id;
234 // Parse the YAML line and return its data
235 $node->data = $this->_parseLine($line);
236 // Add the node to the master list
237 $this->_allNodes[$node->id] = $node;
238 // Add a reference to the node in an indent array
239 $this->_indentSort[$node->indent][] =& $this->_allNodes[$node->id];
240 // Add a reference to the node in a References array if this node
241 // has a YAML reference in it.
242 if (
243 ( (is_array($node->data)) &&
244 isset($node->data[key($node->data)]) &&
245 (!is_array($node->data[key($node->data)])) )
246 &&
247 ( (preg_match('/^&([^ ]+)/',$node->data[key($node->data)]))
248 ||
249 (preg_match('/^\*([^ ]+)/',$node->data[key($node->data)])) )
250 ) {
251 $this->_haveRefs[] =& $this->_allNodes[$node->id];
252 } elseif (
253 ( (is_array($node->data)) &&
254 isset($node->data[key($node->data)]) &&
255 (is_array($node->data[key($node->data)])) )
256 ) {
257 // Incomplete reference making code. Ugly, needs cleaned up.
258 foreach ($node->data[key($node->data)] as $d) {
259 if ( !is_array($d) &&
260 ( (preg_match('/^&([^ ]+)/',$d))
261 ||
262 (preg_match('/^\*([^ ]+)/',$d)) )
263 ) {
264 $this->_haveRefs[] =& $this->_allNodes[$node->id];
265 }
266 }
267 }
268 }
269 }
270 }
271 unset($node);
272
273 // Here we travel through node-space and pick out references (& and *)
274 $this->_linkReferences();
275
276 // Build the PHP array out of node-space
277 $trunk = $this->_buildArray();
278 return $trunk;
279 }
280
281 /**
282 * Dump PHP array to YAML
283 *
284 * The dump method, when supplied with an array, will do its best
285 * to convert the array into friendly YAML. Pretty simple. Feel free to
286 * save the returned string as tasteful.yml and pass it around.
287 *
288 * Oh, and you can decide how big the indent is and what the wordwrap
289 * for folding is. Pretty cool -- just pass in 'false' for either if
290 * you want to use the default.
291 *
292 * Indent's default is 2 spaces, wordwrap's default is 40 characters. And
293 * you can turn off wordwrap by passing in 0.
294 *
295 * @access public
296 * @return string
297 * @param array $array PHP array
298 * @param int $indent Pass in false to use the default, which is 2
299 * @param int $wordwrap Pass in 0 for no wordwrap, false for default (40)
300 */
301 function dump($array,$indent = false,$wordwrap = false) {
302 // Dumps to some very clean YAML. We'll have to add some more features
303 // and options soon. And better support for folding.
304
305 // New features and options.
306 if ($indent === false or !is_numeric($indent)) {
307 $this->_dumpIndent = 2;
308 } else {
309 $this->_dumpIndent = $indent;
310 }
311
312 if ($wordwrap === false or !is_numeric($wordwrap)) {
313 $this->_dumpWordWrap = 40;
314 } else {
315 $this->_dumpWordWrap = $wordwrap;
316 }
317
318 // New YAML document
319 $string = "---\n";
320
321 // Start at the base of the array and move through it.
322 foreach ($array as $key => $value) {
323 $string .= $this->_yamlize($key,$value,0);
324 }
325 return $string;
326 }
327
328 /**** Private Properties ****/
329
330 /**
331 * @access private
332 * @var mixed
333 */
334 var $_haveRefs;
335 var $_allNodes;
336 var $_lastIndent;
337 var $_lastNode;
338 var $_inBlock;
339 var $_isInline;
340 var $_dumpIndent;
341 var $_dumpWordWrap;
342
343 /**** Private Methods ****/
344
345 /**
346 * Attempts to convert a key / value array item to YAML
347 * @access private
348 * @return string
349 * @param $key The name of the key
350 * @param $value The value of the item
351 * @param $indent The indent of the current node
352 */
353 function _yamlize($key,$value,$indent) {
354 if (is_array($value)) {
355 // It has children. What to do?
356 // Make it the right kind of item
357 $string = $this->_dumpNode($key,NULL,$indent);
358 // Add the indent
359 $indent += $this->_dumpIndent;
360 // Yamlize the array
361 $string .= $this->_yamlizeArray($value,$indent);
362 } elseif (!is_array($value)) {
363 // It doesn't have children. Yip.
364 $string = $this->_dumpNode($key,$value,$indent);
365 }
366 return $string;
367 }
368
369 /**
370 * Attempts to convert an array to YAML
371 * @access private
372 * @return string
373 * @param $array The array you want to convert
374 * @param $indent The indent of the current level
375 */
376 function _yamlizeArray($array,$indent) {
377 if (is_array($array)) {
378 $string = '';
379 foreach ($array as $key => $value) {
380 $string .= $this->_yamlize($key,$value,$indent);
381 }
382 return $string;
383 } else {
384 return false;
385 }
386 }
387
388 /**
389 * Returns YAML from a key and a value
390 * @access private
391 * @return string
392 * @param $key The name of the key
393 * @param $value The value of the item
394 * @param $indent The indent of the current node
395 */
396 function _dumpNode($key,$value,$indent) {
397 // do some folding here, for blocks
398 if (strpos($value,"\n")) {
399 $value = $this->_doLiteralBlock($value,$indent);
400 } else {
401 $value = $this->_doFolding($value,$indent);
402 }
403
404 $spaces = str_repeat(' ',$indent);
405
406 if (is_int($key)) {
407 // It's a sequence
408 $string = $spaces.'- '.$value."\n";
409 } else {
410 // It's mapped
411 $string = $spaces.$key.': '.$value."\n";
412 }
413 return $string;
414 }
415
416 /**
417 * Creates a literal block for dumping
418 * @access private
419 * @return string
420 * @param $value
421 * @param $indent int The value of the indent
422 */
423 function _doLiteralBlock($value,$indent) {
424 $exploded = explode("\n",$value);
425 $newValue = '|';
426 $indent += $this->_dumpIndent;
427 $spaces = str_repeat(' ',$indent);
428 foreach ($exploded as $line) {
429 $newValue .= "\n" . $spaces . trim($line);
430 }
431 return $newValue;
432 }
433
434 /**
435 * Folds a string of text, if necessary
436 * @access private
437 * @return string
438 * @param $value The string you wish to fold
439 */
440 function _doFolding($value,$indent) {
441 // Don't do anything if wordwrap is set to 0
442 if ($this->_dumpWordWrap === 0) {
443 return $value;
444 }
445
446 if (strlen($value) > $this->_dumpWordWrap) {
447 $indent += $this->_dumpIndent;
448 $indent = str_repeat(' ',$indent);
449 $wrapped = wordwrap($value,$this->_dumpWordWrap,"\n$indent");
450 $value = ">\n".$indent.$wrapped;
451 }
452 return $value;
453 }
454
455 /* Methods used in loading */
456
457 /**
458 * Finds and returns the indentation of a YAML line
459 * @access private
460 * @return int
461 * @param string $line A line from the YAML file
462 */
463 function _getIndent($line) {
464 $match = array();
465 preg_match('/^\s{1,}/',$line,$match);
466 if (!empty($match[0])) {
467 $indent = substr_count($match[0],' ');
468 } else {
469 $indent = 0;
470 }
471 return $indent;
472 }
473
474 /**
475 * Parses YAML code and returns an array for a node
476 * @access private
477 * @return array
478 * @param string $line A line from the YAML file
479 */
480 function _parseLine($line) {
481 $line = trim($line);
482
483 $array = array();
484
485 if (preg_match('/^-(.*):$/',$line)) {
486 // It's a mapped sequence
487 $key = trim(substr(substr($line,1),0,-1));
488 $array[$key] = '';
489 } elseif ($line[0] == '-' && substr($line,0,3) != '---') {
490 // It's a list item but not a new stream
491 if (strlen($line) > 1) {
492 $value = trim(substr($line,1));
493 // Set the type of the value. Int, string, etc
494 $value = $this->_toType($value);
495 $array[] = $value;
496 } else {
497 $array[] = array();
498 }
499 } elseif (preg_match('/^(.+):/',$line,$key)) {
500 // It's a key/value pair most likely
501 // If the key is in double quotes pull it out
502 $matches = array();
503 if (preg_match('/^(["\'](.*)["\'](\s)*:)/',$line,$matches)) {
504 $value = trim(str_replace($matches[1],'',$line));
505 $key = $matches[2];
506 } else {
507 // Do some guesswork as to the key and the value
508 $explode = explode(':',$line);
509 $key = trim($explode[0]);
510 array_shift($explode);
511 $value = trim(implode(':',$explode));
512 }
513
514 // Set the type of the value. Int, string, etc
515 $value = $this->_toType($value);
516 if (empty($key)) {
517 $array[] = $value;
518 } else {
519 $array[$key] = $value;
520 }
521 }
522 return $array;
523 }
524
525 /**
526 * Finds the type of the passed value, returns the value as the new type.
527 * @access private
528 * @param string $value
529 * @return mixed
530 */
531 function _toType($value) {
532 $matches = array();
533 if (preg_match('/^("(.*)"|\'(.*)\')/',$value,$matches)) {
534 $value = (string)preg_replace('/(\'\'|\\\\\')/',"'",end($matches));
535 $value = preg_replace('/\\\\"/','"',$value);
536 } elseif (preg_match('/^\\[(.+)\\]$/',$value,$matches)) {
537 // Inline Sequence
538
539 // Take out strings sequences and mappings
540 $explode = $this->_inlineEscape($matches[1]);
541
542 // Propogate value array
543 $value = array();
544 foreach ($explode as $v) {
545 $value[] = $this->_toType($v);
546 }
547 } elseif (strpos($value,': ')!==false && !preg_match('/^{(.+)/',$value)) {
548 // It's a map
549 $array = explode(': ',$value);
550 $key = trim($array[0]);
551 array_shift($array);
552 $value = trim(implode(': ',$array));
553 $value = $this->_toType($value);
554 $value = array($key => $value);
555 } elseif (preg_match("/{(.+)}$/",$value,$matches)) {
556 // Inline Mapping
557
558 // Take out strings sequences and mappings
559 $explode = $this->_inlineEscape($matches[1]);
560
561 // Propogate value array
562 $array = array();
563 foreach ($explode as $v) {
564 $array = $array + $this->_toType($v);
565 }
566 $value = $array;
567 } elseif (strtolower($value) == 'null' or $value == '' or $value == '~') {
568 $value = NULL;
569 } elseif (ctype_digit($value)) {
570 $value = (int)$value;
571 } elseif (in_array(strtolower($value),
572 array('true', 'on', '+', 'yes', 'y'))) {
573 $value = TRUE;
574 } elseif (in_array(strtolower($value),
575 array('false', 'off', '-', 'no', 'n'))) {
576 $value = FALSE;
577 } elseif (is_numeric($value)) {
578 $value = (float)$value;
579 } else {
580 // Just a normal string, right?
581 $value = trim(preg_replace('/#(.+)$/','',$value));
582 }
583
584 return $value;
585 }
586
587 /**
588 * Used in inlines to check for more inlines or quoted strings
589 * @access private
590 * @return array
591 */
592 function _inlineEscape($inline) {
593 // There's gotta be a cleaner way to do this...
594 // While pure sequences seem to be nesting just fine,
595 // pure mappings and mappings with sequences inside can't go very
596 // deep. This needs to be fixed.
597
598 // Check for strings
599 $regex = '/(?:(")|(?:\'))((?(1)[^"]+|[^\']+))(?(1)"|\')/';
600 $strings = array();
601 if (preg_match_all($regex,$inline,$strings)) {
602 $saved_strings[] = $strings[0][0];
603 $inline = preg_replace($regex,'YAMLString',$inline);
604 }
605 unset($regex);
606
607 // Check for sequences
608 $seqs = array();
609 if (preg_match_all('/\[(.+)\]/U',$inline,$seqs)) {
610 $inline = preg_replace('/\[(.+)\]/U','YAMLSeq',$inline);
611 $seqs = $seqs[0];
612 }
613
614 // Check for mappings
615 $maps = array();
616 if (preg_match_all('/{(.+)}/U',$inline,$maps)) {
617 $inline = preg_replace('/{(.+)}/U','YAMLMap',$inline);
618 $maps = $maps[0];
619 }
620
621 $explode = explode(', ',$inline);
622
623 // Re-add the strings
624 if (!empty($saved_strings)) {
625 $i = 0;
626 foreach ($explode as $key => $value) {
627 if (strpos($value,'YAMLString')) {
628 $explode[$key] = str_replace('YAMLString',$saved_strings[$i],$value);
629 ++$i;
630 }
631 }
632 }
633
634 // Re-add the sequences
635 if (!empty($seqs)) {
636 $i = 0;
637 foreach ($explode as $key => $value) {
638 if (strpos($value,'YAMLSeq') !== false) {
639 $explode[$key] = str_replace('YAMLSeq',$seqs[$i],$value);
640 ++$i;
641 }
642 }
643 }
644
645 // Re-add the mappings
646 if (!empty($maps)) {
647 $i = 0;
648 foreach ($explode as $key => $value) {
649 if (strpos($value,'YAMLMap') !== false) {
650 $explode[$key] = str_replace('YAMLMap',$maps[$i],$value);
651 ++$i;
652 }
653 }
654 }
655
656 return $explode;
657 }
658
659 /**
660 * Builds the PHP array from all the YAML nodes we've gathered
661 * @access private
662 * @return array
663 */
664 function _buildArray() {
665 $trunk = array();
666
667 if (!isset($this->_indentSort[0])) {
668 return $trunk;
669 }
670
671 foreach ($this->_indentSort[0] as $n) {
672 if (empty($n->parent)) {
673 $this->_nodeArrayizeData($n);
674 // Check for references and copy the needed data to complete them.
675 $this->_makeReferences($n);
676 // Merge our data with the big array we're building
677 $trunk = $this->_array_kmerge($trunk,$n->data);
678 }
679 }
680
681 return $trunk;
682 }
683
684 /**
685 * Traverses node-space and sets references (& and *) accordingly
686 * @access private
687 * @return bool
688 */
689 function _linkReferences() {
690 if (is_array($this->_haveRefs)) {
691 foreach ($this->_haveRefs as $node) {
692 if (!empty($node->data)) {
693 $key = key($node->data);
694 // If it's an array, don't check.
695 if (is_array($node->data[$key])) {
696 foreach ($node->data[$key] as $k => $v) {
697 $this->_linkRef($node,$key,$k,$v);
698 }
699 } else {
700 $this->_linkRef($node,$key);
701 }
702 }
703 }
704 }
705 return true;
706 }
707
708 function _linkRef(&$n,$key,$k = NULL,$v = NULL) {
709 if (empty($k) && empty($v)) {
710 // Look for &refs
711 $matches = array();
712 if (preg_match('/^&([^ ]+)/',$n->data[$key],$matches)) {
713 // Flag the node so we know it's a reference
714 $this->_allNodes[$n->id]->ref = substr($matches[0],1);
715 $this->_allNodes[$n->id]->data[$key] =
716 substr($n->data[$key],strlen($matches[0])+1);
717 // Look for *refs
718 } elseif (preg_match('/^\*([^ ]+)/',$n->data[$key],$matches)) {
719 $ref = substr($matches[0],1);
720 // Flag the node as having a reference
721 $this->_allNodes[$n->id]->refKey = $ref;
722 }
723 } elseif (!empty($k) && !empty($v)) {
724 if (preg_match('/^&([^ ]+)/',$v,$matches)) {
725 // Flag the node so we know it's a reference
726 $this->_allNodes[$n->id]->ref = substr($matches[0],1);
727 $this->_allNodes[$n->id]->data[$key][$k] =
728 substr($v,strlen($matches[0])+1);
729 // Look for *refs
730 } elseif (preg_match('/^\*([^ ]+)/',$v,$matches)) {
731 $ref = substr($matches[0],1);
732 // Flag the node as having a reference
733 $this->_allNodes[$n->id]->refKey = $ref;
734 }
735 }
736 }
737
738 /**
739 * Finds the children of a node and aids in the building of the PHP array
740 * @access private
741 * @param int $nid The id of the node whose children we're gathering
742 * @return array
743 */
744 function _gatherChildren($nid) {
745 $return = array();
746 $node =& $this->_allNodes[$nid];
747 foreach ($this->_allNodes as $z) {
748 if ($z->parent == $node->id) {
749 // We found a child
750 $this->_nodeArrayizeData($z);
751 // Check for references
752 $this->_makeReferences($z);
753 // Merge with the big array we're returning
754 // The big array being all the data of the children of our parent node
755 $return = $this->_array_kmerge($return,$z->data);
756 }
757 }
758 return $return;
759 }
760
761 /**
762 * Turns a node's data and its children's data into a PHP array
763 *
764 * @access private
765 * @param array $node The node which you want to arrayize
766 * @return boolean
767 */
768 function _nodeArrayizeData(&$node) {
769 if (is_array($node->data) && $node->children == true) {
770 // This node has children, so we need to find them
771 $childs = $this->_gatherChildren($node->id);
772 // We've gathered all our children's data and are ready to use it
773 $key = key($node->data);
774 $key = empty($key) ? 0 : $key;
775 // If it's an array, add to it of course
776 if (is_array($node->data[$key])) {
777 $node->data[$key] = $this->_array_kmerge($node->data[$key],$childs);
778 } else {
779 $node->data[$key] = $childs;
780 }
781 } elseif (!is_array($node->data) && $node->children == true) {
782 // Same as above, find the children of this node
783 $childs = $this->_gatherChildren($node->id);
784 $node->data = array();
785 $node->data[] = $childs;
786 }
787
788 // We edited $node by reference, so just return true
789 return true;
790 }
791
792 /**
793 * Traverses node-space and copies references to / from this object.
794 * @access private
795 * @param object $z A node whose references we wish to make real
796 * @return bool
797 */
798 function _makeReferences(&$z) {
799 // It is a reference
800 if (isset($z->ref)) {
801 $key = key($z->data);
802 // Copy the data to this object for easy retrieval later
803 $this->ref[$z->ref] =& $z->data[$key];
804 // It has a reference
805 } elseif (isset($z->refKey)) {
806 if (isset($this->ref[$z->refKey])) {
807 $key = key($z->data);
808 // Copy the data from this object to make the node a real reference
809 $z->data[$key] =& $this->ref[$z->refKey];
810 }
811 }
812 return true;
813 }
814
815
816 /**
817 * Merges arrays and maintains numeric keys.
818 *
819 * An ever-so-slightly modified version of the array_kmerge() function posted
820 * to php.net by mail at nospam dot iaindooley dot com on 2004-04-08.
821 *
822 * http://us3.php.net/manual/en/function.array-merge.php#41394
823 *
824 * @access private
825 * @param array $arr1
826 * @param array $arr2
827 * @return array
828 */
829 function _array_kmerge($arr1,$arr2) {
830 if(!is_array($arr1))
831 $arr1 = array();
832
833 if(!is_array($arr2))
834 $arr2 = array();
835
836 $keys1 = array_keys($arr1);
837 $keys2 = array_keys($arr2);
838 $keys = array_merge($keys1,$keys2);
839 $vals1 = array_values($arr1);
840 $vals2 = array_values($arr2);
841 $vals = array_merge($vals1,$vals2);
842 $ret = array();
843
844 foreach($keys as $key) {
845 list( /* unused */ ,$val) = each($vals);
846 // This is the good part! If a key already exists, but it's part of a
847 // sequence (an int), just keep addin numbers until we find a fresh one.
848 if (isset($ret[$key]) and is_int($key)) {
849 while (array_key_exists($key, $ret)) {
850 $key++;
851 }
852 }
853 $ret[$key] = $val;
854 }
855
856 return $ret;
857 }
858 }
859 ?>