-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAssertableElementsList.php
More file actions
189 lines (158 loc) · 5.35 KB
/
AssertableElementsList.php
File metadata and controls
189 lines (158 loc) · 5.35 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
declare(strict_types=1);
namespace Ziadoz\AssertableHtml\Dom;
use ArrayAccess;
use Countable;
use Dom\Element;
use Dom\HTMLCollection;
use Dom\HTMLElement;
use Dom\NodeList;
use InvalidArgumentException;
use IteratorAggregate;
use OutOfBoundsException;
use RuntimeException;
use Traversable;
use Ziadoz\AssertableHtml\Concerns\AssertsElementsList;
use Ziadoz\AssertableHtml\Concerns\Whenable;
use Ziadoz\AssertableHtml\Concerns\Withable;
final readonly class AssertableElementsList implements ArrayAccess, Countable, IteratorAggregate
{
use AssertsElementsList;
use Whenable;
use Withable;
/** The assertable elements. */
private array $elements;
/** Create a list of assertable elements. */
public function __construct(NodeList|HTMLCollection $nodes)
{
$this->elements = array_values(
array_map(
fn (HTMLElement|Element $element): AssertableElement => new AssertableElement($element),
$nodes instanceof NodeList
? iterator_to_array($nodes)
: $this->htmlCollectionToArray($nodes),
),
);
}
/** Convert a \Dom\HTMLCollection instance to an array. */
private function htmlCollectionToArray(HTMLCollection $nodes): array
{
$array = [];
foreach ($nodes as $node) {
$array[] = $node;
}
return $array;
}
/** Get the assertable element list HTML. */
public function getHtml(): string
{
return implode("\n", array_map(
fn (AssertableElement $element): string => $element->getHtml(),
$this->elements,
));
}
/** Dump the assertable element list. */
public function dump(): void
{
dump($this->getHtml());
}
/** Dump and die the assertable element list. */
public function dd(): never
{
dd($this->getHtml());
}
/** Return whether the assertable element list is empty. */
public function empty(): bool
{
return count($this) === 0;
}
/** Get the assertable element at the nth position in the assertable element list. */
public function nth(int $index): ?AssertableElement
{
return $this->offsetGet($index);
}
/** Return the first assertable element in the assertable element list. */
public function first(): ?AssertableElement
{
return $this->offsetGet(0);
}
/** Return the last assertable element in the assertable element list. */
public function last(): ?AssertableElement
{
return $this->offsetGet(count($this) - 1);
}
/**
* Perform a callback on each element in the assertable element list.
*
* @param callable(AssertableElement $element, int $sequence): void $callback
*/
public function each(callable $callback): self
{
array_map($callback, array_values($this->elements), array_keys($this->elements));
return $this;
}
/**
* Perform a callback on each element in the assertable element list in sequence.
*
* @param callable(AssertableElement $element, int $sequence): void ...$callbacks
*/
public function sequence(callable ...$callbacks): self
{
if (count($callbacks) === 0) {
throw new InvalidArgumentException('No sequence callbacks given.');
}
foreach ($this as $index => $element) {
$callback = $callbacks[$index] ?? throw new OutOfBoundsException(sprintf(
'Missing sequence callback for element at position [%d].',
$index,
));
$callback($element, $index);
}
return $this;
}
/*
|--------------------------------------------------------------------------
| Array Access
|--------------------------------------------------------------------------
*/
/** Check an assertable element exists in the assertable element list. */
public function offsetExists(mixed $offset): bool
{
return isset($this->elements[(int) $offset]);
}
/** Get an assertable element in the assertable element list. */
public function offsetGet(mixed $offset): ?AssertableElement
{
return $this->elements[(int) $offset];
}
/** Unable to add an assertable element to the assertable element list. */
public function offsetSet(mixed $offset, mixed $value): void
{
throw new RuntimeException('Unable to add elements to list');
}
/** Unable to remove an assertable element from the assertable element list. */
public function offsetUnset(mixed $offset): void
{
throw new RuntimeException('Unable to remove elements from list');
}
/*
|--------------------------------------------------------------------------
| Countable
|--------------------------------------------------------------------------
*/
/** Return the number of assertable elements in the assertable element list. */
public function count(): int
{
return count($this->elements);
}
/*
|--------------------------------------------------------------------------
| IteratorAggregate
|--------------------------------------------------------------------------
*/
/** Get an iterator of the assertable element list. */
public function getIterator(): Traversable
{
yield from $this->elements;
}
}