PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/runtime/lib/om/NestedSetRecursiveIterator.php

http://github.com/propelorm/Propel
PHP | 89 lines | 60 code | 15 blank | 14 comment | 5 complexity | e00ada89ed988f8d9a2dfca298242d09 MD5 | raw file
  1. <?php
  2. /**
  3. * This file is part of the Propel package.
  4. * For the full copyright and license information, please view the LICENSE
  5. * file that was distributed with this source code.
  6. *
  7. * @license MIT License
  8. */
  9. /**
  10. * Pre-order node iterator for Node objects.
  11. *
  12. * @author Heltem <heltem@o2php.com>
  13. * @version $Revision$
  14. * @package propel.runtime.om
  15. */
  16. class NestedSetRecursiveIterator implements RecursiveIterator
  17. {
  18. protected $topNode = null;
  19. protected $curNode = null;
  20. public function __construct($node)
  21. {
  22. $this->topNode = $node;
  23. $this->curNode = $node;
  24. }
  25. public function rewind()
  26. {
  27. $this->curNode = $this->topNode;
  28. }
  29. public function valid()
  30. {
  31. return ($this->curNode !== null);
  32. }
  33. public function current()
  34. {
  35. return $this->curNode;
  36. }
  37. public function key()
  38. {
  39. $method = method_exists($this->curNode, 'getPath') ? 'getPath' : 'getAncestors';
  40. $key = array();
  41. foreach ($this->curNode->$method() as $node) {
  42. $key[] = $node->getPrimaryKey();
  43. }
  44. return implode('.', $key);
  45. }
  46. public function next()
  47. {
  48. $nextNode = null;
  49. $method = method_exists($this->curNode, 'retrieveNextSibling') ? 'retrieveNextSibling' : 'getNextSibling';
  50. if ($this->valid()) {
  51. while (null === $nextNode) {
  52. if (null === $this->curNode) {
  53. break;
  54. }
  55. if ($this->curNode->hasNextSibling()) {
  56. $nextNode = $this->curNode->$method();
  57. } else {
  58. break;
  59. }
  60. }
  61. $this->curNode = $nextNode;
  62. }
  63. return $this->curNode;
  64. }
  65. public function hasChildren()
  66. {
  67. return $this->curNode->hasChildren();
  68. }
  69. public function getChildren()
  70. {
  71. $method = method_exists($this->curNode, 'retrieveFirstChild') ? 'retrieveFirstChild' : 'getFirstChild';
  72. return new NestedSetRecursiveIterator($this->curNode->$method());
  73. }
  74. }