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_mapper implements mapper_interface |
13
|
|
|
{ |
14
|
|
|
/** @var \phpbb\db\driver\driver_interface */ |
15
|
|
|
protected $db; |
16
|
|
|
|
17
|
|
|
/** @var \blitze\sitemaker\model\base_collection */ |
18
|
|
|
protected $collection; |
19
|
|
|
|
20
|
|
|
/** @var \blitze\sitemaker\model\mapper_factory */ |
21
|
|
|
protected $mapper_factory; |
22
|
|
|
|
23
|
|
|
/** @var string */ |
24
|
|
|
protected $entity_table; |
25
|
|
|
|
26
|
|
|
/** @var string */ |
27
|
|
|
protected $entity_class; |
28
|
|
|
|
29
|
|
|
/** @var string */ |
30
|
|
|
protected $entity_pkey; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Constructor |
34
|
|
|
* |
35
|
|
|
* @param \phpbb\db\driver\driver_interface $db Database object |
36
|
|
|
* @param \blitze\sitemaker\model\base_collection $collection Entity collection |
37
|
|
|
* @param \blitze\sitemaker\model\mapper_factory $mapper_factory Mapper factory object |
38
|
|
|
* @param string $entity_table |
39
|
|
|
*/ |
40
|
131 |
|
public function __construct(\phpbb\db\driver\driver_interface $db, \blitze\sitemaker\model\base_collection $collection, \blitze\sitemaker\model\mapper_factory $mapper_factory, $entity_table) |
41
|
|
|
{ |
42
|
131 |
|
$this->db = $db; |
43
|
131 |
|
$this->collection = $collection; |
44
|
131 |
|
$this->mapper_factory = $mapper_factory; |
45
|
131 |
|
$this->entity_table = $entity_table; |
46
|
131 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
*/ |
51
|
57 |
|
public function load(array $condition = array()) |
52
|
|
|
{ |
53
|
57 |
|
$sql_where = $this->get_sql_condition($condition); |
54
|
57 |
|
$results = $this->db->sql_query($this->find_sql($sql_where)); |
55
|
57 |
|
$row = $this->db->sql_fetchrow($results); |
56
|
57 |
|
$this->db->sql_freeresult($results); |
57
|
|
|
|
58
|
|
|
if ($row) |
59
|
57 |
|
{ |
60
|
46 |
|
return $this->create_entity($row); |
61
|
|
|
} |
62
|
20 |
|
return null; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritdoc} |
67
|
|
|
*/ |
68
|
51 |
|
public function find(array $condition = array()) |
69
|
|
|
{ |
70
|
51 |
|
$sql_where = $this->get_sql_condition($condition); |
71
|
51 |
|
$results = $this->db->sql_query($this->find_sql($sql_where)); |
72
|
51 |
|
$this->collection->clear(); |
73
|
|
|
|
74
|
51 |
|
while ($row = $this->db->sql_fetchrow($results)) |
75
|
|
|
{ |
76
|
48 |
|
$this->collection[$row[$this->entity_pkey]] = $this->create_entity($row); |
77
|
48 |
|
} |
78
|
51 |
|
$this->db->sql_freeresult($results); |
79
|
|
|
|
80
|
51 |
|
return $this->collection; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* {@inheritdoc} |
85
|
|
|
*/ |
86
|
27 |
|
public function save(\blitze\sitemaker\model\entity_interface $entity) |
87
|
|
|
{ |
88
|
27 |
|
$accessor = 'get_' . $this->entity_pkey; |
89
|
27 |
|
if (is_null($entity->$accessor())) |
90
|
27 |
|
{ |
91
|
12 |
|
$entity = $this->insert($entity); |
92
|
12 |
|
} |
93
|
|
|
else |
94
|
|
|
{ |
95
|
16 |
|
$this->update($entity); |
96
|
|
|
} |
97
|
|
|
|
98
|
26 |
|
return $entity; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* {@inheritdoc} |
103
|
|
|
*/ |
104
|
16 |
|
public function delete($condition) |
105
|
|
|
{ |
106
|
16 |
|
if (!is_array($condition)) |
107
|
16 |
|
{ |
108
|
11 |
|
if ($condition instanceof $this->entity_class) |
109
|
11 |
|
{ |
110
|
10 |
|
$accessor = 'get_' . $this->entity_pkey; |
111
|
10 |
|
$condition = array($this->entity_pkey, '=', $condition->$accessor()); |
112
|
10 |
|
} |
113
|
|
|
else |
114
|
|
|
{ |
115
|
1 |
|
throw new \blitze\sitemaker\exception\invalid_argument(array('entity', 'INVALID_ENTITY')); |
116
|
|
|
} |
117
|
10 |
|
} |
118
|
|
|
|
119
|
15 |
|
$sql_where = $this->get_sql_condition($condition); |
120
|
15 |
|
$this->db->sql_query('DELETE FROM ' . $this->entity_table . (sizeof($sql_where) ? ' WHERE ' . join(' AND ', $sql_where) : '')); |
121
|
15 |
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* {@inheritdoc} |
125
|
|
|
*/ |
126
|
82 |
|
public function create_entity(array $row) |
127
|
|
|
{ |
128
|
82 |
|
return new $this->entity_class($row); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Insert a new row in the table corresponding to the specified entity |
133
|
|
|
* @param \blitze\sitemaker\model\entity_interface $entity |
134
|
|
|
* @return \blitze\sitemaker\model\entity_interface |
135
|
|
|
* @throws \blitze\sitemaker\exception\invalid_argument |
136
|
|
|
*/ |
137
|
12 |
|
protected function insert(\blitze\sitemaker\model\entity_interface $entity) |
138
|
|
|
{ |
139
|
12 |
|
$this->db->sql_query('INSERT INTO ' . $this->entity_table . ' ' . $this->db->sql_build_array('INSERT', $entity->to_db())); |
140
|
|
|
|
141
|
12 |
|
$mutator = 'set_' . $this->entity_pkey; |
142
|
12 |
|
$entity->$mutator((int) $this->db->sql_nextid()); |
143
|
|
|
|
144
|
12 |
|
return $entity; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Update the row in the table corresponding to the specified entity |
149
|
|
|
* @param \blitze\sitemaker\model\entity_interface $entity |
150
|
|
|
* @return mixed |
151
|
|
|
* @throws \blitze\sitemaker\exception\invalid_argument |
152
|
|
|
*/ |
153
|
16 |
|
protected function update(\blitze\sitemaker\model\entity_interface $entity) |
154
|
|
|
{ |
155
|
16 |
|
$accessor = 'get_' . $this->entity_pkey; |
156
|
|
|
|
157
|
16 |
|
return $this->db->sql_query('UPDATE ' . $this->entity_table . ' |
158
|
16 |
|
SET ' . $this->db->sql_build_array('UPDATE', $entity->to_db()) . ' |
159
|
15 |
|
WHERE ' . $this->entity_pkey . ' = ' . (int) $entity->$accessor()); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @param array $sql_where |
164
|
|
|
* @return string |
165
|
|
|
*/ |
166
|
28 |
|
protected function find_sql(array $sql_where) |
167
|
|
|
{ |
168
|
28 |
|
return 'SELECT * FROM ' . $this->entity_table . |
169
|
28 |
|
(sizeof($sql_where) ? ' WHERE ' . join(' AND ', $sql_where) : ''); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @param array $condition |
174
|
|
|
* @return array |
175
|
|
|
*/ |
176
|
120 |
|
protected function get_sql_condition(array $condition) |
177
|
|
|
{ |
178
|
120 |
|
$sql_where = array(); |
179
|
120 |
|
$condition = $this->ensure_multi_array($condition); |
180
|
|
|
|
181
|
120 |
|
foreach ($condition as $info) |
182
|
|
|
{ |
183
|
80 |
|
list($field, $operator, $value) = $info; |
184
|
|
|
|
185
|
80 |
|
$callable = 'get_sql_where_' . gettype($value); |
186
|
80 |
|
if (is_callable(array($this, $callable))) |
187
|
80 |
|
{ |
188
|
80 |
|
$sql_where[] = call_user_func_array(array($this, $callable), array($field, $value, $operator)); |
189
|
80 |
|
} |
190
|
120 |
|
} |
191
|
|
|
|
192
|
120 |
|
return $sql_where; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @param array $condition |
197
|
|
|
* @return mixed |
198
|
|
|
*/ |
199
|
120 |
|
protected function ensure_multi_array(array $condition) |
200
|
|
|
{ |
201
|
120 |
|
return array_filter((is_array(current($condition))) ? $condition : array($condition)); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @param string $field |
206
|
|
|
* @param array $value |
207
|
|
|
* @param string $operator |
208
|
|
|
* @return string |
209
|
|
|
*/ |
210
|
9 |
|
protected function get_sql_where_array($field, array $value, $operator) |
211
|
|
|
{ |
212
|
9 |
|
$sql_where = ''; |
213
|
9 |
|
if (sizeof($value)) |
214
|
9 |
|
{ |
215
|
9 |
|
$sql_where = $this->db->sql_in_set($field, $value, ($operator == '=') ? false : true); |
216
|
9 |
|
} |
217
|
|
|
|
218
|
9 |
|
return $sql_where; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @param string $field |
223
|
|
|
* @param string $value |
224
|
|
|
* @param string $operator |
225
|
|
|
* @return string |
226
|
|
|
*/ |
227
|
24 |
|
protected function get_sql_where_string($field, $value, $operator) |
228
|
|
|
{ |
229
|
24 |
|
return $field . " $operator '" . $this->db->sql_escape($value) . "'"; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* @param string $field |
234
|
|
|
* @param string $value |
235
|
|
|
* @param string $operator |
236
|
|
|
* @return string |
237
|
|
|
*/ |
238
|
79 |
|
protected function get_sql_where_integer($field, $value, $operator) |
239
|
|
|
{ |
240
|
79 |
|
return $field . " $operator " . (int) $value; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* @param string $field |
245
|
|
|
* @param bool $value |
246
|
|
|
* @param string $operator |
247
|
|
|
* @return string |
248
|
|
|
*/ |
249
|
1 |
|
protected function get_sql_where_boolean($field, $value, $operator) |
250
|
|
|
{ |
251
|
1 |
|
return $this->get_sql_where_integer($field, (int) $value, $operator); |
252
|
|
|
} |
253
|
|
|
} |
254
|
|
|
|