-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathMapper.php
More file actions
351 lines (303 loc) · 8.29 KB
/
Mapper.php
File metadata and controls
351 lines (303 loc) · 8.29 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
<?php
namespace Smartbox\Integration\FrameworkBundle\Tools\Mapper;
use JMS\Serializer\SerializationContext;
use Smartbox\Integration\FrameworkBundle\DependencyInjection\Traits\UsesEvaluator;
use Smartbox\Integration\FrameworkBundle\Tools\Helper\DateTimeHelper;
/**
* Class Mapper.
*/
class Mapper implements MapperInterface
{
use UsesEvaluator;
protected $debug = false;
protected $mappings = [];
protected $dictionary = [
'ISO8601' => \DateTime::ISO8601,
'ISO8601Micro' => 'Y-m-d\TH:i:s.000',
];
public function setDebug($debug = false)
{
$this->debug = $debug;
}
public function addMappings(array $mappings)
{
foreach ($mappings as $mappingName => $mapping) {
$this->mappings[$mappingName] = $mapping;
}
}
/**
* @param $format
* @param \DateTime|null $date
*
* @return null|string
*/
public function formatDate($format, \DateTime $date = null)
{
if (null === $date) {
return null;
}
return $date->format($format);
}
/**
* @param mixed $obj
* @param string $mappingName
* @param mixed $context
*
* @return array|mixed
*/
public function map($obj, $mappingName, $context = [])
{
if (!$mappingName || !array_key_exists($mappingName, $this->mappings)) {
throw new \InvalidArgumentException(sprintf('Invalid mapping name "%s"', $mappingName));
}
if (empty($obj)) {
return $obj;
}
$mapping = @$this->mappings[$mappingName];
return $this->resolve($mapping, $obj, $context);
}
/**
* @param $mapping
* @param $obj
* @param $context
*
* @return array|null|string
*/
public function resolve(&$mapping, &$obj, &$context)
{
if (empty($mapping)) {
return $mapping;
} elseif (is_array($mapping)) {
$res = [];
foreach ($mapping as $key => $value) {
$resolved = $this->resolve($value, $obj, $context);
if (null !== $resolved) {
$res[$key] = $resolved;
}
}
return $res;
} elseif (is_string($mapping)) {
$dictionary = array_merge($this->dictionary, ['obj' => $obj, 'context' => $context]);
$res = null;
try {
$res = @$this->evaluator->evaluateWithVars($mapping, $dictionary);
} catch (\RuntimeException $e) {
if ($this->debug) {
throw $e;
}
}
return $res;
}
throw new \RuntimeException('Mapper expected the mapping to be a string or an array');
}
/**
* @param mixed $elements
* @param string $mappingName
* @param mixed $context
*
* @return array
*/
public function mapAll($elements, $mappingName, $context = [])
{
if (!is_array($elements)) {
throw new \RuntimeException('MapAll expected an array');
}
if (empty($elements)) {
return $elements;
}
$res = [];
foreach ($elements as $key => $element) {
$res[$key] = $this->map($element, $mappingName, $context);
}
return $res;
}
/**
* Return true if the key exists in the given array.
*
* @param array $obj
* @param string $key
*
* @return bool
*/
public function keyExists($array, $key)
{
if (!is_array($array)) {
throw new \InvalidArgumentException('keyExists expected the first argument to be an array, \''.print_r($array, true).'\' was given.');
}
if (!is_string($key) and !is_integer($key)) {
throw new \InvalidArgumentException('keyExists expected the key (second argument) to be either a string or an integer, \''.print_r($key, true).'\' was given.');
}
return array_key_exists($key, $array);
}
/**
* @param $class
* @param $property
*
* @return bool
*/
public function propertyExists($class, $property)
{
return property_exists($class, $property);
}
/**
* Get the first element of an array.
*
* @param array $array
*
* @return mixed
*/
public function first(array $array)
{
return reset($array);
}
/**
* Convert an string to date.
*
* @param string $date
*
* @return \DateTime
*/
public function stringToDate($date)
{
if (!empty($date)) {
return new \DateTime($date);
}
}
public function timestampToDate($timestamp)
{
$date = new \DateTime();
$date->setTimestamp($timestamp);
return $date;
}
public function timestampWithMsToDate($timestamp)
{
return DateTimeHelper::createDateTimeFromTimestampWithMilliseconds($timestamp);
}
/**
* Create a Soap var object.
*
* @param mixed $data Data to create the SoapVar object
* @param string $encoding The encoding id
* @param string $type Entity type name
*
* @return \SoapVar
*/
public function toSoapVarObj($data, $encoding, $type = null)
{
return new \SoapVar($data, $encoding, $type);
}
/**
* Convert an array into string.
*
* @param string $glue The string to use to glue the elements of the array
* @param array $data The array of strings to join
*
* @return string
*/
public function arrayToString($glue, array $data)
{
return implode($glue, $data);
}
/**
* Converts a string into an array.
*
* @param string $delimiter
* @param string $string
*
* @return array
*/
public function stringToArray($delimiter, $string)
{
return explode($delimiter, $string);
}
/**
* Flatten an array by key.
*
* @param array $data The array to flatten
* @param string $key The common key
*
* @return array
*/
public function flattenArrayByKey(array $data, $key)
{
$array = [];
foreach ($data as $value) {
if (is_array($value) && isset($value[$key])) {
$array[] = $value[$key];
}
}
return $array;
}
public function toString($data)
{
return (string) $data;
}
public function toMongoID($id)
{
if (class_exists('\MongoDB\BSON\ObjectID')) {
return new \MongoDB\BSON\ObjectID((string) $id);
}
throw new \RuntimeException('To instantiate a Mongo ObjectID object you need to install the php mongo extension.');
}
/**
* @param $search
* @param $replace
* @param $subject
*
* @return mixed
*/
public function replaceStr($search, $replace, $subject)
{
return str_replace($search, $replace, $subject);
}
/**
* Serialize the given datas into the expected format with a group.
*
* @param $data
* @param $format
* @param $group
*
* @return string
*/
public function serializeWithGroup($data, $format, $group)
{
$serializer = $this->evaluator->getSerializer();
return $serializer->serialize($data, $format, SerializationContext::create()->setGroups([$group]));
}
/**
* Return the n-th section of the given string splitted by piece of the given length.
*
* @param string $string
* @param int $length
* @param int $section
*
* @return string
*/
public function wordWrap($string, $length, $section)
{
$wrapped = wordwrap($string, $length, '\mapperDelimiter', true);
$lines = explode('\mapperDelimiter', $wrapped);
--$section;
$result = '';
if (isset($lines[$section])) {
$result = $lines[$section];
}
return $result;
}
/**
* Return $value if $string is empty. $value can be null. If $string is not empty, returns $string.
*
* @param string $string
* @param string $value
*
* @return string
*/
public function emptyTo($string, $value)
{
if (!empty($string)) {
return $string;
} else {
return $value;
}
}
}