Completed
Push — master ( ef48e3...95300d )
by Michael
02:58
created

Query::whereIn()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 14
Ratio 100 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 14
loc 14
ccs 8
cts 8
cp 1
rs 9.4285
cc 2
eloc 7
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\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 15
	public function __construct(\PDO $pdo, $table)
36
	{
37 15
		$this->pdo = $pdo;
38 15
		$this->bindings = [];
39 15
		$this->queryBuilder = new QueryBuilder($table);
40 15
	}
41
42
	/**
43
	 * Returns a string representation of the query object.
44
	 *
45
	 * @return string a string representation of the query object.
46
	 */
47 15
	public function __toString()
48
	{
49 15
		return $this->queryBuilder->__toString();
50
	}
51
52
	/**
53
	 * {@inheritdoc}
54
	 */
55 11
	public function select($columns = ['*'])
56
	{
57 11
		$this->queryBuilder->select($columns);
58
59 11
		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 5
	public function where($column, $operator, $value)
96
	{
97 5
		$binding = sprintf(':%s', $column);
98
99 5
		if ($operator == 'IN') {
100 1
			$this->whereIn($column, $value);
101 1
		} else {
102 4
			$this->bindings[$binding] = $value;
103 4
			$this->queryBuilder->where($column, $operator, $binding);
104
		}
105
106 5
		return $this;
107
	}
108
109
	/**
110
	 * Set an additional where in condition.
111
	 *
112
	 * @param string $column
113
	 * @param mixed $values
114
	 * @return $this
115
	 */
116 1 View Code Duplication
	private function whereIn($column, $values)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
117
	{
118 1
		$bindings = [];
119
120 1
		foreach ($values as $key => $value) {
121 1
			$bindings[] = $binding = sprintf(':%s%s', $column, $key);
122
123 1
			$this->bindings[$binding] = $value;
124 1
		}
125
126 1
		$this->queryBuilder->where($column, 'IN', $bindings);
127
128 1
		return $this;
129
	}
130
131
	/**
132
	 * {@inheritdoc}
133
	 */
134 1
	public function groupBy($column)
135
	{
136 1
		$this->queryBuilder->groupBy($column);
137
138 1
		return $this;
139
	}
140
141
	/**
142
	 * {@inheritdoc}
143
	 */
144 1
	public function orderBy($column, $order = null)
145
	{
146 1
		$this->queryBuilder->orderBy($column, $order);
147
148 1
		return $this;
149
	}
150
151
	/**
152
	 * {@inheritdoc}
153
	 */
154 2
	public function limit($limit)
155
	{
156 2
		$this->bindings[':limit'] = $limit;
157 2
		$this->queryBuilder->limit(':limit');
158
159 2
		return $this;
160
	}
161
162
	/**
163
	 * {@inheritdoc}
164
	 */
165 1
	public function offset($offset)
166
	{
167 1
		$this->bindings[':offset'] = $offset;
168 1
		$this->queryBuilder->offset(':offset');
169
170 1
		return $this;
171
	}
172
173
	/**
174
	 * Returns the result of the executed prepared query.
175
	 *
176
	 * @return QueryResult the result of the executed prepared query.
177
	 */
178 4
	public function execute()
179
	{
180 4
		$pdoStatement = $this->pdo->prepare((string) $this);
181 4
		$pdoStatement->execute($this->bindings);
182
183 4
		return new QueryResult($pdoStatement);
184
	}
185
186
	/**
187
	 * Returns the values array with bindings instead of values.
188
	 *
189
	 * @param array $values
190
	 * @return array the values array with bindings instead of values.
191
	 */
192 2 View Code Duplication
	private function replaceValuesWithBindings(array $values)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
193
	{
194 2
		$result = [];
195
196 2
		foreach ($values as $key => $value) {
197 2
			$binding = sprintf(':%s', $key);
198
199 2
			$this->bindings[$binding] = $value;
200 2
			$result[$key] = $binding;
201 2
		}
202
203 2
		return $result;
204
	}
205
}
206