|
1
|
|
|
<?php |
|
2
|
|
|
namespace Genkgo\Camt; |
|
3
|
|
|
|
|
4
|
|
|
use DOMDocument; |
|
5
|
|
|
use Genkgo\Camt\DTO\Message; |
|
6
|
|
|
use Genkgo\Camt\Exception\ReaderException; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class Reader |
|
10
|
|
|
* @package Genkgo\Camt |
|
11
|
|
|
*/ |
|
12
|
|
|
class Reader |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var Config |
|
16
|
|
|
*/ |
|
17
|
|
|
private $config; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var MessageFormatInterface |
|
21
|
|
|
*/ |
|
22
|
|
|
private $messageFormat; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param Config $config |
|
26
|
|
|
*/ |
|
27
|
3 |
|
public function __construct(Config $config) |
|
28
|
|
|
{ |
|
29
|
3 |
|
$this->config = $config; |
|
30
|
3 |
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param DOMDocument $document |
|
34
|
|
|
* @return mixed |
|
35
|
|
|
* @throws ReaderException |
|
36
|
|
|
*/ |
|
37
|
3 |
|
public function readDom(DOMDocument $document) |
|
38
|
|
|
{ |
|
39
|
3 |
|
if ($document->documentElement === null) { |
|
40
|
1 |
|
throw new ReaderException("Empty document"); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
2 |
|
$xmlNs = $document->documentElement->getAttribute('xmlns'); |
|
44
|
2 |
|
$this->messageFormat = $this->getMessageFormatForXmlNs($xmlNs); |
|
45
|
|
|
|
|
46
|
1 |
|
return $this->messageFormat->getDecoder()->decode($document); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param $string |
|
51
|
|
|
* @return mixed |
|
52
|
|
|
* @throws ReaderException |
|
53
|
|
|
*/ |
|
54
|
1 |
|
public function readString($string) |
|
55
|
|
|
{ |
|
56
|
1 |
|
$dom = new DOMDocument('1.0', 'UTF-8'); |
|
57
|
1 |
|
$dom->loadXML($string); |
|
58
|
|
|
|
|
59
|
1 |
|
return $this->readDom($dom); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function readDoc($string) |
|
63
|
|
|
{ |
|
64
|
|
|
$string = file_get_contents($string); |
|
65
|
|
|
$dom = new DOMDocument('1.0', 'UTF-8'); |
|
66
|
|
|
$dom->loadXML($string); |
|
67
|
|
|
|
|
68
|
|
|
return $dom; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param string $file |
|
74
|
|
|
* @return mixed|Message |
|
75
|
|
|
* @throws ReaderException |
|
76
|
|
|
*/ |
|
77
|
1 |
|
public function readFile($file) |
|
78
|
|
|
{ |
|
79
|
1 |
|
if (!file_exists($file)) { |
|
80
|
|
|
throw new ReaderException("{$file} does not exists"); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
1 |
|
return $this->readString(file_get_contents($file)); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param $xmlNs |
|
88
|
|
|
* @return MessageFormatInterface |
|
89
|
|
|
* @throws ReaderException |
|
90
|
|
|
*/ |
|
91
|
2 |
|
private function getMessageFormatForXmlNs($xmlNs) |
|
92
|
|
|
{ |
|
93
|
2 |
|
$messageFormats = $this->config->getMessageFormats(); |
|
94
|
2 |
|
foreach ($messageFormats as $messageFormat) { |
|
95
|
2 |
|
if ($messageFormat->getXmlNs() === $xmlNs) { |
|
96
|
2 |
|
return $messageFormat; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
1 |
|
throw new ReaderException("Unsupported format, cannot find message format with xmlns {$xmlNs}"); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @return MessageFormatInterface |
|
105
|
|
|
*/ |
|
106
|
|
|
public function getMessageFormat() |
|
107
|
|
|
{ |
|
108
|
|
|
return $this->messageFormat; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|