|
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
|
|
|
* Request the URI with options |
|
27
|
|
|
* |
|
28
|
|
|
* @param string $method HTTP method |
|
29
|
|
|
* @param string $uri URI |
|
30
|
|
|
* @param array $options Options |
|
31
|
|
|
* |
|
32
|
|
|
* @return mixed|null|\Psr\Http\Message\ResponseInterface |
|
33
|
|
|
* |
|
34
|
|
|
* @throws RuntimeException |
|
35
|
|
|
*/ |
|
36
|
19 |
|
protected function request($method, $uri, $options) |
|
37
|
|
|
{ |
|
38
|
19 |
|
$ret = null; |
|
39
|
|
|
|
|
40
|
19 |
|
$client = $this->getClient(); |
|
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
try { |
|
43
|
19 |
|
if ($this->getTest()) { |
|
|
|
|
|
|
44
|
|
|
$options[RequestOptions::QUERY]['test'] = StaticFilter::execute($this->getTest(), 'BooleanToString'); |
|
|
|
|
|
|
45
|
|
|
} |
|
46
|
19 |
|
$ret = $client->request($method, $uri, $options); |
|
47
|
19 |
|
} catch (\Exception $exception) { |
|
48
|
|
|
// \GuzzleHttp\Exception\ClientException |
|
49
|
|
|
// \GuzzleHttp\Exception\ServerException |
|
50
|
1 |
|
$message = (string) $exception->getMessage(); |
|
51
|
1 |
|
$code = (int) $exception->getCode(); |
|
52
|
1 |
|
throw new RuntimeException($message, $code); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
18 |
|
return $ret; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Construct URI with version number |
|
60
|
|
|
* |
|
61
|
|
|
* @param string $uri URI |
|
62
|
|
|
* |
|
63
|
|
|
* @return string |
|
64
|
|
|
*/ |
|
65
|
19 |
|
protected function uri($uri) |
|
66
|
|
|
{ |
|
67
|
19 |
|
return sprintf('/%s%s', $this->getVersion(), $uri); |
|
|
|
|
|
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Using the passed propertyMap, recursively build array |
|
72
|
|
|
* |
|
73
|
|
|
* @param array $array Array |
|
74
|
|
|
* @param PropertyMap $propertyMap PropertyMap |
|
75
|
|
|
* |
|
76
|
|
|
* @return array |
|
77
|
|
|
*/ |
|
78
|
4 |
|
protected function buildPropertyMapArray(array $array, PropertyMap $propertyMap) |
|
79
|
|
|
{ |
|
80
|
4 |
|
$ret = []; |
|
81
|
|
|
|
|
82
|
4 |
|
foreach ($array as $key => $value) { |
|
83
|
4 |
|
$map = $propertyMap->getMap(); |
|
84
|
4 |
|
if (isset($map[$key])) { |
|
85
|
4 |
|
$key = $map[$key]; |
|
86
|
4 |
|
} |
|
87
|
4 |
|
if (is_array($value)) { |
|
88
|
3 |
|
$value = $this->buildPropertyMapArray($value, $propertyMap); |
|
89
|
3 |
|
} |
|
90
|
4 |
|
$ret[$key] = $value; |
|
91
|
4 |
|
} |
|
92
|
|
|
|
|
93
|
4 |
|
return $ret; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Using passed mergeSettings array, build array for backend |
|
98
|
|
|
* |
|
99
|
|
|
* @param array $array MergeSettings array |
|
100
|
|
|
* |
|
101
|
|
|
* @return array |
|
102
|
|
|
*/ |
|
103
|
9 |
|
protected function buildMergeSettingsArray(array $array) |
|
104
|
|
|
{ |
|
105
|
9 |
|
$ret = []; |
|
106
|
|
|
|
|
107
|
9 |
|
$propertyMap = new MergeSettingsPropertyMap(); |
|
108
|
|
|
|
|
109
|
9 |
|
foreach ($propertyMap->getMap() as $property => $key) { |
|
110
|
9 |
|
if (isset($array[$key])) { |
|
111
|
9 |
|
$value = $array[$key]; |
|
112
|
9 |
|
if ('culture' == $key) { |
|
113
|
1 |
|
StaticValidator::execute($value, 'Culture'); |
|
114
|
|
|
} |
|
115
|
9 |
|
if ('remove_' == substr($key, 0, 7)) { |
|
116
|
6 |
|
StaticValidator::execute($value, 'TypeBoolean'); |
|
117
|
4 |
|
} |
|
118
|
9 |
|
if ('_date' == substr($key, -5)) { |
|
119
|
9 |
|
StaticValidator::execute($value, 'Timestamp'); |
|
120
|
7 |
|
$value = StaticFilter::execute($value, 'TimestampToDateTime'); |
|
121
|
7 |
|
} |
|
122
|
9 |
|
$ret[$property] = $value; |
|
123
|
9 |
|
} |
|
124
|
9 |
|
} |
|
125
|
|
|
|
|
126
|
4 |
|
return $ret; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Using passed findAndReplaceData associative array (key-value), build array for backend (list of string arrays) |
|
131
|
|
|
* |
|
132
|
|
|
* @param array $array FindAndReplaceData array |
|
133
|
|
|
* |
|
134
|
|
|
* @return array |
|
135
|
|
|
*/ |
|
136
|
4 |
|
protected function buildFindAndReplaceDataArray(array $array) |
|
137
|
|
|
{ |
|
138
|
4 |
|
$ret = []; |
|
139
|
|
|
|
|
140
|
4 |
|
foreach ($array as $search => $replace) { |
|
141
|
4 |
|
array_push($ret, [ |
|
142
|
4 |
|
$search, |
|
143
|
4 |
|
$replace, |
|
144
|
4 |
|
]); |
|
145
|
4 |
|
} |
|
146
|
|
|
|
|
147
|
4 |
|
return $ret; |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.