1 | <?php |
||
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 | 160 | public function __construct(array $data) |
|
25 | { |
||
26 | 160 | foreach ($data as $name => $value) |
|
27 | { |
||
28 | 119 | $mutator = 'set_' . $name; |
|
29 | 119 | $this->$mutator($value); |
|
30 | 160 | } |
|
31 | 160 | } |
|
32 | |||
33 | /** |
||
34 | * @param $name |
||
35 | * @param $args |
||
36 | * @return $this |
||
37 | * @throws \blitze\sitemaker\exception\unexpected_value |
||
38 | */ |
||
39 | 147 | public function __call($name, $args) |
|
40 | { |
||
41 | 147 | if (preg_match('/^(get|set)_(\w+)/', strtolower($name), $match) && $attribute = $this->attribute_exists($match[2])) |
|
42 | 147 | { |
|
43 | 145 | if ('get' == $match[1]) |
|
44 | 145 | { |
|
45 | 135 | return $this->$attribute; |
|
46 | } |
||
47 | else |
||
48 | { |
||
49 | 122 | $this->$attribute = $this->validate_attribute($match[2], $args[0]); |
|
50 | 120 | return $this; |
|
51 | } |
||
52 | } |
||
53 | else |
||
54 | { |
||
55 | 4 | throw new \blitze\sitemaker\exception\unexpected_value(array($name, 'UNDEFINED_METHOD')); |
|
56 | } |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * {@inheritdoc} |
||
61 | */ |
||
62 | 59 | public function to_array() |
|
63 | { |
||
64 | 59 | $attributes = $this->get_attributes(); |
|
65 | |||
66 | 59 | $data = array(); |
|
67 | 59 | foreach ($attributes as $attribute) |
|
68 | { |
||
69 | 59 | $accessor = 'get_' . $attribute; |
|
70 | 59 | $data[$attribute] = $this->$accessor(); |
|
71 | 59 | } |
|
72 | 59 | unset($data['db_fields'], $data['required_fields']); |
|
73 | |||
74 | 59 | return $data; |
|
75 | } |
||
76 | |||
77 | /** |
||
78 | * {@inheritdoc} |
||
79 | */ |
||
80 | 47 | public function to_db() |
|
81 | { |
||
82 | 47 | $this->check_required(); |
|
83 | |||
84 | 42 | $db_data = array(); |
|
85 | 42 | foreach ($this->db_fields as $attribute) |
|
86 | { |
||
87 | 42 | $type = $this->get_property_type($attribute); |
|
88 | 42 | if (in_array($type, array('boolean', 'integer', 'string'))) |
|
89 | 42 | { |
|
90 | 42 | $db_data[$attribute] = $this->$attribute; |
|
91 | 42 | } |
|
92 | 42 | } |
|
93 | |||
94 | 42 | return $db_data; |
|
95 | } |
||
96 | |||
97 | /** |
||
98 | * @return array |
||
99 | */ |
||
100 | 147 | protected function get_attributes() |
|
104 | |||
105 | /** |
||
106 | * @param string $name |
||
107 | * @return string|null |
||
108 | */ |
||
109 | 147 | protected function attribute_exists($name) |
|
117 | |||
118 | /** |
||
119 | * @throws \blitze\sitemaker\exception\invalid_argument |
||
120 | */ |
||
121 | 47 | protected function check_required() |
|
131 | |||
132 | /** |
||
133 | * @param string $name |
||
134 | * @param mixed $value |
||
135 | * @return mixed |
||
136 | * @throws \blitze\sitemaker\exception\unexpected_value |
||
137 | */ |
||
138 | 122 | protected function validate_attribute($name, $value) |
|
155 | |||
156 | /** |
||
157 | * @param string $name |
||
158 | * @return mixed |
||
159 | */ |
||
160 | 125 | protected function get_property_type($name) |
|
170 | } |
||
171 |