1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Trucker\Finders\Conditions; |
4
|
|
|
|
5
|
|
|
use Guzzle\Http\Message\Request; |
6
|
|
|
use Illuminate\Container\Container; |
7
|
|
|
use Trucker\Facades\Config; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class to manage how sorting requirements for results returned by |
11
|
|
|
* a request, where the directives are passed as HTTP GET parameters with |
12
|
|
|
* particular parameter names (defined in the config). |
13
|
|
|
* The resulting GET params might be something like:. |
14
|
|
|
* |
15
|
|
|
* <code> |
16
|
|
|
* order_by=someProperty |
17
|
|
|
* order_dir=ASC |
18
|
|
|
* </code> |
19
|
|
|
*/ |
20
|
|
|
class GetParamsResultOrder implements QueryResultOrderInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* The IoC Container. |
24
|
|
|
* |
25
|
|
|
* @var Container |
26
|
|
|
*/ |
27
|
|
|
protected $app; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Var to hold the order by field |
31
|
|
|
* that the results shoudl be sorted on. |
32
|
|
|
* |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
protected $orderByField; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Var to hold the order direction |
39
|
|
|
* for results to be returned. |
40
|
|
|
* |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected $orderDirection; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Constructor, likely never called in implementation |
47
|
|
|
* but rather through the service provider. |
48
|
|
|
* |
49
|
|
|
* @param Container $app |
50
|
|
|
*/ |
51
|
2 |
|
public function __construct(Container $app) |
52
|
|
|
{ |
53
|
2 |
|
$this->app = $app; |
54
|
2 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Function to create a new instance that should |
58
|
|
|
* be setup with the IoC Container etc. |
59
|
|
|
* |
60
|
|
|
* @return GetParamsResultOrder |
61
|
|
|
*/ |
62
|
|
|
public function newInstance() |
63
|
|
|
{ |
64
|
|
|
return new static($this->app); |
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Function to set the property which the results |
69
|
|
|
* should be ordered by. |
70
|
|
|
* |
71
|
|
|
* @param string $propertyName |
72
|
|
|
*/ |
73
|
1 |
|
public function setOrderByField($propertyName) |
74
|
|
|
{ |
75
|
1 |
|
$this->orderByField = $propertyName; |
76
|
1 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Function to set the direction which the results |
80
|
|
|
* should be sorted by, ascending, descending. |
81
|
|
|
* |
82
|
|
|
* @param string $direction |
83
|
|
|
* |
84
|
|
|
* @throws \Exception |
85
|
|
|
*/ |
86
|
1 |
|
public function setOrderDirection($direction) |
87
|
|
|
{ |
88
|
1 |
|
if ($direction != $this->getOrderDirectionAscending() && |
89
|
1 |
|
$direction != $this->getOrderDirectionDescending() |
90
|
|
|
) { |
91
|
|
|
throw new \Exception("Invalid order direction: {$direction}"); |
92
|
|
|
} |
93
|
|
|
|
94
|
1 |
|
$this->orderDirection = $direction; |
95
|
1 |
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Getter function to return the string that represents |
99
|
|
|
* the ascending sort direction. |
100
|
|
|
* |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
1 |
|
public function getOrderDirectionAscending() |
104
|
|
|
{ |
105
|
1 |
|
return Config::get('result_order.get_params.order_dir_ascending'); |
|
|
|
|
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Getter function to return the string that represents |
110
|
|
|
* the descending sort direction. |
111
|
|
|
* |
112
|
|
|
* @return string |
113
|
|
|
*/ |
114
|
1 |
|
public function getOrderDirectionDescending() |
115
|
|
|
{ |
116
|
1 |
|
return Config::get('result_order.get_params.order_dir_descending'); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Function to add all the directives that have been |
121
|
|
|
* given to the class to a given request object. |
122
|
|
|
* |
123
|
|
|
* @param Request $request Request passed by reference |
124
|
|
|
*/ |
125
|
1 |
|
public function addToRequest(Request $request) |
126
|
|
|
{ |
127
|
1 |
|
$query = $request->getQuery(); |
128
|
|
|
|
129
|
1 |
|
if (isset($this->orderByField)) { |
130
|
1 |
|
$query->add( |
131
|
1 |
|
Config::get('result_order.get_params.order_by'), |
132
|
1 |
|
$this->orderByField |
133
|
|
|
); |
134
|
|
|
} |
135
|
|
|
|
136
|
1 |
|
if (isset($this->orderDirection)) { |
137
|
1 |
|
$query->add( |
138
|
1 |
|
Config::get('result_order.get_params.order_dir'), |
139
|
1 |
|
$this->orderDirection |
140
|
|
|
); |
141
|
|
|
} |
142
|
1 |
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Function to convert the directives represented in this class |
146
|
|
|
* to an array, this is useful for testing. |
147
|
|
|
* |
148
|
|
|
* @return array |
149
|
|
|
*/ |
150
|
1 |
|
public function toArray() |
151
|
|
|
{ |
152
|
1 |
|
$order_by = Config::get('result_order.get_params.order_by'); |
153
|
1 |
|
$order_dir = Config::get('result_order.get_params.order_dir'); |
154
|
|
|
|
155
|
1 |
|
$params = []; |
156
|
1 |
|
$params[$order_by] = $this->orderByField; |
157
|
1 |
|
$params[$order_dir] = $this->orderDirection; |
158
|
|
|
|
159
|
1 |
|
return $params; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Function to convert the directives represented in this class |
164
|
|
|
* to a querystring, this is useful for testing. |
165
|
|
|
* |
166
|
|
|
* @return string |
167
|
|
|
*/ |
168
|
1 |
|
public function toQueryString() |
169
|
|
|
{ |
170
|
1 |
|
return http_build_query($this->toArray()); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
In the issue above, the returned value is violating the contract defined by the mentioned interface.
Let's take a look at an example: