1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PagaMasTarde\OrdersApiClient\Model; |
4
|
|
|
|
5
|
|
|
use Nayjest\StrCaseConverter\Str; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class AbstractModel |
9
|
|
|
* |
10
|
|
|
* @package PagaMasTarde\OrdersApiClient\Model |
11
|
|
|
*/ |
12
|
|
|
abstract class AbstractModel implements ModelInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Export as Array the object recursively |
16
|
|
|
* |
17
|
|
|
* @param bool $validation |
18
|
|
|
* |
19
|
|
|
* @return array |
20
|
|
|
*/ |
21
|
|
|
public function export($validation = true) |
22
|
|
|
{ |
23
|
|
|
$result = array(); |
24
|
|
|
foreach ($this as $key => $value) { |
25
|
|
|
if (!is_null($value)) { |
26
|
|
|
$result[Str::toSnakeCase($key)] = $this->parseValue($value, $validation); |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
return $result; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Parse the value of the object depending of type. |
35
|
|
|
* |
36
|
|
|
* @param $value |
37
|
|
|
* @param $validation |
38
|
|
|
* |
39
|
|
|
* @return array|string |
40
|
|
|
*/ |
41
|
|
|
protected function parseValue($value, $validation) |
42
|
|
|
{ |
43
|
|
|
if (is_array($value) && !empty($value)) { |
44
|
|
|
$valueArray = array(); |
45
|
|
|
foreach ($value as $subKey => $subValue) { |
46
|
|
|
if (is_object($subValue) && $subValue instanceof AbstractModel) { |
47
|
|
|
$valueArray[Str::toSnakeCase($subKey)] = $subValue->export($validation); |
48
|
|
|
} else { |
49
|
|
|
$valueArray[Str::toSnakeCase($subKey)] = $subValue; |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
return $valueArray; |
53
|
|
|
} |
54
|
|
|
if (is_object($value) && $value instanceof AbstractModel && !empty($value)) { |
55
|
|
|
return $value->export(); |
56
|
|
|
} |
57
|
|
|
if (is_object($value) && $value instanceof \DateTime && !empty($value)) { |
58
|
|
|
return $value->format('Y-m-d\Th:i:s'); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return $value; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Fill Up the Order from the json_decode(false) result of the API response. |
66
|
|
|
* |
67
|
|
|
* @param $object |
68
|
|
|
* |
69
|
|
|
*/ |
70
|
|
|
public function import($object) |
71
|
|
|
{ |
72
|
|
|
if (is_object($object)) { |
73
|
|
|
$properties = get_object_vars($object); |
74
|
|
|
foreach ($properties as $key => $value) { |
75
|
|
|
if (property_exists($this, lcfirst(Str::toCamelCase($key)))) { |
76
|
|
|
if (is_object($value)) { |
77
|
|
|
$objectProperty = $this->{lcfirst(Str::toCamelCase($key))}; |
78
|
|
|
if ($objectProperty instanceof AbstractModel) { |
79
|
|
|
$objectProperty->import($value); |
80
|
|
|
} |
81
|
|
|
} else { |
82
|
|
|
if (is_string($value) && |
83
|
|
|
preg_match('/[0-9\-]*T[0-9\:]/', $value) |
84
|
|
|
) { |
85
|
|
|
$this->{lcfirst(Str::toCamelCase($key))} = new \DateTime($value); |
86
|
|
|
} else { |
87
|
|
|
$this->{lcfirst(Str::toCamelCase($key))} = $value; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|