|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* |
|
5
|
|
|
* @package sitemaker |
|
6
|
|
|
* @copyright (c) 2015 Daniel A. (blitze) |
|
7
|
|
|
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 |
|
8
|
|
|
* |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace blitze\sitemaker\model; |
|
12
|
|
|
|
|
13
|
|
|
abstract class base_entity implements entity_interface |
|
14
|
|
|
{ |
|
15
|
|
|
/** @var array */ |
|
16
|
|
|
protected $db_fields = array(); |
|
17
|
|
|
|
|
18
|
|
|
/** @var array */ |
|
19
|
|
|
protected $required_fields = array(); |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Populate the entity with data |
|
23
|
|
|
* @param array $data |
|
24
|
191 |
|
*/ |
|
25
|
|
|
public function __construct(array $data) |
|
26
|
191 |
|
{ |
|
27
|
|
|
foreach ($data as $name => $value) |
|
28
|
150 |
|
{ |
|
29
|
150 |
|
$mutator = 'set_' . $name; |
|
30
|
191 |
|
$this->$mutator($value); |
|
31
|
191 |
|
} |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param $name |
|
36
|
|
|
* @param $args |
|
37
|
|
|
* @return $this |
|
38
|
|
|
* @throws \blitze\sitemaker\exception\invalid_argument |
|
39
|
173 |
|
*/ |
|
40
|
|
|
public function __call($name, $args) |
|
41
|
173 |
|
{ |
|
42
|
173 |
|
if (preg_match('/^(get|set)_(\w+)/', strtolower($name), $match) && $attribute = $this->attribute_exists($match[2])) |
|
43
|
171 |
|
{ |
|
44
|
171 |
|
if ('get' == $match[1]) |
|
45
|
159 |
|
{ |
|
46
|
|
|
return $this->$attribute; |
|
47
|
|
|
} |
|
48
|
|
|
else |
|
49
|
147 |
|
{ |
|
50
|
145 |
|
$this->$attribute = $this->validate_attribute($match[2], $args[0]); |
|
51
|
|
|
return $this; |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
4 |
|
|
|
55
|
4 |
|
$property = str_replace(array('get_', 'set_'), '', $name); |
|
56
|
|
|
throw new \blitze\sitemaker\exception\invalid_argument(array($property, 'INVALID_PROPERTY')); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* {@inheritdoc} |
|
61
|
81 |
|
*/ |
|
62
|
|
|
public function to_array() |
|
63
|
81 |
|
{ |
|
64
|
|
|
$attributes = $this->get_attributes(); |
|
65
|
81 |
|
|
|
66
|
81 |
|
$data = array(); |
|
67
|
|
|
foreach ($attributes as $attribute) |
|
68
|
81 |
|
{ |
|
69
|
81 |
|
$accessor = 'get_' . $attribute; |
|
70
|
81 |
|
$data[$attribute] = $this->$accessor(); |
|
71
|
81 |
|
} |
|
72
|
|
|
unset($data['db_fields'], $data['required_fields']); |
|
73
|
81 |
|
|
|
74
|
|
|
return $data; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* {@inheritdoc} |
|
79
|
48 |
|
*/ |
|
80
|
|
|
public function to_db() |
|
81
|
48 |
|
{ |
|
82
|
|
|
$this->check_required(); |
|
83
|
43 |
|
|
|
84
|
43 |
|
$db_data = array(); |
|
85
|
|
|
foreach ($this->db_fields as $attribute) |
|
86
|
43 |
|
{ |
|
87
|
43 |
|
$type = $this->get_property_type($attribute); |
|
88
|
43 |
|
if (in_array($type, array('boolean', 'integer', 'string'))) |
|
89
|
43 |
|
{ |
|
90
|
43 |
|
$db_data[$attribute] = $this->$attribute; |
|
91
|
43 |
|
} |
|
92
|
|
|
} |
|
93
|
43 |
|
|
|
94
|
|
|
return $db_data; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @return array |
|
99
|
173 |
|
*/ |
|
100
|
|
|
protected function get_attributes() |
|
101
|
173 |
|
{ |
|
102
|
|
|
return array_keys(get_class_vars(get_class($this))); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @param string $name |
|
107
|
|
|
* @return string|null |
|
108
|
173 |
|
*/ |
|
109
|
|
|
protected function attribute_exists($name) |
|
110
|
173 |
|
{ |
|
111
|
173 |
|
if (in_array(strtolower($name), $this->get_attributes())) |
|
112
|
171 |
|
{ |
|
113
|
|
|
return strtolower($name); |
|
114
|
4 |
|
} |
|
115
|
|
|
return null; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @throws \blitze\sitemaker\exception\invalid_argument |
|
120
|
48 |
|
*/ |
|
121
|
|
|
protected function check_required() |
|
122
|
48 |
|
{ |
|
123
|
|
|
foreach ($this->required_fields as $field) |
|
124
|
48 |
|
{ |
|
125
|
48 |
|
if (!$this->$field) |
|
126
|
5 |
|
{ |
|
127
|
|
|
throw new \blitze\sitemaker\exception\invalid_argument(array($field, 'FIELD_MISSING')); |
|
128
|
46 |
|
} |
|
129
|
43 |
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @param string $name |
|
134
|
|
|
* @param mixed $value |
|
135
|
|
|
* @return mixed |
|
136
|
|
|
* @throws \blitze\sitemaker\exception\invalid_argument |
|
137
|
147 |
|
*/ |
|
138
|
|
|
protected function validate_attribute($name, $value) |
|
139
|
147 |
|
{ |
|
140
|
|
|
$type = $this->get_property_type($name); |
|
141
|
147 |
|
|
|
142
|
147 |
|
if (in_array($type, array('array', 'boolean', 'float', 'integer', 'string'))) |
|
143
|
142 |
|
{ |
|
144
|
142 |
|
settype($value, $type); |
|
145
|
|
|
return $value; |
|
146
|
|
|
} |
|
147
|
36 |
|
|
|
148
|
36 |
|
if ($type === false || !$value instanceof $type) |
|
149
|
2 |
|
{ |
|
150
|
|
|
throw new \blitze\sitemaker\exception\invalid_argument(array($name, 'INVALID_DATA_TYPE')); |
|
151
|
|
|
} |
|
152
|
34 |
|
|
|
153
|
|
|
return $value; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @param string $name |
|
158
|
|
|
* @return mixed |
|
159
|
150 |
|
*/ |
|
160
|
|
|
protected function get_property_type($name) |
|
161
|
150 |
|
{ |
|
162
|
150 |
|
$reflection = new \ReflectionObject($this); |
|
163
|
150 |
|
$reflectionProperty = $reflection->getProperty($name); |
|
164
|
|
|
$doc = $reflectionProperty->getDocComment(); |
|
165
|
150 |
|
|
|
166
|
|
|
preg_match_all('#\/\*\*\s@var\s(.*?)\s\*\/#s', $doc, $annotations); |
|
167
|
150 |
|
|
|
168
|
|
|
return current($annotations[1]); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Set array field value |
|
173
|
|
|
* @param string $field |
|
174
|
|
|
* @param array|string $settings |
|
175
|
|
|
* @return bool |
|
176
|
|
|
*/ |
|
177
|
|
|
protected function set_array_field($field, $settings) |
|
178
|
|
|
{ |
|
179
|
|
|
$this->$field = ''; |
|
180
|
|
|
$encoded = false; |
|
181
|
|
|
|
|
182
|
|
|
if (!is_array($settings)) |
|
183
|
|
|
{ |
|
184
|
|
|
$this->$field = $settings; |
|
185
|
|
|
} |
|
186
|
|
|
else if (sizeof($settings)) |
|
187
|
|
|
{ |
|
188
|
|
|
$this->$field = json_encode($settings); |
|
189
|
|
|
$encoded = true; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
return $encoded; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* Get array field value |
|
197
|
|
|
* @return array |
|
198
|
|
|
*/ |
|
199
|
|
|
protected function get_array_field($field) |
|
200
|
|
|
{ |
|
201
|
|
|
return ($this->$field) ? json_decode($this->$field, true) : array(); |
|
202
|
|
|
} |
|
203
|
|
|
} |
|
204
|
|
|
|