1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace h4kuna\Fio\Request\Pay; |
4
|
|
|
|
5
|
|
|
use h4kuna\Fio\Exceptions\InvalidState; |
6
|
|
|
use h4kuna\Fio\Request\Pay\Payment; |
7
|
|
|
use XMLWriter; |
8
|
|
|
|
9
|
|
|
class XMLFile |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
/** @var XMLWriter|null */ |
13
|
|
|
private $xml; |
14
|
|
|
|
15
|
|
|
/** @var string */ |
16
|
|
|
private $temp; |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
public function __construct(string $temp) |
20
|
|
|
{ |
21
|
|
|
$this->temp = $temp; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
public function setData(Payment\Property $data): self |
26
|
|
|
{ |
27
|
|
|
if ($this->isReady() === false) { |
28
|
|
|
$this->createEmptyXml(); |
29
|
|
|
} |
30
|
|
|
return $this->setBody($data); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
|
34
|
|
|
public function getPathname(bool $keepFile = false): string |
35
|
|
|
{ |
36
|
|
|
$filename = $this->temp . DIRECTORY_SEPARATOR . md5((string) microtime(true)) . '.xml'; |
37
|
|
|
file_put_contents($filename, $this->getXml()); |
38
|
|
|
if ($keepFile === false) { |
39
|
|
|
register_shutdown_function(function () use ($filename) { |
40
|
|
|
@unlink($filename); |
|
|
|
|
41
|
|
|
}); |
42
|
|
|
} |
43
|
|
|
return $filename; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
public function getXml(): string |
48
|
|
|
{ |
49
|
|
|
if ($this->isReady() === false) { |
50
|
|
|
throw new InvalidState('You can read only onetime.'); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return $this->endDocument(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
public function isReady(): bool |
58
|
|
|
{ |
59
|
|
|
return $this->xml !== null; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
private function createEmptyXml(): void |
64
|
|
|
{ |
65
|
|
|
$this->xml = new XMLWriter; |
66
|
|
|
$this->xml->openMemory(); |
67
|
|
|
$this->xml->startDocument('1.0', 'UTF-8'); |
68
|
|
|
$this->xml->startElement('Import'); |
69
|
|
|
$this->xml->writeAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance'); |
70
|
|
|
$this->xml->writeAttribute('xsi:noNamespaceSchemaLocation', 'http://www.fio.cz/schema/importIB.xsd'); |
71
|
|
|
$this->xml->startElement('Orders'); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
private function setBody(Payment\Property $data): self |
76
|
|
|
{ |
77
|
|
|
if ($this->xml === null) { |
78
|
|
|
throw self::exceptionFirstCallSetData(); |
79
|
|
|
} |
80
|
|
|
$elements = $data->getExpectedProperty(); |
81
|
|
|
$this->xml->startElement($data->getStartXmlElement()); |
82
|
|
|
foreach ($data as $node => $value) { |
83
|
|
|
if ($value == false) { // intentionally == |
84
|
|
|
if ($elements[$node] === false) { |
85
|
|
|
continue; |
86
|
|
|
} |
87
|
|
|
$value = ''; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$this->xml->startElement($node); |
91
|
|
|
$this->xml->text((string) $value); |
92
|
|
|
$this->xml->endElement(); |
93
|
|
|
} |
94
|
|
|
$this->xml->endElement(); |
95
|
|
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
|
99
|
|
|
private function endDocument(): string |
100
|
|
|
{ |
101
|
|
|
if ($this->xml === null) { |
102
|
|
|
throw self::exceptionFirstCallSetData(); |
103
|
|
|
} |
104
|
|
|
$this->xml->endDocument(); |
105
|
|
|
$xml = $this->xml->outputMemory(); |
106
|
|
|
$this->xml = null; |
107
|
|
|
return $xml; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
|
111
|
|
|
private static function exceptionFirstCallSetData(): InvalidState |
112
|
|
|
{ |
113
|
|
|
return new InvalidState('First you must call setData().'); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: