PageRenderTime 54ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Propel/Runtime/Om/NestedSetRecursiveIterator.php

http://github.com/propelorm/Propel2
PHP | 89 lines | 61 code | 16 blank | 12 comment | 5 complexity | 0e59732626c29c30dcd9f47e5d581b5e 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. namespace Propel\Runtime\Om;
  10. /**
  11. * Pre-order node iterator for Node objects.
  12. *
  13. * @author Heltem <heltem@o2php.com>
  14. */
  15. class NestedSetRecursiveIterator implements \RecursiveIterator
  16. {
  17. protected $topNode = null;
  18. protected $curNode = null;
  19. public function __construct($node)
  20. {
  21. $this->topNode = $node;
  22. $this->curNode = $node;
  23. }
  24. public function rewind()
  25. {
  26. $this->curNode = $this->topNode;
  27. }
  28. public function valid()
  29. {
  30. return null !== $this->curNode;
  31. }
  32. public function current()
  33. {
  34. return $this->curNode;
  35. }
  36. public function key()
  37. {
  38. $method = method_exists($this->curNode, 'getPath') ? 'getPath' : 'getAncestors';
  39. $key = array();
  40. foreach ($this->curNode->$method() as $node) {
  41. $key[] = $node->getPrimaryKey();
  42. }
  43. return implode('.', $key);
  44. }
  45. public function next()
  46. {
  47. $nextNode = null;
  48. $method = method_exists($this->curNode, 'retrieveNextSibling') ? 'retrieveNextSibling' : 'getNextSibling';
  49. if ($this->valid()) {
  50. while (null === $nextNode) {
  51. if (null === $this->curNode) {
  52. break;
  53. }
  54. if ($this->curNode->hasNextSibling()) {
  55. $nextNode = $this->curNode->$method();
  56. } else {
  57. break;
  58. }
  59. }
  60. $this->curNode = $nextNode;
  61. }
  62. return $this->curNode;
  63. }
  64. public function hasChildren()
  65. {
  66. return $this->curNode->hasChildren();
  67. }
  68. public function getChildren()
  69. {
  70. $method = method_exists($this->curNode, 'retrieveFirstChild') ? 'retrieveFirstChild' : 'getFirstChild';
  71. return new NestedSetRecursiveIterator($this->curNode->$method());
  72. }
  73. }