Collection::__toString()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

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