-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathContextXmlRenderer.php
166 lines (140 loc) · 5.17 KB
/
ContextXmlRenderer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
namespace AqBanking;
use AqBanking\ContentXmlRenderer\MoneyElementRenderer;
use Money\Currency;
use Money\Money;
class ContextXmlRenderer
{
/**
* @var \DOMDocument
*/
private $domDocument;
/**
* @var \DOMXPath
*/
private $xPath;
/**
* @var MoneyElementRenderer
*/
private $moneyElementRenderer;
public function __construct(\DOMDocument $domDocument)
{
$this->domDocument = $domDocument;
$this->xPath = new \DOMXPath($domDocument);
$this->moneyElementRenderer = new MoneyElementRenderer();
}
/**
* @return Transaction[]
*/
public function getTransactions()
{
$transactionNodes = $this->domDocument->getElementsByTagName('transaction');
$transactions = array();
foreach ($transactionNodes as $transactionNode) {
$localBankCode = $this->renderMultiLineElement(
$this->xPath->query('localBankCode/value', $transactionNode)
);
$localAccountNumber = $this->renderMultiLineElement(
$this->xPath->query('localAccountNumber/value', $transactionNode)
);
$localName = $this->renderMultiLineElement($this->xPath->query('localName/value', $transactionNode));
$remoteBankCode = $this->renderMultiLineElement(
$this->xPath->query('remoteBankCode/value', $transactionNode)
);
$remoteAccountNumber = $this->renderMultiLineElement(
$this->xPath->query('remoteAccountNumber/value', $transactionNode)
);
$remoteName = $this->renderMultiLineElement($this->xPath->query('remoteName/value', $transactionNode));
$purpose = $this->renderMultiLineElement($this->xPath->query('purpose/value', $transactionNode));
$valutaDate = $this->renderDateElement($this->xPath->query('valutaDate', $transactionNode)->item(0));
$date = $this->renderDateElement($this->xPath->query('date', $transactionNode)->item(0));
$value = $this->renderMoneyElement($this->xPath->query('value', $transactionNode)->item(0));
$transactions[] = new Transaction(
new Account(new BankCode($localBankCode), $localAccountNumber, $localName),
new Account(new BankCode($remoteBankCode), $remoteAccountNumber, $remoteName),
$purpose,
$valutaDate,
$date,
$value
);
}
return $transactions;
}
/**
* @return Money
*/
public function getBalance()
{
$statusNode = $this->domDocument->getElementsByTagName('bookedBalance')->item(0);
return $this->renderMoneyElement(
$this->xPath->query('value', $statusNode)->item(0)
);
}
/**
* @param \DOMNodeList $nodes
* @throws \RuntimeException
* @return string
*/
private function renderMultiLineElement(\DOMNodeList $nodes)
{
$lines = array();
foreach ($nodes as $node) {
$line = trim($node->nodeValue);
if (false !== strpos($line, '|')) {
throw new \RuntimeException('Unexpected character');
}
$lines[] = $line;
}
return implode('|', $lines);
}
/**
* @param \DOMNode $node
* @param \DOMNode $node
* @throws \RuntimeException
* @return \DateTime
*/
private function renderDateElement(\DOMNode $node)
{
$utcFlagElement = $this->xPath->query('inUtc', $node)->item(0);
if ('1' !== trim($utcFlagElement->nodeValue)) {
throw new \RuntimeException('Unexpected input');
}
$dayElement = $this->xPath->query('date/day/value', $node)->item(0);
$monthElement = $this->xPath->query('date/month/value', $node)->item(0);
$yearElement = $this->xPath->query('date/year/value', $node)->item(0);
$hourElement = $this->xPath->query('time/hour/value', $node)->item(0);
$minuteElement = $this->xPath->query('time/min/value', $node)->item(0);
$secondElement = $this->xPath->query('time/sec/value', $node)->item(0);
$date = new \DateTime('today', new \DateTimeZone('UTC'));
$date->setDate(
(int)$yearElement->nodeValue,
(int)$monthElement->nodeValue,
(int)$dayElement->nodeValue
);
$date->setTime(
(int)$hourElement->nodeValue,
(int)$minuteElement->nodeValue,
(int)$secondElement->nodeValue
);
return $date;
}
/**
* @param \DOMNode $node
* @return Money
* @throws \Exception
*/
private function renderMoneyElement(\DOMNode $node)
{
$valueString = $this->renderSimpleTextElement($this->xPath->query('value/value', $node));
$currencyString = $this->renderSimpleTextElement($this->xPath->query('currency/value', $node));
return $this->moneyElementRenderer->render($valueString, $currencyString);
}
/**
* @param \DOMNodeList $valueNodes
* @return string
*/
private function renderSimpleTextElement(\DOMNodeList $valueNodes)
{
return trim($valueNodes->item(0)->nodeValue);
}
}