SegmentIterator.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. /**
  3. * Segments iterator
  4. * You need PHP 5.2 at least
  5. * You need Zip Extension or PclZip library
  6. *
  7. * @copyright GPL License 2008 - Julien Pauli - Cyril PIERRE de GEYER - Anaska (http://www.anaska.com)
  8. * @license https://www.gnu.org/copyleft/gpl.html GPL License
  9. * @version 1.3
  10. */
  11. class SegmentIterator implements RecursiveIterator
  12. {
  13. private $ref;
  14. private $key;
  15. public function __construct(array $ref)
  16. {
  17. $this->ref = $ref;
  18. $this->key = 0;
  19. $this->keys = array_keys($this->ref);
  20. }
  21. public function hasChildren()
  22. {
  23. return $this->valid() && $this->current() instanceof Segment;
  24. }
  25. public function current()
  26. {
  27. return $this->ref[$this->keys[$this->key]];
  28. }
  29. function getChildren()
  30. {
  31. return new self($this->current()->children);
  32. }
  33. public function key()
  34. {
  35. return $this->key;
  36. }
  37. public function valid()
  38. {
  39. return array_key_exists($this->key, $this->keys);
  40. }
  41. public function rewind()
  42. {
  43. $this->key = 0;
  44. }
  45. public function next()
  46. {
  47. $this->key ++;
  48. }
  49. }