1<?php
2
3/*
4 * This file is part of Composer.
5 *
6 * (c) Nils Adermann <naderman@naderman.de>
7 *     Jordi Boggiano <j.boggiano@seld.be>
8 *
9 * For the full copyright and license information, please view the LICENSE
10 * file that was distributed with this source code.
11 */
12
13namespace Composer\DependencyResolver;
14
15/**
16 * An extension of SplDoublyLinkedList with seek and removal of current element
17 *
18 * SplDoublyLinkedList only allows deleting a particular offset and has no
19 * method to set the internal iterator to a particular offset.
20 *
21 * @author Nils Adermann <naderman@naderman.de>
22 */
23class RuleWatchChain extends \SplDoublyLinkedList
24{
25    protected $offset = 0;
26
27    /**
28     * Moves the internal iterator to the specified offset
29     *
30     * @param int $offset The offset to seek to.
31     */
32    public function seek($offset)
33    {
34        $this->rewind();
35        for ($i = 0; $i < $offset; $i++, $this->next());
36    }
37
38    /**
39     * Removes the current element from the list
40     *
41     * As SplDoublyLinkedList only allows deleting a particular offset and
42     * incorrectly sets the internal iterator if you delete the current value
43     * this method sets the internal iterator back to the following element
44     * using the seek method.
45     */
46    public function remove()
47    {
48        $offset = $this->key();
49        $this->offsetUnset($offset);
50        $this->seek($offset);
51    }
52}
53