Passed
Push — v2 ( 18d8b1...a0d121 )
by Berend
02:30
created

ActiveRecordQuery::getIterator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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
 */
9
10
namespace miBadger\ActiveRecord;
11
12
use miBadger\Query\Query;
13
use miBadger\Query\QueryInterface;
14
use miBadger\Query\QueryExpression;
15
16
/**
17
 * The active record exception class.
18
 *
19
 * @since 2.0.0
20
 */
21
class ActiveRecordQuery implements \IteratorAggregate
22
{
23
24
	private $clauses = [];
25
26
	private $query;
27
28
	private $results;
29
30
	private $table;
31
32
	private $type;
33
	
34
	private $whereExpression = null;
35
36 17
	public function __construct(AbstractActiveRecord $instance, $table, Array $additionalWhereClauses)
37
	{
38 17
		$this->table = $table;
39 17
		$this->query = new Query($instance->getPdo(), $table);
40 17
		$this->type = $instance;
41 17
		$this->clauses = $additionalWhereClauses;
42 17
	}
43
44 16
	private function execute()
45
	{
46 16
		$clauses = $this->clauses;
47
48
		// Optionally add user concatenated where expression
49 16
		if ($this->whereExpression !== null)
50
		{
51 9
			$clauses[] = $this->whereExpression;
52
		}
53
54
		// Construct where clause
55 16
		if (count($clauses) == 1)
56
		{
57 10
			$this->query->where($clauses[0]);
58 6
		} else if (count($clauses) >= 2)
59
		{
60
			$rest = array_slice($clauses, 1);
61
			$this->query->where(Query::And($clauses[0], ...$rest));
62
		}
63
64 16
		$this->query->select();
65
66 16
		$this->results = $this->query->execute();
67
68 13
		return $this;
69
	}
70
71
	public function getIterator()
72
	{
73
		return new \ArrayIterator($this->fetchAll());
74
	}
75
76 13
	public function fetchAll()
77
	{
78
		try {
79
			// TODO: Should execute call be explicit?
80 13
			$this->execute();
81
82 11
			$typedResults = [];
83
84 11
			$entries = $this->results->fetchAll();
85 11
			if ($entries === false) {
86
				throw new ActiveRecordException(sprintf('Can not search one non-existent entry from the `%s` table.', $this->table));
87
			}
88
89 11
			foreach ($entries as $entry) {
90 11
				$typedEntry = clone $this->type;
91 11
				$typedEntry->fill($entry);
92 11
				$typedResults[] = $typedEntry;
93
			}
94
95 11
			return $typedResults;
96 2
		} catch (\PDOException $e) {
97 2
			throw new ActiveRecordException($e->getMessage(), 0, $e);
98
		}
99
	}
100
101 3
	public function fetch()
102
	{
103
		try {
104 3
			$this->execute();
105
106 2
			$typedResult = clone $this->type;
107
108 2
			$entry = $this->results->fetch();
109 2
			if ($entry === false) {
110 1
				throw new ActiveRecordException(sprintf('Can not search one non-existent entry from the `%s` table.', $this->table));
111
			}
112
113 1
			$typedResult->fill($entry);
114
115 1
			return $typedResult;
116 2
		} catch (\PDOException $e) {
117 1
			throw new ActiveRecordException($e->getMessage(), 0, $e);
118
		}
119
	}
120
121
122
	/**
123
	 * Set the where condition
124
	 *
125
	 * @param QueryExpression $expression the query expression
126
	 * @return $this
127
	 * @see https://en.wikipedia.org/wiki/SQL#Operators
128
	 * @see https://en.wikipedia.org/wiki/Where_(SQL)
129
	 */
130 9
	public function where(QueryExpression $expression)
131
	{
132 9
		$this->whereExpression = $expression;
133 9
		return $this;
134
	}
135
136
	/**
137
	 * Set an additional group by.
138
	 *
139
	 * @param string $column
140
	 * @return $this
141
	 * @see https://en.wikipedia.org/wiki/SQL#Queries
142
	 */
143
	public function groupBy($column)
144
	{
145
		$this->query->groupBy($column);
146
		return $this;
147
	}
148
149
	/**
150
	 * Set an additional order condition.
151
	 *
152
	 * @param string $column
153
	 * @param string|null $order
154
	 * @return $this
155
	 * @see https://en.wikipedia.org/wiki/SQL#Queries
156
	 * @see https://en.wikipedia.org/wiki/Order_by
157
	 */
158 1
	public function orderBy($column, $order = null)
159
	{
160 1
		$this->query->orderBy($column, $order);	
161 1
		return $this;
162
	}
163
164
	/**
165
	 * Set the limit.
166
	 *
167
	 * @param mixed $limit
168
	 * @return $this
169
	 */
170 2
	public function limit($limit)
171
	{
172 2
		$this->query->limit($limit);
173 2
		return $this;
174
	}
175
176
	/**
177
	 * Set the offset.
178
	 *
179
	 * @param mixed $offset
180
 	 * @return $this
181
	 */
182 1
	public function offset($offset)
183
	{
184 1
		$this->query->offset($offset);
185 1
		return $this;
186
	}
187
}
188