1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of byrokrat\giroapp. |
4
|
|
|
* |
5
|
|
|
* byrokrat\giroapp is free software: you can redistribute it and/or |
6
|
|
|
* modify it under the terms of the GNU General Public License as published |
7
|
|
|
* by the Free Software Foundation, either version 3 of the License, or |
8
|
|
|
* (at your option) any later version. |
9
|
|
|
* |
10
|
|
|
* byrokrat\giroapp is distributed in the hope that it will be useful, |
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13
|
|
|
* GNU General Public License for more details. |
14
|
|
|
* |
15
|
|
|
* You should have received a copy of the GNU General Public License |
16
|
|
|
* along with byrokrat\giroapp. If not, see <http://www.gnu.org/licenses/>. |
17
|
|
|
* |
18
|
|
|
* Copyright 2016-18 Hannes Forsgård |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
declare(strict_types = 1); |
22
|
|
|
|
23
|
|
|
namespace byrokrat\giroapp\Xml; |
24
|
|
|
|
25
|
|
|
use byrokrat\giroapp\Builder\DonorBuilder; |
26
|
|
|
use byrokrat\giroapp\Exception\InvalidXmlFormException; |
27
|
|
|
use byrokrat\amount\Currency\SEK; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Write data to donor builder based on form translations |
31
|
|
|
*/ |
32
|
|
|
class XmlFormTranslator |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @var XmlFormInterface[] |
36
|
|
|
*/ |
37
|
|
|
private $xmlForms = []; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
private $createdMaps = []; |
43
|
|
|
|
44
|
|
|
public function addXmlForm(XmlFormInterface $xmlForm) |
45
|
|
|
{ |
46
|
|
|
$this->xmlForms[] = $xmlForm; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Write translated value to donorBuilder |
51
|
|
|
*/ |
52
|
|
|
public function writeValue(DonorBuilder $donorBuilder, string $formId, string $key, string $value): void |
53
|
|
|
{ |
54
|
|
|
$map = $this->getMap($formId); |
55
|
|
|
isset($map[$key]) |
56
|
|
|
? $map[$key]($donorBuilder, $value) |
57
|
|
|
: $donorBuilder->setAttribute($key, $value); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Get cached map for requested form |
62
|
|
|
*/ |
63
|
|
|
private function getMap(string $formId): array |
64
|
|
|
{ |
65
|
|
|
if (!isset($this->createdMaps[$formId])) { |
66
|
|
|
$this->createdMaps[$formId] = $this->buildMap($formId); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $this->createdMaps[$formId]; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @throws InvalidXmlFormException if map contains invalid value |
74
|
|
|
*/ |
75
|
|
|
private function buildMap(string $formId): array |
76
|
|
|
{ |
77
|
|
|
$map = []; |
78
|
|
|
$translations = []; |
79
|
|
|
|
80
|
|
|
foreach ($this->xmlForms as $xmlForm) { |
81
|
|
|
if ($xmlForm->getName() == $formId) { |
82
|
|
|
$translations = $xmlForm->getTranslations(); |
83
|
|
|
break; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
foreach ($translations as $key => $value) { |
88
|
|
|
if (is_callable($value)) { |
89
|
|
|
$map[$key] = $value; |
90
|
|
|
continue; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
switch ($value) { |
94
|
|
|
case XmlFormInterface::PHONE: |
95
|
|
|
$map[$key] = function ($donorBuilder, $value) { |
96
|
|
|
$donorBuilder->setPhone($value); |
97
|
|
|
}; |
98
|
|
|
break; |
99
|
|
|
case XmlFormInterface::EMAIL: |
100
|
|
|
$map[$key] = function ($donorBuilder, $value) { |
101
|
|
|
$donorBuilder->setEmail($value); |
102
|
|
|
}; |
103
|
|
|
break; |
104
|
|
|
case XmlFormInterface::DONATION_AMOUNT: |
105
|
|
|
$map[$key] = function ($donorBuilder, $value) { |
106
|
|
|
$donorBuilder->setDonationAmount(new SEK($value)); |
107
|
|
|
}; |
108
|
|
|
break; |
109
|
|
|
case XmlFormInterface::COMMENT: |
110
|
|
|
$map[$key] = function ($donorBuilder, $value) { |
111
|
|
|
$donorBuilder->setComment($value); |
112
|
|
|
}; |
113
|
|
|
break; |
114
|
|
|
default: |
115
|
|
|
throw new InvalidXmlFormException("Invalid translation for key $key"); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $map; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|