Completed
Push — master ( 98f87b...ef48e3 )
by Michael
05:41 queued 02:12
created

Query::orderBy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
crap 1
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\Query;
12
13
/**
14
 * The query class.
15
 *
16
 * @since 1.0.0
17
 */
18
class Query implements QueryInterface
19
{
20
	/* @var \PDO The PDO. */
21
	private $pdo;
22
23
	/* @var array The bindings. */
24
	private $bindings;
25
26
	/* @var QueryBuilder The query builder. */
27
	private $queryBuilder;
28
29
	/**
30
	 * Construct a query object with the given pdo and table.
31
	 *
32
	 * @param \PDO $pdo
33
	 * @param string $table
34
	 */
35 14
	public function __construct(\PDO $pdo, $table)
36
	{
37 14
		$this->pdo = $pdo;
38 14
		$this->bindings = [];
39 14
		$this->queryBuilder = new QueryBuilder($table);
40 14
	}
41
42
	/**
43
	 * Returns a string representation of the query object.
44
	 *
45
	 * @return string a string representation of the query object.
46
	 */
47 14
	public function __toString()
48
	{
49 14
		return $this->queryBuilder->__toString();
50
	}
51
52
	/**
53
	 * {@inheritdoc}
54
	 */
55 10
	public function select($columns = ['*'])
56
	{
57 10
		$this->queryBuilder->select($columns);
58
59 10
		return $this;
60
	}
61
62
	/**
63
	 * {@inheritdoc}
64
	 */
65 1
	public function insert(array $values)
66
	{
67 1
		$this->queryBuilder->insert($this->replaceValuesWithBindings($values));
68
69 1
		return $this;
70
	}
71
72
	/**
73
	 * {@inheritdoc}
74
	 */
75 1
	public function update(array $values)
76
	{
77 1
		$this->queryBuilder->update($this->replaceValuesWithBindings($values));
78
79 1
		return $this;
80
	}
81
82
	/**
83
	 * {@inheritdoc}
84
	 */
85 1
	public function delete()
86
	{
87 1
		$this->queryBuilder->delete();
88
89 1
		return $this;
90
	}
91
92
	/**
93
	 * {@inheritdoc}
94
	 */
95 4
	public function where($column, $operator, $value)
96
	{
97 4
		$binding = sprintf(':%s', $column);
98
99 4
		$this->bindings[$binding] = $value;
100 4
		$this->queryBuilder->where($column, $operator, $binding);
101
102 4
		return $this;
103
	}
104
105
	/**
106
	 * {@inheritdoc}
107
	 */
108 1
	public function groupBy($column)
109
	{
110 1
		$this->queryBuilder->groupBy($column);
111
112 1
		return $this;
113
	}
114
115
	/**
116
	 * {@inheritdoc}
117
	 */
118 1
	public function orderBy($column, $order = null)
119
	{
120 1
		$this->queryBuilder->orderBy($column, $order);
121
122 1
		return $this;
123
	}
124
125
	/**
126
	 * {@inheritdoc}
127
	 */
128 2
	public function limit($limit)
129
	{
130 2
		$this->bindings[':limit'] = $limit;
131 2
		$this->queryBuilder->limit(':limit');
132
133 2
		return $this;
134
	}
135
136
	/**
137
	 * {@inheritdoc}
138
	 */
139 1
	public function offset($offset)
140
	{
141 1
		$this->bindings[':offset'] = $offset;
142 1
		$this->queryBuilder->offset(':offset');
143
144 1
		return $this;
145
	}
146
147
	/**
148
	 * Returns the result of the executed prepared query.
149
	 *
150
	 * @return QueryResult the result of the executed prepared query.
151
	 */
152 4
	public function execute()
153
	{
154 4
		$pdoStatement = $this->pdo->prepare((string) $this);
155 4
		$pdoStatement->execute($this->bindings);
156
157 4
		return new QueryResult($pdoStatement);
158
	}
159
160
	/**
161
	 * Returns the values array with bindings instead of values.
162
	 *
163
	 * @param array $values
164
	 * @return array the values array with bindings instead of values.
165
	 */
166 2
	private function replaceValuesWithBindings(array $values)
167
	{
168 2
		$result = [];
169
170 2
		foreach ($values as $key => $value) {
171 2
			$binding = sprintf(':%s', $key);
172
173 2
			$this->bindings[$binding] = $value;
174 2
			$result[$key] = $binding;
175 2
		}
176
177 2
		return $result;
178
	}
179
}
180