RuleSetIterator.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\DependencyResolver;
  12. /**
  13. * @author Nils Adermann <naderman@naderman.de>
  14. */
  15. class RuleSetIterator implements \Iterator
  16. {
  17. protected $rules;
  18. protected $types;
  19. protected $currentOffset;
  20. protected $currentType;
  21. protected $currentTypeOffset;
  22. public function __construct(array $rules)
  23. {
  24. $this->rules = $rules;
  25. $this->types = array_keys($rules);
  26. sort($this->types);
  27. $this->rewind();
  28. }
  29. public function current()
  30. {
  31. return $this->rules[$this->currentType][$this->currentOffset];
  32. }
  33. public function key()
  34. {
  35. return $this->currentType;
  36. }
  37. public function next()
  38. {
  39. $this->currentOffset++;
  40. if (!isset($this->rules[$this->currentType])) {
  41. return;
  42. }
  43. if ($this->currentOffset >= sizeof($this->rules[$this->currentType])) {
  44. $this->currentOffset = 0;
  45. do {
  46. $this->currentTypeOffset++;
  47. if (!isset($this->types[$this->currentTypeOffset])) {
  48. $this->currentType = -1;
  49. break;
  50. }
  51. $this->currentType = $this->types[$this->currentTypeOffset];
  52. } while (isset($this->types[$this->currentTypeOffset]) && !sizeof($this->rules[$this->currentType]));
  53. }
  54. }
  55. public function rewind()
  56. {
  57. $this->currentOffset = 0;
  58. $this->currentTypeOffset = -1;
  59. $this->currentType = -1;
  60. do {
  61. $this->currentTypeOffset++;
  62. if (!isset($this->types[$this->currentTypeOffset])) {
  63. $this->currentType = -1;
  64. break;
  65. }
  66. $this->currentType = $this->types[$this->currentTypeOffset];
  67. } while (isset($this->types[$this->currentTypeOffset]) && !sizeof($this->rules[$this->currentType]));
  68. }
  69. public function valid()
  70. {
  71. return isset($this->rules[$this->currentType][$this->currentOffset]);
  72. }
  73. }