Passed
Pull Request — master (#207)
by
unknown
02:09
created

Collection::__toString()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 2
rs 10
c 0
b 0
f 0
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 15
    public function __call(string $method, array $arguments)
35
    {
36 15
        $node = reset($this->collection);
37 15
        if ($node instanceof AbstractNode) {
38 12
            return call_user_func_array([$node, $method], $arguments);
39
        } else {
40 3
            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 12
    public function __get($key)
53
    {
54 12
        $node = reset($this->collection);
55 12
        if ($node instanceof AbstractNode) {
56 9
            return $node->$key;
57
        } else {
58 3
            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 6
    public function __toString(): string
69
    {
70 6
        $node = reset($this->collection);
71 6
        if ($node instanceof AbstractNode) {
72 3
            return (string)$node;
73
        } else {
74 3
            return '';
75
        }
76
    }
77
78
    /**
79
     * Returns the count of the collection.
80
     *
81
     * @return int
82
     */
83 57
    public function count(): int
84
    {
85 57
        return count($this->collection);
86
    }
87
88
    /**
89
     * Returns an iterator for the collection.
90
     *
91
     * @return ArrayIterator
92
     */
93 6
    public function getIterator(): ArrayIterator
94
    {
95 6
        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 135
    public function offsetSet($offset, $value): void
105
    {
106 135
        if (is_null($offset)) {
107 129
            $this->collection[] = $value;
108
        } else {
109 6
            $this->collection[$offset] = $value;
110
        }
111 135
    }
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 3
    public function offsetUnset($offset): void
130
    {
131 3
        unset($this->collection[$offset]);
132 3
    }
133
134
    /**
135
     * Gets a node at the given offset, or null
136
     *
137
     * @param mixed $offset
138
     * @return mixed
139
     */
140 60
    public function offsetGet($offset)
141
    {
142 60
        return $this->collection[$offset] ?? null;
143
    }
144
145
    /**
146
     * Returns this collection as an array.
147
     *
148
     * @return array
149
     */
150 3
    public function toArray(): array
151
    {
152 3
        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 3
    public function each(callable $callback)
162
    {
163 3
        foreach ($this->collection as $key => $value) {
164 3
            $callback($value, $key);
165
        }
166 3
    }
167
}
168