|
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
|
|
|
use blitze\sitemaker\model\mapper_interface; |
|
13
|
|
|
|
|
14
|
|
|
abstract class base_mapper implements mapper_interface |
|
15
|
|
|
{ |
|
16
|
|
|
/** @var \phpbb\db\driver\driver_interface */ |
|
17
|
|
|
protected $db; |
|
18
|
|
|
|
|
19
|
|
|
/** @var \blitze\sitemaker\model\base_collection */ |
|
20
|
|
|
protected $_collection; |
|
21
|
|
|
|
|
22
|
|
|
/** @var \blitze\sitemaker\model\mapper_factory */ |
|
23
|
|
|
protected $mapper_factory; |
|
24
|
|
|
|
|
25
|
|
|
/** @var string */ |
|
26
|
|
|
protected $_entity_table; |
|
27
|
|
|
|
|
28
|
|
|
/** @var string */ |
|
29
|
|
|
protected $_entity_class; |
|
30
|
|
|
|
|
31
|
|
|
/** @var string */ |
|
32
|
|
|
protected $_entity_pkey; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Constructor |
|
36
|
|
|
* |
|
37
|
|
|
* @param \phpbb\db\driver\driver_interface $db Database object |
|
38
|
|
|
* @param \blitze\sitemaker\model\base_collection $collection Entity collection |
|
39
|
|
|
* @param \blitze\sitemaker\model\mapper_factory $mapper_factory Mapper factory object |
|
40
|
|
|
* @param string $entity_table |
|
41
|
|
|
*/ |
|
42
|
117 |
|
public function __construct(\phpbb\db\driver\driver_interface $db, \blitze\sitemaker\model\base_collection $collection, \blitze\sitemaker\model\mapper_factory $mapper_factory, $entity_table) |
|
43
|
|
|
{ |
|
44
|
117 |
|
$this->db = $db; |
|
45
|
117 |
|
$this->_collection = $collection; |
|
46
|
117 |
|
$this->mapper_factory = $mapper_factory; |
|
47
|
117 |
|
$this->_entity_table = $entity_table; |
|
48
|
117 |
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Get the collection |
|
52
|
|
|
*/ |
|
53
|
|
|
public function get_collection() |
|
54
|
|
|
{ |
|
55
|
|
|
return $this->_collection; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* {@inheritdoc} |
|
60
|
|
|
*/ |
|
61
|
55 |
|
public function load(array $condition = array()) |
|
62
|
|
|
{ |
|
63
|
55 |
|
$sql_where = $this->_get_condition($condition); |
|
64
|
55 |
|
$results = $this->db->sql_query($this->_find_sql($sql_where)); |
|
65
|
55 |
|
$row = $this->db->sql_fetchrow($results); |
|
66
|
55 |
|
$this->db->sql_freeresult($results); |
|
67
|
|
|
|
|
68
|
|
|
if ($row) |
|
69
|
55 |
|
{ |
|
70
|
44 |
|
return $this->create_entity($row); |
|
71
|
|
|
} |
|
72
|
19 |
|
return null; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* {@inheritdoc} |
|
77
|
|
|
*/ |
|
78
|
42 |
|
public function find(array $condition = array()) |
|
79
|
|
|
{ |
|
80
|
42 |
|
$sql_where = $this->_get_condition($condition); |
|
81
|
42 |
|
$results = $this->db->sql_query($this->_find_sql($sql_where)); |
|
82
|
42 |
|
$this->_collection->clear(); |
|
83
|
|
|
|
|
84
|
42 |
|
while ($row = $this->db->sql_fetchrow($results)) |
|
85
|
|
|
{ |
|
86
|
39 |
|
$this->_collection[$row[$this->_entity_pkey]] = $this->create_entity($row); |
|
87
|
39 |
|
} |
|
88
|
42 |
|
$this->db->sql_freeresult($results); |
|
89
|
|
|
|
|
90
|
42 |
|
return $this->_collection; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* {@inheritdoc} |
|
95
|
|
|
*/ |
|
96
|
26 |
|
public function save(\blitze\sitemaker\model\entity_interface $entity) |
|
97
|
|
|
{ |
|
98
|
26 |
|
$accessor = 'get_' . $this->_entity_pkey; |
|
99
|
26 |
|
if (is_null($entity->$accessor())) |
|
100
|
26 |
|
{ |
|
101
|
11 |
|
$entity = $this->_insert($entity); |
|
102
|
11 |
|
} |
|
103
|
|
|
else |
|
104
|
|
|
{ |
|
105
|
16 |
|
$this->_update($entity); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
25 |
|
return $entity; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* {@inheritdoc} |
|
113
|
|
|
*/ |
|
114
|
15 |
|
public function delete($condition) |
|
115
|
|
|
{ |
|
116
|
15 |
|
if ($condition instanceof $this->_entity_class) |
|
117
|
15 |
|
{ |
|
118
|
10 |
|
$accessor = 'get_' . $this->_entity_pkey; |
|
119
|
10 |
|
$condition = array($this->_entity_pkey, '=', $condition->$accessor()); |
|
120
|
10 |
|
} |
|
121
|
|
|
|
|
122
|
15 |
|
$sql_where = $this->_get_condition($condition); |
|
|
|
|
|
|
123
|
15 |
|
$this->db->sql_query('DELETE FROM ' . $this->_entity_table . (sizeof($sql_where) ? ' WHERE ' . join(' AND ', $sql_where) : '')); |
|
124
|
15 |
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Create the entity |
|
128
|
|
|
*/ |
|
129
|
71 |
|
public function create_entity(array $row) |
|
130
|
|
|
{ |
|
131
|
71 |
|
return new $this->_entity_class($row); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Insert a new row in the table corresponding to the specified entity |
|
136
|
|
|
*/ |
|
137
|
11 |
|
protected function _insert(\blitze\sitemaker\model\entity_interface $entity) |
|
138
|
|
|
{ |
|
139
|
11 |
|
if ($entity instanceof $this->_entity_class) |
|
140
|
11 |
|
{ |
|
141
|
11 |
|
$this->db->sql_query('INSERT INTO ' . $this->_entity_table . ' ' . $this->db->sql_build_array('INSERT', $entity->to_db())); |
|
142
|
|
|
|
|
143
|
11 |
|
$mutator = 'set_' . $this->_entity_pkey; |
|
144
|
11 |
|
$entity->$mutator((int) $this->db->sql_nextid()); |
|
145
|
|
|
|
|
146
|
11 |
|
return $entity; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
throw new \blitze\sitemaker\exception\unexpected_value('INVALID_ENTITY'); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Update the row in the table corresponding to the specified entity |
|
154
|
|
|
*/ |
|
155
|
16 |
|
protected function _update(\blitze\sitemaker\model\entity_interface $entity) |
|
156
|
|
|
{ |
|
157
|
16 |
|
if ($entity instanceof $this->_entity_class) |
|
158
|
16 |
|
{ |
|
159
|
16 |
|
$accessor = 'get_' . $this->_entity_pkey; |
|
160
|
|
|
|
|
161
|
16 |
|
return $this->db->sql_query('UPDATE ' . $this->_entity_table . ' |
|
162
|
16 |
|
SET ' . $this->db->sql_build_array('UPDATE', $entity->to_db()) . ' |
|
163
|
15 |
|
WHERE ' . $this->_entity_pkey . ' = ' . (int) $entity->$accessor()); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
throw new \blitze\sitemaker\exception\unexpected_value('INVALID_ENTITY'); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
28 |
|
protected function _find_sql(array $sql_where) |
|
170
|
|
|
{ |
|
171
|
28 |
|
return 'SELECT * FROM ' . $this->_entity_table . |
|
172
|
28 |
|
(sizeof($sql_where) ? ' WHERE ' . join(' AND ', $sql_where) : ''); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
107 |
|
protected function _get_condition(array $condition) |
|
176
|
|
|
{ |
|
177
|
107 |
|
$sql_where = array(); |
|
178
|
107 |
|
$condition = $this->ensure_multi_array($condition); |
|
179
|
|
|
|
|
180
|
107 |
|
foreach ($condition as $info) |
|
181
|
|
|
{ |
|
182
|
78 |
|
list($field, $operator, $value) = $info; |
|
183
|
|
|
|
|
184
|
78 |
|
switch (gettype($value)) |
|
185
|
|
|
{ |
|
186
|
78 |
|
case 'array': |
|
187
|
9 |
|
$sql_where[] = $this->db->sql_in_set($field, $value, ($operator == '=') ? false : true); |
|
188
|
9 |
|
break; |
|
189
|
78 |
|
case 'string': |
|
190
|
23 |
|
$sql_where[] = $field . " $operator '" . $this->db->sql_escape($value) . "'"; |
|
191
|
23 |
|
break; |
|
192
|
77 |
|
case 'boolean': |
|
193
|
77 |
|
case 'integer': |
|
194
|
77 |
|
$sql_where[] = $field . " $operator " . (int) $value; |
|
195
|
77 |
|
break; |
|
196
|
78 |
|
} |
|
197
|
107 |
|
} |
|
198
|
|
|
|
|
199
|
107 |
|
return $sql_where; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
107 |
|
protected function ensure_multi_array(array $condition) |
|
203
|
|
|
{ |
|
204
|
107 |
|
return array_filter((is_array(current($condition))) ? $condition : array($condition)); |
|
205
|
|
|
} |
|
206
|
|
|
} |
|
207
|
|
|
|
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.