forked from php-soap/encoding
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathElementList.php
More file actions
121 lines (106 loc) · 2.89 KB
/
ElementList.php
File metadata and controls
121 lines (106 loc) · 2.89 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
<?php
declare(strict_types=1);
namespace Soap\Encoding\Xml\Node;
use Closure;
use Countable;
use Dom\Element as DomElement;
use Soap\Encoding\Xml\Reader\DocumentToLookupArrayReader;
use Stringable;
use VeeWee\Xml\Dom\Document;
use function count;
use function Psl\Iter\reduce;
use function Psl\Vec\map;
use function VeeWee\Xml\Dom\Locator\Element\children as readChildren;
/**
* @psalm-import-type LookupArray from DocumentToLookupArrayReader
*/
final class ElementList implements Countable, Stringable
{
/** @var list<Element> */
private array $elements;
/**
* @no-named-arguments
*/
public function __construct(Element ...$elements)
{
$this->elements = $elements;
}
/**
* Can be used to parse a nested array structure to a full flattened ElementList.
*
* @see \Soap\Encoding\Xml\Reader\DocumentToLookupArrayReader::__invoke
*
* @param LookupArray $data
*/
public static function fromLookupArray(array $data): self
{
return new self(
...reduce(
$data,
/**
* @param list<Element> $elements
*
* @return list<Element>
*/
static fn (array $elements, string|Element|ElementList $value) => [
...$elements,
...match(true) {
$value instanceof Element => [$value],
$value instanceof ElementList => $value->elements(),
default => [], // Strings are considered simpleTypes - not elements
}
],
[],
)
);
}
/**
* @param non-empty-string $xml
*/
public static function fromString(string $xml): self
{
$doc = Document::fromXmlString($xml);
return new self(
...readChildren($doc->locateDocumentElement())->map(
static fn (DomElement $element): Element => Element::fromDOMElement($element)
)
);
}
public function append(Element $element): self
{
$this->elements[] = $element;
return $this;
}
/**
* @return list<Element>
*/
public function elements(): array
{
return $this->elements;
}
public function hasElements(): bool
{
return (bool) $this->elements;
}
/**
* @template R
* @param Closure(Element): R $mapper
* @return list<R>
*/
public function traverse(Closure $mapper): array
{
return map($this->elements, $mapper);
}
public function value(): string
{
return implode('', $this->traverse(static fn (Element $element): string => $element->value()));
}
public function __toString()
{
return $this->value();
}
public function count(): int
{
return count($this->elements);
}
}