forked from php-soap/encoding
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathElement.php
More file actions
80 lines (66 loc) · 1.76 KB
/
Element.php
File metadata and controls
80 lines (66 loc) · 1.76 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
<?php
declare(strict_types=1);
namespace Soap\Encoding\Xml\Node;
use Dom\Element as DomElement;
use Stringable;
use VeeWee\Xml\Dom\Document;
use function Psl\invariant;
use function VeeWee\Xml\Dom\Assert\assert_document;
final class Element implements Stringable
{
private ?DomElement $element = null;
/**
* @var non-empty-string|null
*/
private ?string $value = null;
private function __construct()
{
}
/**
* @param non-empty-string $xml
*/
public static function fromString(string $xml): Element
{
$new = new self();
$new->element = null;
$new->value = $xml;
return $new;
}
public static function fromDOMElement(DomElement $element): self
{
$new = new self();
$new->element = $element;
$new->value = null;
return $new;
}
public function element(): DomElement
{
if (!$this->element) {
invariant($this->value !== null, 'Expected an XML value to be present');
$this->element = Document::fromXmlString($this->value)->locateDocumentElement();
}
return $this->element;
}
/**
* @return non-empty-string
*/
public function value(): string
{
if ($this->value === null) {
invariant($this->element !== null, 'Expected an DOMElement to be present');
$this->value = Document::fromXmlNode($this->element)->stringifyDocumentElement();
}
return $this->value;
}
public function document(): Document
{
return Document::fromUnsafeDocument(assert_document($this->element()->ownerDocument));
}
/**
* @return non-empty-string
*/
public function __toString()
{
return $this->value();
}
}