summaryrefslogtreecommitdiff
path: root/plugins/af_readability/vendor/fivefilters/readability.php/src/Nodes/DOM/DOMNodeList.php
blob: a718c00ce7f2c4399f561f81f59de1b84080812f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php

namespace fivefilters\Readability\Nodes\DOM;

/**
 * Class DOMNodeList.
 *
 * This is a fake DOMNodeList class that allows adding items to the list. The original class is static and the nodes
 * are defined automagically when instantiating it. This fake version behaves exactly the same way but adds the function
 * add() that allows to insert new DOMNodes into the DOMNodeList.
 *
 * It cannot extend the original DOMNodeList class because the functionality behind the property ->length is hidden
 * from the user and cannot be extended, changed, or tweaked.
 */
class DOMNodeList implements \Countable, \IteratorAggregate
{
    /**
     * @var array
     */
    protected $items = [];

    /**
     * @var int
     */
    protected $length = 0;

    /**
     * To allow access to length in the same way that DOMNodeList allows.
     *
     * {@inheritdoc}
     */
    public function __get($name)
    {
        switch ($name) {
            case 'length':
                return $this->length;
            default:
                trigger_error(sprintf('Undefined property: %s::%s', static::class, $name));
        }
    }

    /**
     * @param DOMNode|DOMElement|DOMComment $node
     *
     * @return DOMNodeList
     */
    public function add($node)
    {
        $this->items[] = $node;
        $this->length++;

        return $this;
    }

    /**
     * @param int $offset
     *
     * @return DOMNode|DOMElement|DOMComment
     */
    public function item(int $offset)
    {
        return $this->items[$offset];
    }

    /**
     * @return int|void
     */
    public function count(): int
    {
        return $this->length;
    }

    /**
     * To make it compatible with iterator_to_array() function.
     *
     * {@inheritdoc}
     */
    public function getIterator(): \ArrayIterator
    {
        return new \ArrayIterator($this->items);
    }
}