Completed
Push — master ( d0d590...4bea8e )
by Daniel
08:46
created

base_mapper::get_collection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 3
Bugs 1 Features 1
Metric Value
c 3
b 1
f 1
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
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 119
	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 119
		$this->db = $db;
43 119
		$this->collection = $collection;
44 119
		$this->mapper_factory = $mapper_factory;
45 119
		$this->entity_table = $entity_table;
46 119
	}
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 43
	public function find(array $condition = array())
69
	{
70 43
		$sql_where = $this->get_sql_condition($condition);
71 43
		$results = $this->db->sql_query($this->find_sql($sql_where));
72 43
		$this->collection->clear();
73
74 43
		while ($row = $this->db->sql_fetchrow($results))
75
		{
76 40
			$this->collection[$row[$this->entity_pkey]] = $this->create_entity($row);
77 40
		}
78 43
		$this->db->sql_freeresult($results);
79
80 43
		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 15
	public function delete($condition)
105
	{
106 15
		if (!is_array($condition))
107 15
		{
108 10
			if ($condition instanceof $this->entity_class)
109 10
			{
110 10
				$accessor = 'get_' . $this->entity_pkey;
111 10
				$condition = array($this->entity_pkey, '=', $condition->$accessor());
112 10
			}
113
			else
114
			{
115
				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 73
	public function create_entity(array $row)
127
	{
128 73
		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 109
	protected function get_sql_condition(array $condition)
177
	{
178 109
		$sql_where = array();
179 109
		$condition = $this->ensure_multi_array($condition);
180
181 109
		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 109
		}
191
192 109
		return $sql_where;
193
	}
194
195
	/**
196
	 * @param array $condition
197
	 * @return mixed
198
	 */
199 109
	protected function ensure_multi_array(array $condition)
200
	{
201 109
		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
		return $this->db->sql_in_set($field, $value, ($operator == '=') ? false : true);
213
	}
214
215
	/**
216
	 * @param string $field
217
	 * @param string $value
218
	 * @param string $operator
219
	 * @return string
220
	 */
221 24
	protected function get_sql_where_string($field, $value, $operator)
222
	{
223 24
		return $field . " $operator '" . $this->db->sql_escape($value) . "'";
224
	}
225
226
	/**
227
	 * @param string $field
228
	 * @param string $value
229
	 * @param string $operator
230
	 * @return string
231
	 */
232 79
	protected function get_sql_where_integer($field, $value, $operator)
233
	{
234 79
		return $field . " $operator " . (int) $value;
235
	}
236
237
	/**
238
	 * @param string $field
239
	 * @param bool $value
240
	 * @param string $operator
241
	 * @return string
242
	 */
243 1
	protected function get_sql_where_boolean($field, $value, $operator)
244
	{
245 1
		return $this->get_sql_where_integer($field, (int) $value, $operator);
246
	}
247
}
248