1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (c) 2018 - present |
4
|
|
|
* Google Maps PHP - AbstractObject.php |
5
|
|
|
* author: Roberto Belotti - [email protected] |
6
|
|
|
* web : robertobelotti.com, github.com/biscolab |
7
|
|
|
* Initial version created on: 5/9/2018 |
8
|
|
|
* MIT license: https://github.com/biscolab/google-maps-php/blob/master/LICENSE |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Biscolab\GoogleMaps\Abstracts; |
12
|
|
|
|
13
|
|
|
use Biscolab\GoogleMaps\Exception\Exception; |
14
|
|
|
use function Biscolab\GoogleMaps\camel2Snake; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class AbstractObject |
18
|
|
|
* @package Biscolab\GoogleMaps\Abstracts |
19
|
|
|
*/ |
20
|
|
|
abstract class AbstractObject |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
protected $typeCheck = []; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
protected $required = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
protected $errors = []; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* AbstractObject constructor. |
40
|
|
|
* |
41
|
|
|
* @param array $args |
42
|
|
|
*/ |
43
|
|
|
public function __construct(?array $args = []) |
44
|
|
|
{ |
45
|
|
|
|
46
|
|
|
if (is_null($args)) { |
|
|
|
|
47
|
|
|
$args = []; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$this->setArgs($args); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param array $args |
55
|
|
|
* |
56
|
|
|
* @throws Exception |
57
|
|
|
*/ |
58
|
|
|
protected function setArgs(array $args) |
59
|
|
|
{ |
60
|
|
|
|
61
|
|
|
foreach ($this->typeCheck as $field_name => $field_type) { |
62
|
|
|
if (empty($args[$field_name]) || is_null($args[$field_name])) { |
63
|
|
|
if ($this->isFieldRequired($field_name)) { |
64
|
|
|
$this->addError('Missing "' . $field_name . '" in ' . static::class); |
65
|
|
|
} |
66
|
|
|
} else { |
67
|
|
|
$this->$field_name = $this->parseFieldValue($field_type, $args[$field_name]); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
$this->throwErrors(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param string $field_name |
75
|
|
|
* |
76
|
|
|
* @return bool |
77
|
|
|
*/ |
78
|
|
|
protected function isFieldRequired(string $field_name): bool |
79
|
|
|
{ |
80
|
|
|
|
81
|
|
|
return in_array($field_name, $this->required); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param string $error |
86
|
|
|
* |
87
|
|
|
* @return array |
88
|
|
|
*/ |
89
|
|
|
protected function addError(string $error): array |
90
|
|
|
{ |
91
|
|
|
|
92
|
|
|
array_push($this->errors, $error); |
93
|
|
|
|
94
|
|
|
return $this->errors; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param string $field_type |
99
|
|
|
* @param string $field_value |
100
|
|
|
* |
101
|
|
|
* @return mixed |
102
|
|
|
*/ |
103
|
|
|
protected function parseFieldValue(string $field_type, $field_value) |
104
|
|
|
{ |
105
|
|
|
|
106
|
|
|
switch ($field_type) { |
107
|
|
|
case 'string': |
108
|
|
|
case 'int': |
109
|
|
|
case 'float': |
110
|
|
|
case 'array': |
111
|
|
|
case 'json': |
112
|
|
|
return $field_value; |
113
|
|
|
default: |
114
|
|
|
return ($field_value instanceof $field_type) ? $field_value : new $field_type($field_value); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @throws Exception |
120
|
|
|
*/ |
121
|
|
|
protected function throwErrors() |
122
|
|
|
{ |
123
|
|
|
|
124
|
|
|
if (count($this->errors)) { |
125
|
|
|
throw new Exception(implode(', ', $this->errors)); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return string |
131
|
|
|
*/ |
132
|
|
|
public function toJson(): string |
133
|
|
|
{ |
134
|
|
|
|
135
|
|
|
return json_encode($this->toArray()); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @return array |
140
|
|
|
*/ |
141
|
|
|
public function toArray(): array |
142
|
|
|
{ |
143
|
|
|
|
144
|
|
|
$fields = get_object_vars($this); |
145
|
|
|
|
146
|
|
|
foreach ($fields as $field_name => $field_value) { |
147
|
|
|
|
148
|
|
|
if (!is_scalar($field_value) && method_exists($field_value, 'toJson')) { |
149
|
|
|
$fields[$field_name] = $field_value->toArray(); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return $fields; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @return string |
158
|
|
|
*/ |
159
|
|
|
public function __toString(): string |
160
|
|
|
{ |
161
|
|
|
|
162
|
|
|
return implode(',', $this->toArray()); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param $name |
167
|
|
|
* @param $arguments |
168
|
|
|
* |
169
|
|
|
* @return mixed |
170
|
|
|
*/ |
171
|
|
|
public function __call($name, $arguments) |
172
|
|
|
{ |
173
|
|
|
|
174
|
|
|
preg_match('/(?<=(g|s)et)([A-Za-z0-9])\w+/', $name, $match); |
175
|
|
|
|
176
|
|
|
$camel_field = (empty($match[0])) ? '' : $match[0]; |
177
|
|
|
|
178
|
|
|
$snake_field = $this->getFieldName($camel_field); |
179
|
|
|
|
180
|
|
|
$field_type = (empty($this->typeCheck[$snake_field])) ? null : $this->typeCheck[$snake_field]; |
181
|
|
|
|
182
|
|
|
if (!empty($match[1]) && $field_type) { |
183
|
|
|
switch ($match[1]) { |
184
|
|
|
case 's': |
185
|
|
|
return $this->$snake_field = $this->parseFieldValue($field_type, current($arguments)); |
186
|
|
|
case 'g': |
187
|
|
|
return $this->$snake_field; |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @param string $initial_field_name |
194
|
|
|
* |
195
|
|
|
* @return string |
196
|
|
|
*/ |
197
|
|
|
protected function getFieldName(string $initial_field_name): string |
198
|
|
|
{ |
199
|
|
|
|
200
|
|
|
return camel2Snake($initial_field_name); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
} |