1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* ReportingCloud PHP Wrapper |
5
|
|
|
* |
6
|
|
|
* PHP wrapper for ReportingCloud Web API. Authored and supported by Text Control GmbH. |
7
|
|
|
* |
8
|
|
|
* @link http://www.reporting.cloud to learn more about ReportingCloud |
9
|
|
|
* @link https://github.com/TextControl/txtextcontrol-reportingcloud-php for the canonical source repository |
10
|
|
|
* @license https://raw.githubusercontent.com/TextControl/txtextcontrol-reportingcloud-php/master/LICENSE.md |
11
|
|
|
* @copyright © 2018 Text Control GmbH |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace TxTextControl\ReportingCloud; |
15
|
|
|
|
16
|
|
|
use GuzzleHttp\RequestOptions; |
17
|
|
|
use TxTextControl\ReportingCloud\Exception\RuntimeException; |
18
|
|
|
use TxTextControl\ReportingCloud\Filter\StaticFilter; |
19
|
|
|
use TxTextControl\ReportingCloud\PropertyMap\AbstractPropertyMap as PropertyMap; |
20
|
|
|
use TxTextControl\ReportingCloud\PropertyMap\MergeSettings as MergeSettingsPropertyMap; |
21
|
|
|
use TxTextControl\ReportingCloud\Validator\StaticValidator; |
22
|
|
|
|
23
|
|
|
trait UtilityTrait |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Return the REST client of the backend web service |
27
|
|
|
* |
28
|
|
|
* @return \GuzzleHttp\Client |
29
|
|
|
*/ |
30
|
|
|
abstract public function getClient(); |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Return the test flag |
34
|
|
|
* |
35
|
|
|
* @return mixed |
36
|
|
|
*/ |
37
|
|
|
abstract public function getTest(); |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Get the version string of the backend web service |
41
|
|
|
* |
42
|
|
|
* @return string |
43
|
|
|
*/ |
44
|
|
|
abstract public function getVersion(); |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Request the URI with options |
48
|
|
|
* |
49
|
|
|
* @param string $method HTTP method |
50
|
|
|
* @param string $uri URI |
51
|
|
|
* @param array $options Options |
52
|
|
|
* |
53
|
|
|
* @return mixed|null|\Psr\Http\Message\ResponseInterface |
54
|
|
|
* |
55
|
|
|
* @throws RuntimeException |
56
|
|
|
*/ |
57
|
46 |
|
protected function request($method, $uri, $options) |
58
|
|
|
{ |
59
|
46 |
|
$ret = null; |
|
|
|
|
60
|
|
|
|
61
|
46 |
|
$client = $this->getClient(); |
62
|
|
|
|
63
|
|
|
try { |
64
|
44 |
|
if ($this->getTest()) { |
65
|
2 |
|
$options[RequestOptions::QUERY]['test'] = StaticFilter::execute($this->getTest(), 'BooleanToString'); |
66
|
1 |
|
} |
67
|
44 |
|
$ret = $client->request($method, $uri, $options); |
68
|
24 |
|
} catch (\Exception $exception) { |
69
|
|
|
// \GuzzleHttp\Exception\ClientException |
70
|
|
|
// \GuzzleHttp\Exception\ServerException |
71
|
4 |
|
$message = (string) $exception->getMessage(); |
72
|
4 |
|
$code = (int) $exception->getCode(); |
73
|
4 |
|
throw new RuntimeException($message, $code); |
74
|
|
|
} |
75
|
|
|
|
76
|
42 |
|
return $ret; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Construct URI with version number |
81
|
|
|
* |
82
|
|
|
* @param string $uri URI |
83
|
|
|
* |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
46 |
|
protected function uri($uri) |
87
|
|
|
{ |
88
|
46 |
|
return sprintf('/%s%s', $this->getVersion(), $uri); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Using the passed propertyMap, recursively build array |
93
|
|
|
* |
94
|
|
|
* @param array $array Array |
95
|
|
|
* @param PropertyMap $propertyMap PropertyMap |
96
|
|
|
* |
97
|
|
|
* @return array |
98
|
|
|
*/ |
99
|
14 |
|
protected function buildPropertyMapArray(array $array, PropertyMap $propertyMap) |
100
|
|
|
{ |
101
|
14 |
|
$ret = []; |
102
|
|
|
|
103
|
14 |
|
foreach ($array as $key => $value) { |
104
|
14 |
|
$map = $propertyMap->getMap(); |
105
|
14 |
|
if (isset($map[$key])) { |
106
|
14 |
|
$key = $map[$key]; |
107
|
7 |
|
} |
108
|
14 |
|
if (is_array($value)) { |
109
|
6 |
|
$value = $this->buildPropertyMapArray($value, $propertyMap); |
110
|
3 |
|
} |
111
|
14 |
|
$ret[$key] = $value; |
112
|
7 |
|
} |
113
|
|
|
|
114
|
14 |
|
return $ret; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Using passed mergeSettings array, build array for backend |
119
|
|
|
* |
120
|
|
|
* @param array $array MergeSettings array |
121
|
|
|
* |
122
|
|
|
* @return array |
123
|
|
|
*/ |
124
|
18 |
|
protected function buildMergeSettingsArray(array $array) |
125
|
|
|
{ |
126
|
18 |
|
$ret = []; |
127
|
|
|
|
128
|
18 |
|
$propertyMap = new MergeSettingsPropertyMap(); |
129
|
|
|
|
130
|
18 |
|
foreach ($propertyMap->getMap() as $property => $key) { |
131
|
18 |
|
if (isset($array[$key])) { |
132
|
18 |
|
$value = $array[$key]; |
133
|
18 |
|
if ('culture' == $key) { |
134
|
2 |
|
StaticValidator::execute($value, 'Culture'); |
135
|
|
|
} |
136
|
18 |
|
if ('remove_' == substr($key, 0, 7)) { |
137
|
12 |
|
StaticValidator::execute($value, 'TypeBoolean'); |
138
|
4 |
|
} |
139
|
18 |
|
if ('_date' == substr($key, -5)) { |
140
|
18 |
|
StaticValidator::execute($value, 'Timestamp'); |
141
|
14 |
|
$value = StaticFilter::execute($value, 'TimestampToDateTime'); |
142
|
7 |
|
} |
143
|
18 |
|
$ret[$property] = $value; |
144
|
9 |
|
} |
145
|
9 |
|
} |
146
|
|
|
|
147
|
8 |
|
return $ret; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Using passed findAndReplaceData associative array (key-value), build array for backend (list of string arrays) |
152
|
|
|
* |
153
|
|
|
* @param array $array FindAndReplaceData array |
154
|
|
|
* |
155
|
|
|
* @return array |
156
|
|
|
*/ |
157
|
8 |
|
protected function buildFindAndReplaceDataArray(array $array) |
158
|
|
|
{ |
159
|
8 |
|
$ret = []; |
160
|
|
|
|
161
|
8 |
|
foreach ($array as $search => $replace) { |
162
|
8 |
|
array_push($ret, [ |
163
|
8 |
|
$search, |
164
|
8 |
|
$replace, |
165
|
4 |
|
]); |
166
|
4 |
|
} |
167
|
|
|
|
168
|
8 |
|
return $ret; |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|