Completed
Push — master ( a1f77e...83cdfb )
by Chris
02:54
created

Query::unique()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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