forked from php-soap/encoding
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSoap11FaultEncoder.php
More file actions
99 lines (90 loc) · 3.25 KB
/
Soap11FaultEncoder.php
File metadata and controls
99 lines (90 loc) · 3.25 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
<?php
declare(strict_types=1);
namespace Soap\Encoding\Fault\Encoder;
use Soap\Encoding\Fault\Soap11Fault;
use Soap\Encoding\Restriction\WhitespaceRestriction;
use Soap\Xml\Xmlns;
use VeeWee\Reflecta\Iso\Iso;
use VeeWee\Xml\Dom\Document;
use VeeWee\Xml\Writer\Writer;
use function Psl\invariant;
use function VeeWee\Xml\Dom\Xpath\Configurator\namespaces;
use function VeeWee\Xml\Writer\Builder\children;
use function VeeWee\Xml\Writer\Builder\element;
use function VeeWee\Xml\Writer\Builder\namespaced_element;
use function VeeWee\Xml\Writer\Builder\raw;
use function VeeWee\Xml\Writer\Builder\value;
use function VeeWee\Xml\Writer\Mapper\memory_output;
/**
* @implements SoapFaultEncoder<Soap11Fault>
*/
final class Soap11FaultEncoder implements SoapFaultEncoder
{
/**
* @return Iso<Soap11Fault, non-empty-string>
*/
public function iso(): Iso
{
return new Iso(
$this->to(...),
$this->from(...)
);
}
/**
* @return non-empty-string
*/
private function to(Soap11Fault $fault): string
{
$envNamespace = Xmlns::soap11Envelope()->value();
/** @var non-empty-string */
return Writer::inMemory()
->write(children([
namespaced_element(
$envNamespace,
'env',
'Fault',
children([
element(
'faultcode',
value($fault->faultCode),
),
element(
'faultstring',
value($fault->faultString),
),
...(
$fault->faultActor !== null
? [
element(
'faultactor',
value($fault->faultActor)
)
]
: []
),
...($fault->detail !== null ? [raw($fault->detail)] : []),
])
)
]))
->map(memory_output());
}
/**
* @param non-empty-string $fault
*/
private function from(string $fault): Soap11Fault
{
$document = Document::fromXmlString($fault);
$documentElement = $document->locateDocumentElement();
$envelopeUri = $documentElement->namespaceURI;
invariant($envelopeUri !== null, 'No SoapFault envelope namespace uri was specified.');
$xpath = $document->xpath(namespaces(['env' => $envelopeUri]));
$actor = $xpath->query('./faultactor');
$detail = $xpath->query('./detail');
return new Soap11Fault(
faultCode: WhitespaceRestriction::collapse($xpath->querySingle('./faultcode')->textContent ?? ''),
faultString: WhitespaceRestriction::collapse($xpath->querySingle('./faultstring')->textContent ?? ''),
faultActor: $actor->count() ? trim($actor->expectFirst()->textContent ?? '') : null,
detail: $detail->count() ? Document::fromXmlNode($detail->expectFirst())->stringifyDocumentElement() : null,
);
}
}