1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @author Jared King <[email protected]> |
5
|
|
|
* |
6
|
|
|
* @link http://jaredtking.com |
7
|
|
|
* |
8
|
|
|
* @copyright 2015 Jared King |
9
|
|
|
* @license MIT |
10
|
|
|
*/ |
11
|
|
|
namespace JAQB\Statement; |
12
|
|
|
|
13
|
|
|
class OrderStatement extends Statement |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var bool |
17
|
|
|
*/ |
18
|
|
|
protected $groupBy; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
protected $fields = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param bool $groupBy when true, statement becomes a group by statement |
27
|
|
|
*/ |
28
|
|
|
public function __construct($groupBy = false) |
29
|
|
|
{ |
30
|
|
|
$this->groupBy = $groupBy; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Tells whether this statement is a GROUP BY statement. |
35
|
|
|
* |
36
|
|
|
* @return bool true: is group by, false: is order by |
37
|
|
|
*/ |
38
|
|
|
public function isGroupBy() |
39
|
|
|
{ |
40
|
|
|
return $this->groupBy; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Adds fields to this statement |
45
|
|
|
* Support input styles: |
46
|
|
|
* - addFields('field ASC,field2') |
47
|
|
|
* - addFields('field', 'ASC') |
48
|
|
|
* - addFields(['field','field2'], 'DESC') |
49
|
|
|
* - addFields([['field','ASC'], ['field2','ASC']]). |
50
|
|
|
* |
51
|
|
|
* @param string|array $fields |
52
|
|
|
* @param string $direction direction for fields where direction is unspecified (optional) |
53
|
|
|
* |
54
|
|
|
* @return self |
55
|
|
|
*/ |
56
|
|
|
public function addFields($fields, $direction = false) |
57
|
|
|
{ |
58
|
|
|
if (!is_array($fields)) { |
59
|
|
|
$fields = array_map(function ($f) { |
60
|
|
|
return trim($f); |
61
|
|
|
}, explode(',', $fields)); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
foreach ($fields as $field) { |
65
|
|
|
if (!is_array($field)) { |
66
|
|
|
$field = explode(' ', trim($field)); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if (count($field) === 1 && $direction !== false) { |
70
|
|
|
$field[] = $direction; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
// validate the direction |
74
|
|
|
if (count($field) === 1 || in_array(strtolower($field[1]), ['asc', 'desc'])) { |
75
|
|
|
$this->fields[] = $field; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Gets the fields associated with this statement. |
84
|
|
|
* |
85
|
|
|
* @return array fields i.e. [['field1','ASC'], ['field2']] |
86
|
|
|
*/ |
87
|
|
|
public function getFields() |
88
|
|
|
{ |
89
|
|
|
return $this->fields; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function build() |
93
|
|
|
{ |
94
|
|
|
$fields = $this->fields; |
95
|
|
|
foreach ($fields as &$field) { |
96
|
|
|
$field[0] = $this->escapeIdentifier($field[0]); |
97
|
|
|
$field = implode(' ', $field); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
// remove empty values |
101
|
|
|
$fields = array_filter($fields); |
102
|
|
|
|
103
|
|
|
if (count($fields) == 0) { |
104
|
|
|
return ''; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return ((!$this->groupBy) ? 'ORDER BY ' : 'GROUP BY '). |
108
|
|
|
implode(',', $fields); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|