|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Raigu\XRoad\SoapEnvelope; |
|
4
|
|
|
|
|
5
|
|
|
use Raigu\XRoad\SoapEnvelope\Element\DeferredFragmentInjection; |
|
6
|
|
|
use Raigu\XRoad\SoapEnvelope\Element\DOMElementInjection; |
|
7
|
|
|
use Raigu\XRoad\SoapEnvelope\Element\FragmentInjection; |
|
8
|
|
|
use Raigu\XRoad\SoapEnvelope\Element\UnInitialized; |
|
9
|
|
|
|
|
10
|
|
|
final class SoapEnvelopeBuilder |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var array |
|
14
|
|
|
*/ |
|
15
|
|
|
private $elements; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Clone builder and replace service in SOAP header |
|
19
|
|
|
* |
|
20
|
|
|
* @param string $service encoded service. |
|
21
|
|
|
* Format: {xRoadInstance}/{memberClass/{memberCode}/{subsystemCode}/{serviceCode}/{serviceVerson} |
|
22
|
|
|
* Example: EE/GOV/70000310/DHX.Riigi-Teataja/sendDocument/v1 |
|
23
|
|
|
* @return self cloned builder with overwritten service data |
|
24
|
|
|
*/ |
|
25
|
7 |
|
public function withService(string $service): self |
|
26
|
|
|
{ |
|
27
|
7 |
|
$elements = $this->elements; |
|
28
|
7 |
|
$elements['service'] = (new ServiceFactory())->fromStr($service); |
|
29
|
|
|
|
|
30
|
7 |
|
return new self($elements); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Clone builder and replace client in SOAP header |
|
35
|
|
|
* |
|
36
|
|
|
* @param string $client encoded client. |
|
37
|
|
|
* Format: {xRoadInstance}/{memberClass/{memberCode}/{subsystemCode} |
|
38
|
|
|
* Example: EE/COM/00000000/sys |
|
39
|
|
|
* @return self cloned builder with overwritten client data |
|
40
|
|
|
*/ |
|
41
|
7 |
|
public function withClient(string $client): self |
|
42
|
|
|
{ |
|
43
|
7 |
|
$elements = $this->elements; |
|
44
|
7 |
|
$elements['client'] = (new ClientFactory)->fromStr($client); |
|
45
|
|
|
|
|
46
|
7 |
|
return new self($elements); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Clone builder and replace userId in SOAP header |
|
51
|
|
|
* |
|
52
|
|
|
* @param string $userId the user who is making the request |
|
53
|
|
|
* Format: {iso2LetterCountryCode}{personCode} |
|
54
|
|
|
* Example: EE0000000000 |
|
55
|
|
|
* @return self cloned builder with overwritten client data |
|
56
|
|
|
*/ |
|
57
|
1 |
|
public function withUserId(string $userId): self |
|
58
|
|
|
{ |
|
59
|
1 |
|
$elements = $this->elements; |
|
60
|
1 |
|
$elements['userId'] = new DOMElementInjection( |
|
61
|
1 |
|
'http://schemas.xmlsoap.org/soap/envelope/', |
|
62
|
1 |
|
'Header', |
|
63
|
1 |
|
new \DOMElement( |
|
64
|
1 |
|
'userId', |
|
65
|
1 |
|
(new ValidatedUserId($userId))->asStr(), |
|
66
|
1 |
|
'http://x-road.eu/xsd/xroad.xsd' |
|
67
|
|
|
) |
|
68
|
|
|
); |
|
69
|
|
|
|
|
70
|
1 |
|
return new self($elements); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Clone builder and replace SOAP body |
|
75
|
|
|
* @param string $body content of SOAP Body tag. |
|
76
|
|
|
* @return self |
|
77
|
|
|
*/ |
|
78
|
7 |
|
public function withBody(string $body): self |
|
79
|
|
|
{ |
|
80
|
7 |
|
$elements = $this->elements; |
|
81
|
7 |
|
$elements['body'] = new FragmentInjection( |
|
82
|
7 |
|
'http://schemas.xmlsoap.org/soap/envelope/', |
|
83
|
7 |
|
'Body', |
|
84
|
7 |
|
$body |
|
85
|
|
|
); |
|
86
|
|
|
|
|
87
|
7 |
|
return new self($elements); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Clone builder and replace representedParty in SOAP header. |
|
92
|
|
|
* @see https://x-tee.ee/docs/live/xroad/pr-third_party_representation_extension.html |
|
93
|
|
|
* @param string $representedParty string representing representative party. format: [{partyClass}/]{partyCode} |
|
94
|
|
|
* String is concatenation of represented party class (optional) and code in separated by /. |
|
95
|
|
|
* @return $this |
|
96
|
|
|
*/ |
|
97
|
1 |
|
public function withRepresentedParty(string $representedParty): self |
|
98
|
|
|
{ |
|
99
|
1 |
|
$elements = $this->elements; |
|
100
|
1 |
|
$elements['representedParty'] = (new RepresentedPartyFactory)->fromStr($representedParty); |
|
101
|
|
|
|
|
102
|
1 |
|
return new self($elements); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
8 |
|
public function build(): string |
|
106
|
|
|
{ |
|
107
|
|
|
$envelope = <<<EOD |
|
108
|
8 |
|
<?xml version="1.0" encoding="UTF-8"?> |
|
109
|
|
|
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" |
|
110
|
|
|
xmlns:id="http://x-road.eu/xsd/identifiers" |
|
111
|
|
|
xmlns:xrd="http://x-road.eu/xsd/xroad.xsd"> |
|
112
|
|
|
<env:Header> |
|
113
|
|
|
<xrd:protocolVersion>4.0</xrd:protocolVersion> |
|
114
|
|
|
</env:Header> |
|
115
|
|
|
<env:Body/> |
|
116
|
|
|
</env:Envelope> |
|
117
|
|
|
EOD; |
|
118
|
|
|
|
|
119
|
8 |
|
$dom = new \DOMDocument(); |
|
120
|
8 |
|
$dom->loadXML($envelope); |
|
121
|
|
|
|
|
122
|
8 |
|
foreach ($this->elements as $element) { |
|
123
|
8 |
|
$element->inject($dom); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
7 |
|
return $dom->saveXML(); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
8 |
|
public static function create(): self |
|
130
|
|
|
{ |
|
131
|
8 |
|
return new self([ |
|
132
|
8 |
|
'service' => new UnInitialized('Service not initialized'), |
|
133
|
8 |
|
'client' => new UnInitialized('Client not initialized'), |
|
134
|
8 |
|
'body' => new UnInitialized('Body not initialized'), |
|
135
|
|
|
] |
|
136
|
|
|
); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
7 |
|
public static function stub(): self |
|
140
|
|
|
{ |
|
141
|
7 |
|
return self::create() |
|
142
|
7 |
|
->withService('EE/COM/11111111/PROVIDER_SYS/provider_method/v99') |
|
143
|
7 |
|
->withClient('EE/COM/22222222/CLIENT_SYS') |
|
144
|
7 |
|
->withBody('<stub xmlns="https://stub.ee"></stub>'); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
8 |
|
private function __construct(array $elements) |
|
148
|
|
|
{ |
|
149
|
8 |
|
$this->elements = $elements; |
|
150
|
8 |
|
$this->elements['id'] = new DeferredFragmentInjection( |
|
151
|
8 |
|
'http://schemas.xmlsoap.org/soap/envelope/', |
|
152
|
8 |
|
'Header', |
|
153
|
|
|
function (): string { |
|
154
|
7 |
|
return sprintf( |
|
155
|
7 |
|
'<xrd:id xmlns:xrd="http://x-road.eu/xsd/xroad.xsd">%s</xrd:id>', |
|
156
|
7 |
|
bin2hex(random_bytes(16)) |
|
157
|
|
|
); |
|
158
|
8 |
|
} |
|
159
|
|
|
); |
|
160
|
8 |
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|