Completed
Push — develop ( 7995ec...fbffd5 )
by Michael
02:27
created

AbstractActiveRecordSearch::search()   B

Complexity

Conditions 3
Paths 12

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 3

Importance

Changes 5
Bugs 0 Features 2
Metric Value
c 5
b 0
f 2
dl 0
loc 27
ccs 17
cts 17
cp 1
rs 8.8571
cc 3
eloc 16
nc 12
nop 4
crap 3
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 = [], $sort = [], $limit = -1, $offset = 0)
24
	{
25
		try {
26 11
			$pdoStatement = $this->getPdo()->prepare($this->getSearchQuery($where, $sort, $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 options.
53
	 *
54
	 * @param array $where = []
55
	 * @return string the search query with the given options.
56
	 */
57 11
	private function getSearchQuery($where = [], $sort = [], $limit = -1, $offset = 0)
58
	{
59 11
		$columns = array_keys($this->getActiveRecordData());
60 11
		$columns[] = 'id';
61 11
		$whereValues = [];
62 11
		$sortValues = [];
63
64 11
		foreach ($where as $key => $value) {
65 6
			if (!in_array($key, $columns)) {
66 1
				throw new ActiveRecordException(sprintf('Search option key `%s` does not exists.', $key));
67
			}
68
69 5
			if (is_numeric($value)) {
70 1
				$whereValues[] = sprintf('`%s` = ?', $key);
71 5
			} elseif (is_string($value)) {
72 1
				$whereValues[] = sprintf('`%s` LIKE ?', $key);
73 4
			} elseif (is_null($value)) {
74 1
				$whereValues[] = sprintf('`%s` IS ?', $key);
75 3
			} elseif (is_array($value) && !empty($value)) {
76 1
				$whereValues[] = sprintf('`%s` IN (%s)', $key, implode(',', array_fill(0, count($value), '?')));
77 1
			} else {
78 1
				throw new ActiveRecordException(sprintf('Search option value of key `%s` is not supported.', $key));
79
			}
80 9
		}
81
82 9
		foreach ($sort as $key => $value) {
83 1
			$sortValues[] = sprintf('`%s` %s', $key, $value == 'DESC' ? 'DESC' : 'ASC');
84 9
		}
85
86 9
		$where = empty($whereValues) ? '' : 'WHERE ' . implode(' AND ', $whereValues);
87 9
		$orderby = empty($sortValues) ? '' : 'ORDER BY ' . implode(', ', $sortValues);
88
89 9
		return sprintf('SELECT * FROM `%s` %s %s LIMIT %d OFFSET %d', $this->getActiveRecordName(), $where, $orderby, $limit, $offset);
90
	}
91
}
92