RuleWatchChain.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. * An extension of SplDoublyLinkedList with seek and removal of current element
  14. *
  15. * SplDoublyLinkedList only allows deleting a particular offset and has no
  16. * method to set the internal iterator to a particular offset.
  17. *
  18. * @author Nils Adermann <naderman@naderman.de>
  19. */
  20. class RuleWatchChain extends \SplDoublyLinkedList
  21. {
  22. protected $offset = 0;
  23. /**
  24. * Moves the internal iterator to the specified offset
  25. *
  26. * @param int $offset The offset to seek to.
  27. */
  28. public function seek($offset)
  29. {
  30. $this->rewind();
  31. for ($i = 0; $i < $offset; $i++, $this->next());
  32. }
  33. /**
  34. * Removes the current element from the list
  35. *
  36. * As SplDoublyLinkedList only allows deleting a particular offset and
  37. * incorrectly sets the internal iterator if you delete the current value
  38. * this method sets the internal iterator back to the following element
  39. * using the seek method.
  40. */
  41. public function remove()
  42. {
  43. $offset = $this->key();
  44. $this->offsetUnset($offset);
  45. $this->seek($offset);
  46. }
  47. }