Completed
Push — master ( 20ce86...00401a )
by Chris
02:49
created

Query::read()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
dl 0
loc 9
c 2
b 0
f 1
rs 9.6666
cc 2
eloc 5
nc 2
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: Standardised operators? Think about how this will affect translators.
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
	 * @param array        $filter   [optional]
98
	 * @param array        $order    [optional]
99
	 * @param int          $limit    [optional]
100
	 * @param int          $offset   [optional]
101
	 */
102
	public function __construct($resource, $fields = array(), array $filter = array(), array $order = array(), $limit = null, $offset = 0) {
103
		$this->type   = static::READ;
104
		
105
		$this->resource = $resource;
106
		$this->fields = (array) $fields;
107
		$this->filter = $filter;
108
		$this->order  = $order;
109
		$this->limit  = $limit;
110
		$this->offset = (int) $offset;
111
	}
112
	
113
	/**
114
	 * Change the type of this query to the given type with optional data.
115
	 * 
116
	 * @param mixed $type
117
	 * @param array $data [optional]
118
	 */
119
	protected function modify($type, array $data = array()) {
120
		$this->type = $type;
121
		$this->data = $data;
122
	}
123
	
124
	/**
125
	 * Set the query to result in all data.
126
	 * 
127
	 * @return $this
128
	 */
129
	public function all() {
130
		$this->distinct = false;
131
		
132
		return $this;
133
	}
134
	
135
	/**
136
	 * Set the query to result in unique data.
137
	 * 
138
	 * @return $this
139
	 */
140
	public function distinct() {
141
		$this->distinct = true;
142
		
143
		return $this;
144
	}
145
	
146
	/**
147
	 * Change the resource to be queried.
148
	 * 
149
	 * @param string $resource
150
	 * @return $this
151
	 */
152
	public function resource($resource) {
153
		$this->resource = $resource;
154
		
155
		return $this;
156
	}
157
	
158
	/**
159
	 * Change the resource fields to retrieve.
160
	 * 
161
	 * Used by read queries.
162
	 * 
163
	 * @param array|string $fields [optional]
164
	 * @return $this
165
	 */
166
	public function fields($fields = array()) {
167
		$this->fields = (array) $fields;
168
		
169
		return $this;
170
	}
171
	
172
	/**
173
	 * Make this a create query with the given data.
174
	 * 
175
	 * @param array $data
176
	 * @return $this
177
	 */
178
	public function create(array $data) {
179
		$this->modify(static::CREATE, $data);
180
		
181
		return $this;
182
	}
183
	
184
	/**
185
	 * Make this a read query.
186
	 * 
187
	 * Optionally accepts the resource fields to retrieve.
188
	 * 
189
	 * @param array|string $fields [optional]
190
	 * @return $this
191
	 */
192
	public function read($fields = array()) {
193
		$this->type = static::READ;
194
		
195
		if ($fields) {
196
			$this->fields($fields);
197
		}
198
		
199
		return $this;
200
	}
201
	
202
	/**
203
	 * Make this an update query with the given data.
204
	 * 
205
	 * @param array $data
206
	 * @return $this
207
	 */
208
	public function update(array $data) {
209
		$this->modify(static::UPDATE, $data);
210
		
211
		return $this;
212
	}
213
	
214
	/**
215
	 * Make this a delete query.
216
	 * 
217
	 * @return $this
218
	 */
219
	public function delete() {
220
		$this->type = static::DELETE;
221
		
222
		return $this;
223
	}
224
	
225
	/**
226
	 * Alias for create().
227
	 * 
228
	 * @param array $data
229
	 * @return $this
230
	 */
231
	public function insert(array $data) {
232
		$this->create($data);
233
		
234
		return $this;
235
	}
236
	
237
	/**
238
	 * Alias for read().
239
	 * 
240
	 * @param array|string $fields [optional]
241
	 * @return $this
242
	 */
243
	public function select($fields = array()) {
244
		$this->read($fields);
245
		
246
		return $this;
247
	}
248
	
249
	/**
250
	 * Add a filter condition to the query.
251
	 * 
252
	 * @param string $field
253
	 * @param mixed  $value [optional]
254
	 * @return $this
255
	 */
256
	public function filter($field, $value = null) {
257
		$this->filter = array_merge($this->filter, array($field => $value));
258
		
259
		return $this;
260
	}
261
	
262
	/**
263
	 * Alias for filter().
264
	 * 
265
	 * @param string $field
266
	 * @param mixed  $value [optional]
267
	 * @return $this
268
	 */
269
	public function where($field, $value = null) {
270
		$this->filter($field, $value);
271
		
272
		return $this;
273
	}
274
	
275
	/**
276
	 * Add an order condition to the query.
277
	 * 
278
	 * $order can be 'asc' or 'desc'.
279
	 * 
280
	 * @param string $field
281
	 * @param string $order [optional]
282
	 * @return $this
283
	 */
284
	public function order($field, $order = 'asc') {
285
		$this->order = array_merge($this->order, array($field => $order));
286
		
287
		return $this;
288
	}
289
	
290
	/**
291
	 * Alias for order().
292
	 * 
293
	 * @param string $field
294
	 * @param string $order [optional]
295
	 */
296
	public function sort($field, $order = 'asc') {
297
		$this->order($field, $order);
298
		
299
		return $this;
300
	}
301
	
302
	/**
303
	 * Set a limit on the query.
304
	 * 
305
	 * An optional offset can be passed as the second parameter.
306
	 * 
307
	 * @param int $limit
308
	 * @param int $offset [optional]
309
	 * @return $this
310
	 */
311
	public function limit($limit, $offset = 0) {
312
		$this->limit = $limit;
313
		
314
		if (is_numeric($offset)) {
315
			$this->offset = (int) $offset;
316
		}
317
		
318
		return $this;
319
	}
320
	
321
	/**
322
	 * Set an offset on the query.
323
	 * 
324
	 * @param int $offset
325
	 * @return this
326
	 */
327
	public function offset($offset) {
328
		$this->offset = (int) $offset;
329
		
330
		return $this;
331
	}
332
	
333
	/**
334
	 * Alias for offset().
335
	 * 
336
	 * @param int $offset
337
	 * @return $this
338
	 */
339
	public function skip($offset) {
340
		$this->offset($offset);
341
		
342
		return $this;
343
	}
344
	
345
	/**
346
	 * Dynamically retrieve a property.
347
	 * 
348
	 * @param string $property
349
	 */
350
	public function __get($property) {
351
		if (property_exists($this, $property)) {
352
			return $this->$property;
353
		}
354
	}
355
}
356