Completed
Push — master ( aa57ef...7cb27e )
by Chris
03:03
created

Query::orders()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 15
rs 9.4285
cc 3
eloc 9
nc 3
nop 1
1
<?php
2
namespace Darya\Storage;
3
4
/**
5
 * Darya's storage query class.
6
 * 
7
 * TODO: Maybe make a query interface?
8
 * TODO: Formalise filters and orders?
9
 * 
10
 * @property-read bool     $distinct
11
 * @property-read string   $resource
12
 * @property-read array    $fields
13
 * @property-read string   $type
14
 * @property-read array    $data
15
 * @property-read array    $filter
16
 * @property-read array    $order
17
 * @property-read int|null $limit
18
 * @property-read int      $offset
19
 * 
20
 * @author Chris Andrew <[email protected]>
21
 */
22
class Query {
23
	
24
	const CREATE = 'create';
25
	const READ   = 'read';
26
	const UPDATE = 'update';
27
	const DELETE = 'delete';
28
	
29
	/**
30
	 * Determines whether to return unique resource data.
31
	 * 
32
	 * @var bool
33
	 */
34
	protected $distinct = false;
35
	
36
	/**
37
	 * The resource to query.
38
	 * 
39
	 * @var string
40
	 */
41
	protected $resource;
42
	
43
	/**
44
	 * The resource fields to retrieve.
45
	 * 
46
	 * @var array
47
	 */
48
	protected $fields;
49
	
50
	/**
51
	 * The type of the query.
52
	 * 
53
	 * @var string
54
	 */
55
	protected $type;
56
	
57
	/**
58
	 * The data to create or update resource data with.
59
	 * 
60
	 * @var array
61
	 */
62
	protected $data = array();
63
	
64
	/**
65
	 * Filters to apply to the query.
66
	 * 
67
	 * @var array
68
	 */
69
	protected $filter = array();
70
	
71
	/**
72
	 * Fields to sort resource data by.
73
	 * 
74
	 * @var array
75
	 */
76
	protected $order = array();
77
	
78
	/**
79
	 * Limits the number of rows to retrieve.
80
	 * 
81
	 * @var int
82
	 */
83
	protected $limit;
84
	
85
	/**
86
	 * Offsets the rows to retrieve.
87
	 * 
88
	 * @var int
89
	 */
90
	protected $offset;
91
	
92
	/**
93
	 * Instantiate a new storage query.
94
	 * 
95
	 * @param string       $resource
96
	 * @param array|string $fields   [optional]
97
	 */
98
	public function __construct($resource, $fields = array()) {
99
		$this->type   = static::READ;
100
		
101
		$this->resource = $resource;
102
		$this->fields = (array) $fields;
103
	}
104
	
105
	/**
106
	 * Change the type of this query to the given type with optional data.
107
	 * 
108
	 * @param mixed $type
109
	 * @param array $data [optional]
110
	 */
111
	protected function modify($type, array $data = array()) {
112
		$this->type = $type;
113
		$this->data = $data;
114
	}
115
	
116
	/**
117
	 * Set the query to result in all data.
118
	 * 
119
	 * @return $this
120
	 */
121
	public function all() {
122
		$this->distinct = false;
123
		
124
		return $this;
125
	}
126
	
127
	/**
128
	 * Set the query to result in unique data.
129
	 * 
130
	 * @return $this
131
	 */
132
	public function distinct() {
133
		$this->distinct = true;
134
		
135
		return $this;
136
	}
137
	
138
	/**
139
	 * Change the resource to be queried.
140
	 * 
141
	 * @param string $resource
142
	 * @return $this
143
	 */
144
	public function resource($resource) {
145
		$this->resource = $resource;
146
		
147
		return $this;
148
	}
149
	
150
	/**
151
	 * Change the resource fields to retrieve.
152
	 * 
153
	 * Used by read queries.
154
	 * 
155
	 * @param array|string $fields [optional]
156
	 * @return $this
157
	 */
158
	public function fields($fields = array()) {
159
		$this->fields = (array) $fields;
160
		
161
		return $this;
162
	}
163
	
164
	/**
165
	 * Make this a create query with the given data.
166
	 * 
167
	 * @param array $data
168
	 * @return $this
169
	 */
170
	public function create(array $data) {
171
		$this->modify(static::CREATE, $data);
172
		
173
		return $this;
174
	}
175
	
176
	/**
177
	 * Make this a read query.
178
	 * 
179
	 * Optionally accepts the resource fields to retrieve.
180
	 * 
181
	 * @param array|string $fields [optional]
182
	 * @return $this
183
	 */
184
	public function read($fields = array()) {
185
		$this->type = static::READ;
186
		
187
		if ($fields) {
188
			$this->fields($fields);
189
		}
190
		
191
		return $this;
192
	}
193
	
194
	/**
195
	 * Make this an update query with the given data.
196
	 * 
197
	 * @param array $data
198
	 * @return $this
199
	 */
200
	public function update(array $data) {
201
		$this->modify(static::UPDATE, $data);
202
		
203
		return $this;
204
	}
205
	
206
	/**
207
	 * Make this a delete query.
208
	 * 
209
	 * @return $this
210
	 */
211
	public function delete() {
212
		$this->type = static::DELETE;
213
		
214
		return $this;
215
	}
216
	
217
	/**
218
	 * Alias for create().
219
	 * 
220
	 * @param array $data
221
	 * @return $this
222
	 */
223
	public function insert(array $data) {
224
		$this->create($data);
225
		
226
		return $this;
227
	}
228
	
229
	/**
230
	 * Alias for read().
231
	 * 
232
	 * @param array|string $fields [optional]
233
	 * @return $this
234
	 */
235
	public function select($fields = array()) {
236
		$this->read($fields);
237
		
238
		return $this;
239
	}
240
	
241
	/**
242
	 * Add a filter condition to the query.
243
	 * 
244
	 * @param string $field
245
	 * @param mixed  $value [optional]
246
	 * @return $this
247
	 */
248
	public function filter($field, $value = null) {
249
		$this->filter = array_merge($this->filter, array($field => $value));
250
		
251
		return $this;
252
	}
253
	
254
	/**
255
	 * Add multiple filter conditions to the query.
256
	 * 
257
	 * @param array $filters
258
	 * @return $this
259
	 */
260
	public function filters(array $filters = array()) {
261
		$this->filter = array_merge($this->filter, $filters);
262
		
263
		return $this;
264
	}
265
	
266
	/**
267
	 * Alias for filter().
268
	 * 
269
	 * @param string $field
270
	 * @param mixed  $value [optional]
271
	 * @return $this
272
	 */
273
	public function where($field, $value = null) {
274
		$this->filter($field, $value);
275
		
276
		return $this;
277
	}
278
	
279
	/**
280
	 * Add an order condition to the query.
281
	 * 
282
	 * $order can be 'asc' or 'desc'.
283
	 * 
284
	 * @param string $field
285
	 * @param string $order [optional]
286
	 * @return $this
287
	 */
288
	public function order($field, $order = 'asc') {
289
		$this->order = array_merge($this->order, array($field => $order));
290
		
291
		return $this;
292
	}
293
	
294
	/**
295
	 * Add multiple order conditions to the query.
296
	 * 
297
	 * TODO: Simplify.
298
	 * 
299
	 * @param array $orders
300
	 * @return $this
301
	 */
302
	public function orders(array $orders = array()) {
303
		$prepared = array();
304
		
305
		foreach ($orders as $field => $order) {
306
			if (is_numeric($field)) {
307
				$prepared[$order] = 'asc';
308
			} else {
309
				$prepared[$field] = $order;
310
			}
311
		}
312
		
313
		$this->order = array_merge($this->order, $prepared);
314
		
315
		return $this;
316
	}
317
	
318
	/**
319
	 * Alias for order().
320
	 * 
321
	 * @param string $field
322
	 * @param string $order [optional]
323
	 */
324
	public function sort($field, $order = 'asc') {
325
		$this->order($field, $order);
326
		
327
		return $this;
328
	}
329
	
330
	/**
331
	 * Set a limit on the query.
332
	 * 
333
	 * An optional offset can be passed as the second parameter.
334
	 * 
335
	 * @param int $limit
336
	 * @param int $offset [optional]
337
	 * @return $this
338
	 */
339
	public function limit($limit, $offset = 0) {
340
		$this->limit = $limit;
341
		
342
		if (is_numeric($offset)) {
343
			$this->offset = (int) $offset;
344
		}
345
		
346
		return $this;
347
	}
348
	
349
	/**
350
	 * Set an offset on the query.
351
	 * 
352
	 * @param int $offset
353
	 * @return this
354
	 */
355
	public function offset($offset) {
356
		$this->offset = (int) $offset;
357
		
358
		return $this;
359
	}
360
	
361
	/**
362
	 * Alias for offset().
363
	 * 
364
	 * @param int $offset
365
	 * @return $this
366
	 */
367
	public function skip($offset) {
368
		$this->offset($offset);
369
		
370
		return $this;
371
	}
372
	
373
	/**
374
	 * Dynamically retrieve a property.
375
	 * 
376
	 * @param string $property
377
	 */
378
	public function __get($property) {
379
		if (property_exists($this, $property)) {
380
			return $this->$property;
381
		}
382
	}
383
}
384