1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* ReportingCloud PHP Wrapper |
6
|
|
|
* |
7
|
|
|
* PHP wrapper for ReportingCloud Web API. Authored and supported by Text Control GmbH. |
8
|
|
|
* |
9
|
|
|
* @link https://www.reporting.cloud to learn more about ReportingCloud |
10
|
|
|
* @link https://github.com/TextControl/txtextcontrol-reportingcloud-php for the canonical source repository |
11
|
|
|
* @license https://raw.githubusercontent.com/TextControl/txtextcontrol-reportingcloud-php/master/LICENSE.md |
12
|
|
|
* @copyright © 2019 Text Control GmbH |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace TxTextControl\ReportingCloud; |
16
|
|
|
|
17
|
|
|
use TxTextControl\ReportingCloud\Assert\Assert; |
18
|
|
|
use TxTextControl\ReportingCloud\Filter\Filter; |
19
|
|
|
use TxTextControl\ReportingCloud\PropertyMap\AbstractPropertyMap as PropertyMap; |
20
|
|
|
use TxTextControl\ReportingCloud\PropertyMap\DocumentSettings as DocumentSettingsPropertyMap; |
21
|
|
|
use TxTextControl\ReportingCloud\PropertyMap\MergeSettings as MergeSettingsPropertyMap; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Trait BuildTrait |
25
|
|
|
* |
26
|
|
|
* @package TxTextControl\ReportingCloud |
27
|
|
|
* @author Jonathan Maron (@JonathanMaron) |
28
|
|
|
*/ |
29
|
|
|
trait BuildTrait |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* Build Methods |
33
|
|
|
* ----------------------------------------------------------------------------------------------------------------- |
34
|
|
|
*/ |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Using the passed propertyMap, recursively build array |
38
|
|
|
* |
39
|
|
|
* @param array $array Array |
40
|
|
|
* @param PropertyMap $propertyMap PropertyMap |
41
|
|
|
* |
42
|
|
|
* @return array |
43
|
|
|
*/ |
44
|
7 |
|
protected function buildPropertyMapArray(array $array, PropertyMap $propertyMap): array |
45
|
|
|
{ |
46
|
7 |
|
$ret = []; |
47
|
|
|
|
48
|
7 |
|
foreach ($array as $key => $value) { |
49
|
7 |
|
$map = $propertyMap->getMap(); |
50
|
7 |
|
if (isset($map[$key])) { |
51
|
7 |
|
$key = $map[$key]; |
52
|
|
|
} |
53
|
7 |
|
if (is_array($value)) { |
54
|
3 |
|
$value = $this->buildPropertyMapArray($value, $propertyMap); |
55
|
|
|
} |
56
|
7 |
|
$ret[$key] = $value; |
57
|
|
|
} |
58
|
|
|
|
59
|
7 |
|
return $ret; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Using passed documentsData array, build array for backend |
64
|
|
|
* |
65
|
|
|
* @param array $array |
66
|
|
|
* |
67
|
|
|
* @return array |
68
|
|
|
* @throws TxTextControl\ReportingCloud\Exception\InvalidArgumentException |
69
|
|
|
*/ |
70
|
1 |
|
protected function buildDocumentsArray(array $array): array |
71
|
|
|
{ |
72
|
1 |
|
$ret = []; |
73
|
|
|
|
74
|
1 |
|
foreach ($array as $inner) { |
75
|
1 |
|
Assert::isArray($inner); |
76
|
1 |
|
$document = []; |
77
|
1 |
|
foreach ($inner as $key => $value) { |
78
|
|
|
switch ($key) { |
79
|
1 |
|
case 'filename': |
80
|
1 |
|
Assert::filenameExists($value); |
81
|
1 |
|
Assert::assertDocumentExtension($value); |
82
|
1 |
|
$filename = realpath($value); |
83
|
1 |
|
$data = file_get_contents($filename); |
84
|
1 |
|
$data = base64_encode($data); |
85
|
1 |
|
$document['document'] = $data; |
86
|
1 |
|
break; |
87
|
1 |
|
case 'divider': |
88
|
1 |
|
Assert::assertDocumentDivider($value); |
89
|
1 |
|
$document['documentDivider'] = $value; |
90
|
1 |
|
break; |
91
|
|
|
} |
92
|
|
|
} |
93
|
1 |
|
$ret[] = $document; |
94
|
|
|
} |
95
|
|
|
|
96
|
1 |
|
return $ret; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Using passed documentsSettings array, build array for backend |
101
|
|
|
* |
102
|
|
|
* @param array $array |
103
|
|
|
* |
104
|
|
|
* @return array |
105
|
|
|
* @throws TxTextControl\ReportingCloud\Exception\InvalidArgumentException |
106
|
|
|
* @throws \Exception |
107
|
|
|
*/ |
108
|
1 |
|
protected function buildDocumentSettingsArray(array $array): array |
109
|
|
|
{ |
110
|
1 |
|
$ret = []; |
111
|
|
|
|
112
|
1 |
|
$propertyMap = new DocumentSettingsPropertyMap(); |
113
|
|
|
|
114
|
1 |
|
foreach ($propertyMap->getMap() as $property => $key) { |
115
|
1 |
|
if (isset($array[$key])) { |
116
|
1 |
|
$value = $array[$key]; |
117
|
1 |
|
if ($this->endsWith($key, '_date')) { |
118
|
1 |
|
Assert::assertTimestamp($value); |
119
|
1 |
|
$value = Filter::filterTimestampToDateTime($value); |
120
|
|
|
} |
121
|
1 |
|
$ret[$property] = $value; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
1 |
|
return $ret; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Using passed mergeSettings array, build array for backend |
130
|
|
|
* |
131
|
|
|
* @param array $array MergeSettings array |
132
|
|
|
* |
133
|
|
|
* @return array |
134
|
|
|
* @throws TxTextControl\ReportingCloud\Exception\InvalidArgumentException |
135
|
|
|
* @throws \Exception |
136
|
|
|
*/ |
137
|
9 |
|
protected function buildMergeSettingsArray(array $array): array |
138
|
|
|
{ |
139
|
9 |
|
$ret = []; |
140
|
|
|
|
141
|
9 |
|
$propertyMap = new MergeSettingsPropertyMap(); |
142
|
|
|
|
143
|
9 |
|
foreach ($propertyMap->getMap() as $property => $key) { |
144
|
9 |
|
if (!isset($array[$key])) { |
145
|
6 |
|
continue; |
146
|
|
|
} |
147
|
9 |
|
$value = $array[$key]; |
148
|
9 |
|
if ('culture' === $key) { |
149
|
1 |
|
Assert::assertCulture($value); |
150
|
|
|
} |
151
|
9 |
|
if ($this->startsWith($key, 'remove_')) { |
152
|
6 |
|
Assert::boolean($value); |
153
|
|
|
} |
154
|
9 |
|
if ($this->endsWith($key, '_date')) { |
155
|
9 |
|
Assert::assertTimestamp($value); |
156
|
7 |
|
$value = Filter::filterTimestampToDateTime($value); |
157
|
|
|
} |
158
|
9 |
|
$ret[$property] = $value; |
159
|
|
|
} |
160
|
|
|
|
161
|
4 |
|
return $ret; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Using passed findAndReplaceData associative array (key-value), build array for backend (list of string arrays) |
166
|
|
|
* |
167
|
|
|
* @param array $array |
168
|
|
|
* |
169
|
|
|
* @return array |
170
|
|
|
*/ |
171
|
10 |
|
protected function buildFindAndReplaceDataArray(array $array): array |
172
|
|
|
{ |
173
|
10 |
|
$ret = []; |
174
|
|
|
|
175
|
10 |
|
foreach ($array as $key => $value) { |
176
|
10 |
|
$ret[] = [ |
177
|
10 |
|
$key, |
178
|
10 |
|
$value, |
179
|
|
|
]; |
180
|
|
|
} |
181
|
|
|
|
182
|
10 |
|
return $ret; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Return true, if needle is at the beginning of haystack |
187
|
|
|
* |
188
|
|
|
* @param string $haystack |
189
|
|
|
* @param string $needle |
190
|
|
|
* |
191
|
|
|
* @return bool |
192
|
|
|
*/ |
193
|
9 |
|
protected function startsWith(string $haystack, string $needle): bool |
194
|
|
|
{ |
195
|
9 |
|
$len = strlen($needle); |
196
|
|
|
|
197
|
9 |
|
return ($needle === substr($haystack, 0, $len)); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Return true, if needle is at the end of haystack |
202
|
|
|
* |
203
|
|
|
* @param string $haystack |
204
|
|
|
* @param string $needle |
205
|
|
|
* |
206
|
|
|
* @return bool |
207
|
|
|
*/ |
208
|
10 |
|
protected function endsWith(string $haystack, string $needle): bool |
209
|
|
|
{ |
210
|
10 |
|
$len = strlen($needle); |
211
|
|
|
|
212
|
10 |
|
return ($needle === substr($haystack, -$len)); |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|