Completed
Push — master ( c609bc...b05d4c )
by Michael
05:07 queued 02:06
created

getSearchQueryLimitClause()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 2
crap 2
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 %s',
64 11
			$this->getActiveRecordName(),
65 11
			$this->getSearchQueryWhereClause($where),
66 9
			$this->getSearchQueryOrderByClause($orderBy),
67 9
			$this->getSearchQueryLimitClause($limit, $offset)
68 9
		);
69
	}
70
71
	/**
72
	 * Returns the search query where clause.
73
	 *
74
	 * @param array $where
75
	 * @return string the search query where clause.
76
	 */
77 11
	private function getSearchQueryWhereClause($where)
78
	{
79 11
		$columns = array_keys($this->getActiveRecordData());
80 11
		$columns[] = 'id';
81 11
		$result = [];
82
83 11
		foreach ($where as $key => $value) {
84 6
			if (!in_array($key, $columns)) {
85 1
				throw new ActiveRecordException(sprintf('Search option key `%s` does not exists.', $key));
86
			}
87
88 5
			if (is_numeric($value)) {
89 1
				$result[] = sprintf('`%s` = ?', $key);
90 5
			} elseif (is_string($value)) {
91 1
				$result[] = sprintf('`%s` LIKE ?', $key);
92 4
			} elseif (is_null($value)) {
93 1
				$result[] = sprintf('`%s` IS ?', $key);
94 3
			} elseif (is_array($value) && !empty($value)) {
95 1
				$result[] = sprintf('`%s` IN (%s)', $key, implode(',', array_fill(0, count($value), '?')));
96 1
			} else {
97 1
				throw new ActiveRecordException(sprintf('Search option value of key `%s` is not supported.', $key));
98
			}
99 9
		}
100
101 9
		return empty($result) ? '' : 'WHERE ' . implode(' AND ', $result);
102
	}
103
104
	/**
105
	 * Returns the search query order by clause.
106
	 *
107
	 * @param array $orderBy
108
	 * @return string the search query order by clause.
109
	 */
110 9
	private function getSearchQueryOrderByClause($orderBy)
111
	{
112 9
		$result = [];
113
114 9
		foreach ($orderBy as $key => $value) {
115 1
			$result[] = sprintf('`%s` %s', $key, $value == 'DESC' ? 'DESC' : 'ASC');
116 9
		}
117
118 9
		return empty($result) ? '' : 'ORDER BY ' . implode(', ', $result);
119
	}
120
121
	/**
122
	 * Returns the search query limit and clause.
123
	 *
124
	 * @param int $limit = -1
125
	 * @param int $offset = 0
126
	 * @return string the search query limit and clause.
127
	 */
128 9
	private function getSearchQueryLimitClause($limit, $offset)
129
	{
130 9
		if ($limit == -1) {
131 7
			return '';
132
		}
133
134 2
		return sprintf('LIMIT %d OFFSET %d', $limit, $offset);
135
	}
136
}
137