|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace OrcaServices\NovaApi\Parser; |
|
4
|
|
|
|
|
5
|
|
|
use DOMNode; |
|
6
|
|
|
use OrcaServices\NovaApi\Result\NovaMessage; |
|
7
|
|
|
use OrcaServices\NovaApi\Xml\XmlDocument; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class. |
|
11
|
|
|
*/ |
|
12
|
|
|
final class NovaMessageParser |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* Get spring validation errors. |
|
16
|
|
|
* |
|
17
|
|
|
* @param XmlDocument $xml The xml document |
|
18
|
|
|
* |
|
19
|
|
|
* @return NovaMessage[] The nova validation error messages and codes |
|
20
|
|
|
*/ |
|
21
|
13 |
|
public function findNovaMessages(XmlDocument $xml): array |
|
22
|
|
|
{ |
|
23
|
13 |
|
$messages = []; |
|
24
|
|
|
|
|
25
|
|
|
// Possible namespace combinations where to find messages |
|
26
|
|
|
$namespaces = [ |
|
27
|
13 |
|
['', ''], |
|
28
|
|
|
['ns13', 'base'], |
|
29
|
|
|
['ns1', 'ns2'], |
|
30
|
|
|
['xmlns', 'base'], |
|
31
|
|
|
['novasp-swisspass', 'base'], |
|
32
|
|
|
]; |
|
33
|
|
|
|
|
34
|
13 |
|
foreach ($namespaces as $namespace) { |
|
35
|
13 |
|
$messages = $this->appendNovaMessages($xml, $namespace, $messages); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
13 |
|
return $messages; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Add nova messages to array. |
|
43
|
|
|
* |
|
44
|
|
|
* @param XmlDocument $xml The xml document |
|
45
|
|
|
* @param string[] $namespace The namespace |
|
46
|
|
|
* @param array $messages The messages |
|
47
|
|
|
* |
|
48
|
|
|
* @return array The new messages |
|
49
|
|
|
*/ |
|
50
|
13 |
|
private function appendNovaMessages(XmlDocument $xml, array $namespace, array $messages): array |
|
51
|
|
|
{ |
|
52
|
13 |
|
$ns1 = $namespace[0]; |
|
53
|
13 |
|
$ns2 = $namespace[1]; |
|
54
|
|
|
|
|
55
|
13 |
|
if ($ns1 && !$xml->existsNamespace($ns1)) { |
|
56
|
13 |
|
return $messages; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
13 |
|
$ns1 = $ns1 ? $ns1 . ':' : $ns1; |
|
60
|
13 |
|
$ns2 = $ns2 ? $ns2 . ':' : $ns2; |
|
61
|
|
|
|
|
62
|
|
|
// Check for action response errors |
|
63
|
13 |
|
$messageNodes = $xml->queryNodes(sprintf('//%smeldungen/%smeldung', $ns1, $ns2)); |
|
64
|
|
|
|
|
65
|
13 |
|
if ($messageNodes->length === 0) { |
|
66
|
11 |
|
return $messages; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** @var DOMNode $messageNode */ |
|
70
|
3 |
|
foreach ($messageNodes as $messageNode) { |
|
71
|
3 |
|
$message = new NovaMessage(); |
|
72
|
|
|
|
|
73
|
3 |
|
$value = $xml->findAttributeValue(sprintf('@%smeldungsCode', $ns2), $messageNode); |
|
74
|
3 |
|
if ($value) { |
|
75
|
3 |
|
$message->code = $value; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
3 |
|
$value = $xml->findAttributeValue( |
|
79
|
3 |
|
sprintf('//%sbeschreibung/@%sdefaultWert', $ns2, $ns2), |
|
80
|
3 |
|
$messageNode |
|
81
|
|
|
); |
|
82
|
3 |
|
if ($value) { |
|
83
|
3 |
|
$message->message = $value; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
3 |
|
$value = $xml->findAttributeValue(sprintf('@%sid', $ns2), $messageNode); |
|
87
|
3 |
|
if ($value) { |
|
88
|
2 |
|
$message->id = $value; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
3 |
|
$value = $xml->findAttributeValue(sprintf('@%styp', $ns2), $messageNode); |
|
92
|
3 |
|
if ($value) { |
|
93
|
3 |
|
$message->type = $value; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
3 |
|
$value = $xml->findAttributeValue(sprintf('@%szeitStempel', $ns2), $messageNode); |
|
97
|
3 |
|
if ($value) { |
|
98
|
3 |
|
$message->timestamp = $value; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
3 |
|
$value = $xml->findAttributeValue(sprintf('@%sendKundenRelevant', $ns2), $messageNode); |
|
102
|
3 |
|
if ($value) { |
|
103
|
3 |
|
$message->customerRelevant = $value; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
3 |
|
$messages[] = $message; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
3 |
|
return $messages; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|