Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
365 lars 1
<?php declare(strict_types=1);
2
namespace PHPHtmlParser\Dom;
3
 
4
use ArrayAccess;
5
use ArrayIterator;
6
use Countable;
7
use IteratorAggregate;
8
use PHPHtmlParser\Exceptions\EmptyCollectionException;
9
 
10
/**
11
 * Class Collection
12
 *
13
 * @package PHPHtmlParser\Dom
14
 */
15
class Collection implements IteratorAggregate, ArrayAccess, Countable
16
{
17
 
18
    /**
19
     * The collection of Nodes.
20
     *
21
     * @var array
22
     */
23
    protected $collection = [];
24
 
25
    /**
26
     * Attempts to call the method on the first node in
27
     * the collection.
28
     *
29
     * @param string $method
30
     * @param array $arguments
31
     * @return mixed
32
     * @throws EmptyCollectionException
33
     */
34
    public function __call(string $method, array $arguments)
35
    {
36
        $node = reset($this->collection);
37
        if ($node instanceof AbstractNode) {
38
            return call_user_func_array([$node, $method], $arguments);
39
        } else {
40
            throw new EmptyCollectionException('The collection does not contain any Nodes.');
41
        }
42
    }
43
 
44
    /**
45
     * Attempts to apply the magic get to the first node
46
     * in the collection.
47
     *
48
     * @param mixed $key
49
     * @return mixed
50
     * @throws EmptyCollectionException
51
     */
52
    public function __get($key)
53
    {
54
        $node = reset($this->collection);
55
        if ($node instanceof AbstractNode) {
56
            return $node->$key;
57
        } else {
58
            throw new EmptyCollectionException('The collection does not contain any Nodes.');
59
        }
60
    }
61
 
62
    /**
63
     * Applies the magic string method to the first node in
64
     * the collection.
65
     *
66
     * @return string
67
     */
68
    public function __toString(): string
69
    {
70
        $node = reset($this->collection);
71
        if ($node instanceof AbstractNode) {
72
            return (string)$node;
73
        } else {
74
            return '';
75
        }
76
    }
77
 
78
    /**
79
     * Returns the count of the collection.
80
     *
81
     * @return int
82
     */
83
    public function count(): int
84
    {
85
        return count($this->collection);
86
    }
87
 
88
    /**
89
     * Returns an iterator for the collection.
90
     *
91
     * @return ArrayIterator
92
     */
93
    public function getIterator(): ArrayIterator
94
    {
95
        return new ArrayIterator($this->collection);
96
    }
97
 
98
    /**
99
     * Set an attribute by the given offset
100
     *
101
     * @param mixed $offset
102
     * @param mixed $value
103
     */
104
    public function offsetSet($offset, $value): void
105
    {
106
        if (is_null($offset)) {
107
            $this->collection[] = $value;
108
        } else {
109
            $this->collection[$offset] = $value;
110
        }
111
    }
112
 
113
    /**
114
     * Checks if an offset exists.
115
     *
116
     * @param mixed $offset
117
     * @return bool
118
     */
119
    public function offsetExists($offset): bool
120
    {
121
        return isset($this->collection[$offset]);
122
    }
123
 
124
    /**
125
     * Unset a collection Node.
126
     *
127
     * @param mixed $offset
128
     */
129
    public function offsetUnset($offset): void
130
    {
131
        unset($this->collection[$offset]);
132
    }
133
 
134
    /**
135
     * Gets a node at the given offset, or null
136
     *
137
     * @param mixed $offset
138
     * @return mixed
139
     */
140
    public function offsetGet($offset)
141
    {
142
        return $this->collection[$offset] ?? null;
143
    }
144
 
145
    /**
146
     * Returns this collection as an array.
147
     *
148
     * @return array
149
     */
150
    public function toArray(): array
151
    {
152
        return $this->collection;
153
    }
154
 
155
    /**
156
     * Similar to jQuery "each" method. Calls the callback with each
157
     * Node in this collection.
158
     *
159
     * @param callable $callback
160
     */
161
    public function each(callable $callback)
162
    {
163
        foreach ($this->collection as $key => $value) {
164
            $callback($value, $key);
165
        }
166
    }
167
}