Completed
Push — develop ( 8401cc...e4ca03 )
by Timothy
01:58
created

State::getOffset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
/**
3
 * Query
4
 *
5
 * SQL Query Builder / Database Abstraction Layer
6
 *
7
 * PHP version 7.1
8
 *
9
 * @package     Query
10
 * @author      Timothy J. Warren <[email protected]>
11
 * @copyright   2012 - 2018 Timothy J. Warren
12
 * @license     http://www.opensource.org/licenses/mit-license.html  MIT License
13
 * @link        https://git.timshomepage.net/aviat4ion/Query
14
 */
15
namespace Query;
16
17
/**
18
 * Query builder state
19
 */
20
class State {
21
	// --------------------------------------------------------------------------
22
	// ! SQL Clause Strings
23
	// --------------------------------------------------------------------------
24
25
	/**
26
	 * Compiled 'select' clause
27
	 * @var string
28
	 */
29
	protected $selectString = '';
30
31
	/**
32
	 * Compiled 'from' clause
33
	 * @var string
34
	 */
35
	protected $fromString = '';
36
37
	/**
38
	 * Compiled arguments for insert / update
39
	 * @var string
40
	 */
41
	protected $setString = '';
42
43
	/**
44
	 * Order by clause
45
	 * @var string
46
	 */
47
	protected $orderString = '';
48
49
	/**
50
	 * Group by clause
51
	 * @var string
52
	 */
53
	protected $groupString = '';
54
55
	// --------------------------------------------------------------------------
56
	// ! SQL Clause Arrays
57
	// --------------------------------------------------------------------------
58
59
	/**
60
	 * Keys for insert/update statement
61
	 * @var array
62
	 */
63
	protected $setArrayKeys = [];
64
65
	/**
66
	 * Key/val pairs for order by clause
67
	 * @var array
68
	 */
69
	protected $orderArray = [];
70
71
	/**
72
	 * Key/val pairs for group by clause
73
	 * @var array
74
	 */
75
	protected $groupArray = [];
76
77
	// --------------------------------------------------------------------------
78
	// ! Other Class vars
79
	// --------------------------------------------------------------------------
80
81
	/**
82
	 * Values to apply to prepared statements
83
	 * @var array
84
	 */
85
	protected $values = [];
86
87
	/**
88
	 * Values to apply to where clauses in prepared statements
89
	 * @var array
90
	 */
91
	protected $whereValues = [];
92
93
	/**
94
	 * Value for limit string
95
	 * @var int
96
	 */
97
	protected $limit;
98
99
	/**
100
	 * Value for offset in limit string
101
	 * @var string|false
102
	 */
103
	protected $offset = FALSE;
104
105
	/**
106
	 * Query component order mapping
107
	 * for complex select queries
108
	 *
109
	 * Format:
110
	 * array(
111
	 *		'type' => 'where',
112
	 *		'conjunction' => ' AND ',
113
	 *		'string' => 'k=?'
114
	 * )
115
	 *
116
	 * @var array
117
	 */
118
	protected $queryMap = [];
119
120
	/**
121
	 * Map for having clause
122
	 * @var array
123
	 */
124
	protected $havingMap = [];
125
126
	// --------------------------------------------------------------------------
127
128
    /**
129
     * @param string $str
130
     * @return State
131
     */
132
	public function setSelectString(string $str): self
133
    {
134
        $this->selectString = $str;
135
        return $this;
136
    }
137
138
	/**
139
	 * @return string
140
	 */
141
    public function getSelectString(): string
142
	{
143
		return $this->selectString;
144
	}
145
146
	/**
147
	 * @param string $str
148
	 * @return State
149
	 */
150
    public function appendSelectString(string $str): self
151
	{
152
		$this->selectString .= $str;
153
		return $this;
154
	}
155
156
	/**
157
	 * @return string
158
	 */
159
	public function getFromString(): string
160
	{
161
		return $this->fromString;
162
	}
163
164
	/**
165
	 * @param string $fromString
166
	 * @return State
167
	 */
168
	public function setFromString(string $fromString): State
169
	{
170
		$this->fromString = $fromString;
171
		return $this;
172
	}
173
174
	/**
175
	 * @return string
176
	 */
177
	public function getSetString(): string
178
	{
179
		return $this->setString;
180
	}
181
182
	/**
183
	 * @param string $setString
184
	 * @return State
185
	 */
186
	public function setSetString(string $setString): State
187
	{
188
		$this->setString = $setString;
189
		return $this;
190
	}
191
192
	/**
193
	 * @return string
194
	 */
195
	public function getOrderString(): string
196
	{
197
		return $this->orderString;
198
	}
199
200
	/**
201
	 * @param string $orderString
202
	 * @return State
203
	 */
204
	public function setOrderString(string $orderString): State
205
	{
206
		$this->orderString = $orderString;
207
		return $this;
208
	}
209
210
	/**
211
	 * @return string
212
	 */
213
	public function getGroupString(): string
214
	{
215
		return $this->groupString;
216
	}
217
218
	/**
219
	 * @param string $groupString
220
	 * @return State
221
	 */
222
	public function setGroupString(string $groupString): State
223
	{
224
		$this->groupString = $groupString;
225
		return $this;
226
	}
227
228
	/**
229
	 * @return array
230
	 */
231
	public function getSetArrayKeys(): array
232
	{
233
		return $this->setArrayKeys;
234
	}
235
236
	/**
237
	 * @param array $setArrayKeys
238
	 * @return State
239
	 */
240
	public function appendSetArrayKeys(array $setArrayKeys): State
241
	{
242
		$this->setArrayKeys = array_merge($this->setArrayKeys, $setArrayKeys);
243
		return $this;
244
	}
245
246
	/**
247
	 * @param array $setArrayKeys
248
	 * @return State
249
	 */
250
	public function setSetArrayKeys(array $setArrayKeys): State
251
	{
252
		$this->setArrayKeys = $setArrayKeys;
253
		return $this;
254
	}
255
256
	/**
257
	 * @return array
258
	 */
259
	public function getOrderArray(): array
260
	{
261
		return $this->orderArray;
262
	}
263
264
	/**
265
	 * @param string $key
266
	 * @param mixed $orderArray
267
	 * @return State
268
	 */
269
	public function setOrderArray(string $key, $orderArray): State
270
	{
271
		$this->orderArray[$key] = $orderArray;
272
		return $this;
273
	}
274
275
	/**
276
	 * @return array
277
	 */
278
	public function getGroupArray(): array
279
	{
280
		return $this->groupArray;
281
	}
282
283
	/**
284
	 * @param array $groupArray
285
	 * @return State
286
	 */
287
	public function setGroupArray(array $groupArray): State
288
	{
289
		$this->groupArray = $groupArray;
290
		return $this;
291
	}
292
293
	/**
294
	 * @param string $groupArray
295
	 * @return State
296
	 */
297
	public function appendGroupArray(string $groupArray): State
298
	{
299
		$this->groupArray[] = $groupArray;
300
		return $this;
301
	}
302
303
	/**
304
	 * @return array
305
	 */
306
	public function getValues(): array
307
	{
308
		return $this->values;
309
	}
310
311
	/**
312
	 * @param array $values
313
	 * @return State
314
	 */
315
	public function appendValues(array $values): State
316
	{
317
		$this->values = array_merge($this->values, $values);
318
		return $this;
319
	}
320
321
	/**
322
	 * @return array
323
	 */
324
	public function getWhereValues(): array
325
	{
326
		return $this->whereValues;
327
	}
328
329
	/**
330
	 * @param mixed $val
331
	 * @return State
332
	 */
333
	public function appendWhereValues($val): State
334
	{
335
		if (\is_array($val))
336
		{
337
			foreach($val as $v)
338
			{
339
				$this->whereValues[] = $v;
340
			}
341
342
			return $this;
343
		}
344
345
		$this->whereValues[] = $val;
346
		return $this;
347
	}
348
349
	/**
350
	 * @return int
351
	 */
352
	public function getLimit(): ?int
353
	{
354
		return $this->limit;
355
	}
356
357
	/**
358
	 * @param int $limit
359
	 * @return State
360
	 */
361
	public function setLimit(int $limit): State
362
	{
363
		$this->limit = $limit;
364
		return $this;
365
	}
366
367
	/**
368
	 * @return string|false
369
	 */
370
	public function getOffset()
371
	{
372
		return $this->offset;
373
	}
374
375
	/**
376
	 * @param string|false $offset
377
	 * @return State
378
	 */
379
	public function setOffset($offset): State
380
	{
381
		$this->offset = $offset;
382
		return $this;
383
	}
384
385
	/**
386
	 * @return array
387
	 */
388
	public function getQueryMap(): array
389
	{
390
		return $this->queryMap;
391
	}
392
393
	/**
394
	 * Add an additional set of mapping pairs to a internal map
395
	 *
396
	 * @param string $conjunction
397
	 * @param string $string
398
	 * @param string $type
399
	 * @return State
400
	 */
401
	public function appendMap(string $conjunction = '', string $string = '', string $type = ''): self
402
	{
403
		$this->queryMap[] = [
404
			'type' => $type,
405
			'conjunction' => $conjunction,
406
			'string' => $string
407
		];
408
		return $this;
409
	}
410
411
	/**
412
	 * @return array
413
	 */
414
	public function getHavingMap(): array
415
	{
416
		return $this->havingMap;
417
	}
418
419
	/**
420
	 * @param array $item
421
	 * @return State
422
	 */
423
	public function appendHavingMap(array $item): State
424
	{
425
		$this->havingMap[] = $item;
426
		return $this;
427
	}
428
}
429