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