1 | <?php |
||
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) |
|
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) |
|
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) |
|
161 | |||
162 | /** |
||
163 | * @param array $sql_where |
||
164 | * @return string |
||
165 | */ |
||
166 | 28 | protected function find_sql(array $sql_where) |
|
171 | |||
172 | /** |
||
173 | * @param array $condition |
||
174 | * @return array |
||
175 | */ |
||
176 | 120 | protected function get_sql_condition(array $condition) |
|
194 | |||
195 | /** |
||
196 | * @param array $condition |
||
197 | * @return mixed |
||
198 | */ |
||
199 | 120 | protected function ensure_multi_array(array $condition) |
|
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) |
|
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) |
|
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) |
|
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) |
|
253 | } |
||
254 |