Completed
Push — master ( 2fd22e...c609bc )
by Michael
02:32
created

getSearchQueryWhereClause()   D

Complexity

Conditions 9
Paths 12

Size

Total Lines 26
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 9

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 26
ccs 19
cts 19
cp 1
rs 4.909
cc 9
eloc 18
nc 12
nop 1
crap 9
1
<?php
2
3
/**
4
 * This file is part of the miBadger package.
5
 *
6
 * @author Michael Webbers <[email protected]>
7
 * @license http://opensource.org/licenses/Apache-2.0 Apache v2 License
8
 * @version 1.0.0
9
 */
10
11
namespace miBadger\ActiveRecord;
12
13
/**
14
 * The abstract active record search class.
15
 *
16
 * @since 1.0.0
17
 */
18
abstract class AbstractActiveRecordSearch extends AbstractActiveRecord
19
{
20
	/**
21
	 * {@inheritdoc}
22
	 */
23 11
	public function search($where = [], $orderBy = [], $limit = -1, $offset = 0)
24
	{
25
		try {
26 11
			$pdoStatement = $this->getPdo()->prepare($this->getSearchQuery($where, $orderBy, $limit, $offset));
27 8
			array_walk_recursive($where, function(&$value) use ($pdoStatement) {
28 4
				static $index = 1;
29
30 4
				$pdoStatement->bindParam($index++, $value);
31 8
			});
32
33 8
			$pdoStatement->execute();
34 8
			$result = [];
35
36 8
			while ($fetch = $pdoStatement->fetch()) {
37 8
				$new = new static($this->getPdo());
38
39 8
				$new->setId(intval($fetch['id']));
40 8
				$new->setActiveRecordData($fetch);
41
42 8
				$result[] = $new;
43 8
			}
44
45 8
			return $result;
46 3
		} catch (\PDOException $e) {
47 1
			throw new ActiveRecordException(sprintf('Can not search the record in the `%s` table.', $this->getActiveRecordName()), 0, $e);
48
		}
49
	}
50
51
	/**
52
	 * Returns the search query with the given where, order by, limit and offset clauses.
53
	 *
54
	 * @param array $where = []
55
	 * @param array $orderBy = []
56
	 * @param int $limit = -1
57
	 * @param int $offset = 0
58
	 * @return string the search query with the given where, order by, limit and offset clauses.
59
	 */
60 11
	private function getSearchQuery($where = [], $orderBy = [], $limit = -1, $offset = 0)
61
	{
62 11
		return sprintf(
63 11
			'SELECT * FROM `%s` %s %s LIMIT %d OFFSET %d',
64 11
			$this->getActiveRecordName(),
65 11
			$this->getSearchQueryWhereClause($where),
66 9
			$this->getSearchQueryOrderByClause($orderBy),
67 9
			$limit,
68
			$offset
69 9
		);
70
	}
71
72
	/**
73
	 * Returns the search query where clause.
74
	 *
75
	 * @param array $where
76
	 * @return string the search query where clause.
77
	 */
78 11
	private function getSearchQueryWhereClause($where)
79
	{
80 11
		$columns = array_keys($this->getActiveRecordData());
81 11
		$columns[] = 'id';
82 11
		$result = [];
83
84 11
		foreach ($where as $key => $value) {
85 6
			if (!in_array($key, $columns)) {
86 1
				throw new ActiveRecordException(sprintf('Search option key `%s` does not exists.', $key));
87
			}
88
89 5
			if (is_numeric($value)) {
90 1
				$result[] = sprintf('`%s` = ?', $key);
91 5
			} elseif (is_string($value)) {
92 1
				$result[] = sprintf('`%s` LIKE ?', $key);
93 4
			} elseif (is_null($value)) {
94 1
				$result[] = sprintf('`%s` IS ?', $key);
95 3
			} elseif (is_array($value) && !empty($value)) {
96 1
				$result[] = sprintf('`%s` IN (%s)', $key, implode(',', array_fill(0, count($value), '?')));
97 1
			} else {
98 1
				throw new ActiveRecordException(sprintf('Search option value of key `%s` is not supported.', $key));
99
			}
100 9
		}
101
102 9
		return empty($result) ? '' : 'WHERE ' . implode(' AND ', $result);
103
	}
104
105
	/**
106
	 * Returns the search query order by clause.
107
	 *
108
	 * @param array $orderBy
109
	 * @return string the search query order by clause.
110
	 */
111 9
	private function getSearchQueryOrderByClause($orderBy)
112
	{
113 9
		$result = [];
114
115 9
		foreach ($orderBy as $key => $value) {
116 1
			$result[] = sprintf('`%s` %s', $key, $value == 'DESC' ? 'DESC' : 'ASC');
117 9
		}
118
119 9
		return empty($result) ? '' : 'ORDER BY ' . implode(', ', $result);
120
	}
121
}
122