|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Charcoal\Source; |
|
4
|
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
|
6
|
|
|
|
|
7
|
|
|
// From 'charcoal-core' |
|
8
|
|
|
use Charcoal\Source\Order; |
|
9
|
|
|
use Charcoal\Source\OrderInterface; |
|
10
|
|
|
use Charcoal\Source\OrderCollectionInterface; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Provides a order expression list. |
|
14
|
|
|
* |
|
15
|
|
|
* Satisfies {@see OrderCollectionInterface}. |
|
16
|
|
|
*/ |
|
17
|
|
|
trait OrderCollectionTrait |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* The list of query sorting objects. |
|
21
|
|
|
* |
|
22
|
|
|
* For example: one key of the array might look like "RAND()" |
|
23
|
|
|
* or `{ "property": "col", "direction": "ASC" }`. |
|
24
|
|
|
* |
|
25
|
|
|
* @var OrderInterface[] |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $orders = []; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Replace the query order(s) on this object. |
|
31
|
|
|
* |
|
32
|
|
|
* Note: Any existing orders are dropped. |
|
33
|
|
|
* |
|
34
|
|
|
* @param mixed[] $orders One or more orders to set on this expression. |
|
35
|
|
|
* @return self |
|
36
|
|
|
*/ |
|
37
|
|
|
public function setOrders(array $orders) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->orders = []; |
|
40
|
|
|
$this->addOrders($orders); |
|
41
|
|
|
return $this; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Append one or more query orders on this object. |
|
46
|
|
|
* |
|
47
|
|
|
* @uses self::processOrder() |
|
48
|
|
|
* @param mixed[] $orders One or more orders to add on this expression. |
|
49
|
|
|
* @return self |
|
50
|
|
|
*/ |
|
51
|
|
|
public function addOrders(array $orders) |
|
52
|
|
|
{ |
|
53
|
|
|
foreach ($orders as $key => $order) { |
|
54
|
|
|
$this->addOrder($order); |
|
55
|
|
|
|
|
56
|
|
|
/** Name the expression if $key is a non-numeric string. */ |
|
57
|
|
|
if (is_string($key) && !is_numeric($key)) { |
|
58
|
|
|
$order = end($this->orders); |
|
59
|
|
|
$order->setName($key); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return $this; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Append a query order on this object. |
|
68
|
|
|
* |
|
69
|
|
|
* @uses self::processOrder() |
|
70
|
|
|
* @param mixed $order The expression string, structure, object, or callable to be parsed. |
|
71
|
|
|
* @return self |
|
72
|
|
|
*/ |
|
73
|
|
|
public function addOrder($order) |
|
74
|
|
|
{ |
|
75
|
|
|
$this->orders[] = $this->processOrder($order); |
|
76
|
|
|
return $this; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Process a query order to build a tree of expressions. |
|
81
|
|
|
* |
|
82
|
|
|
* Implement in subclasses to dynamically parse orders before being appended. |
|
83
|
|
|
* |
|
84
|
|
|
* @param mixed $order The expression string, structure, object, or callable to be parsed. |
|
85
|
|
|
* @throws InvalidArgumentException If a order is not a string, array, object, or callable. |
|
86
|
|
|
* @return OrderInterface |
|
87
|
|
|
*/ |
|
88
|
|
|
protected function processOrder($order) |
|
89
|
|
|
{ |
|
90
|
|
|
if (!is_string($order) && is_callable($order)) { |
|
91
|
|
|
$expr = $this->createOrder(); |
|
92
|
|
|
/** |
|
93
|
|
|
* @param OrderInterface $expr The new order expression object. |
|
94
|
|
|
* @param OrderCollectionInterface $this The context of the collection. |
|
95
|
|
|
* @return string|array|OrderInterface The prepared order expression |
|
96
|
|
|
* string, structure, object. |
|
97
|
|
|
*/ |
|
98
|
|
|
$order = $order($expr, $this); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
if (is_string($order)) { |
|
102
|
|
|
$expr = $this->createOrder()->setCondition($order); |
|
|
|
|
|
|
103
|
|
|
$order = $expr; |
|
104
|
|
|
} elseif (is_array($order)) { |
|
105
|
|
|
$expr = $this->createOrder()->setData($order); |
|
|
|
|
|
|
106
|
|
|
$order = $expr; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** Append the order to the expression's stack. */ |
|
110
|
|
|
if ($order instanceof OrderInterface) { |
|
111
|
|
|
return $order; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
throw new InvalidArgumentException(sprintf( |
|
115
|
|
|
'Order must be a string, structure, or Expression object; received %s', |
|
116
|
|
|
is_object($order) ? get_class($order) : gettype($order) |
|
117
|
|
|
)); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Determine if the object has any query orders. |
|
122
|
|
|
* |
|
123
|
|
|
* @return boolean |
|
124
|
|
|
*/ |
|
125
|
|
|
public function hasOrders() |
|
126
|
|
|
{ |
|
127
|
|
|
return !empty($this->orders); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Retrieve the query orders stored in this object. |
|
132
|
|
|
* |
|
133
|
|
|
* @return array |
|
134
|
|
|
*/ |
|
135
|
|
|
public function orders() |
|
136
|
|
|
{ |
|
137
|
|
|
return $this->orders; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Traverses the tree of query orders and applies a user function to every expression. |
|
142
|
|
|
* |
|
143
|
|
|
* @param callable $callable The function to run for each expression. |
|
144
|
|
|
* @return self |
|
145
|
|
|
*/ |
|
146
|
|
|
public function traverseOrders(callable $callable) |
|
147
|
|
|
{ |
|
148
|
|
|
foreach ($this->orders() as $expr) { |
|
149
|
|
|
/** |
|
150
|
|
|
* @param OrderInterface $expr The iterated order expression object. |
|
151
|
|
|
* @param OrderCollectionInterface $this The context of the traversal. |
|
152
|
|
|
* @return void |
|
153
|
|
|
*/ |
|
154
|
|
|
$callable($expr, $this); |
|
155
|
|
|
if ($expr instanceof OrderCollectionInterface) { |
|
156
|
|
|
$expr->traverseOrders($callable); |
|
|
|
|
|
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
return $this; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Create a new query sorting expression. |
|
165
|
|
|
* |
|
166
|
|
|
* @param array $data Optional expression data. |
|
167
|
|
|
* @return OrderInterface A new order expression object. |
|
168
|
|
|
*/ |
|
169
|
|
|
abstract protected function createOrder(array $data = null); |
|
170
|
|
|
} |
|
171
|
|
|
|