1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace h4kuna\Fio\Response\Pay; |
4
|
|
|
|
5
|
|
|
use SimpleXMLElement; |
6
|
|
|
|
7
|
|
|
class XMLResponse implements IResponse |
8
|
|
|
{ |
9
|
|
|
|
10
|
|
|
/** @var SimpleXMLElement */ |
11
|
|
|
private $xml; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
public function __construct(string $xml) |
15
|
|
|
{ |
16
|
|
|
$this->xml = new SimpleXMLElement($xml); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
public function isOk(): bool |
21
|
|
|
{ |
22
|
|
|
return $this->status() === 'ok' && $this->code() === 0; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* READ XML **************************************************************** |
28
|
|
|
* ************************************************************************* |
29
|
|
|
*/ |
30
|
|
|
|
31
|
|
|
public function getXml(): SimpleXMLElement |
32
|
|
|
{ |
33
|
|
|
return $this->xml; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
public function code(): int |
38
|
|
|
{ |
39
|
|
|
return (int) $this->getValue('result/errorCode'); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
public function getIdInstruction() |
44
|
|
|
{ |
45
|
|
|
return $this->getValue('result/idInstruction'); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
public function status(): string |
50
|
|
|
{ |
51
|
|
|
return $this->getValue('result/status'); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
|
55
|
|
|
public function errorMessages(): array |
56
|
|
|
{ |
57
|
|
|
$errorMessages = []; |
58
|
|
|
/** @var SimpleXMLElement[] $messages */ |
59
|
|
|
$messages = $this->getXml()->xpath('ordersDetails/detail/messages'); |
60
|
|
|
foreach ($messages as $message) { |
61
|
|
|
foreach ($message as $item) { |
62
|
|
|
if (isset($item['errorCode'])) { |
63
|
|
|
$errorMessages[(string) $item['errorCode']] = (string) $item; |
64
|
|
|
} else { |
65
|
|
|
$errorMessages[] = (string) $item; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
return $errorMessages; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
private function getValue(string $path): string |
74
|
|
|
{ |
75
|
|
|
$val = $this->getXml()->xpath($path . '/text()'); |
76
|
|
|
return (string) $val[0]; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
|
80
|
|
|
public function saveXML(string $fileName): void |
81
|
|
|
{ |
82
|
|
|
$this->getXml()->saveXML($fileName); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
public function __toString() |
87
|
|
|
{ |
88
|
|
|
return (string) $this->xml->asXML(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
} |
92
|
|
|
|