1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace As3\Modlr\Rest; |
4
|
|
|
|
5
|
|
|
use As3\Modlr\Util\Validator; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* REST Configuration. |
9
|
|
|
* |
10
|
|
|
* @author Jacob Bare <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
class RestConfiguration |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
private $rootEndpoint = '/'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Validator service for handling common validation tasks. |
21
|
|
|
* |
22
|
|
|
* @var Validator |
23
|
|
|
*/ |
24
|
|
|
private $validator; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The configured API scheme, such as http or https. |
28
|
|
|
* |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
private $scheme; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* The configured API hostname. |
35
|
|
|
* |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
private $host; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Whether all relationship fields should be included by default. |
42
|
|
|
* |
43
|
|
|
* @var bool |
44
|
|
|
*/ |
45
|
|
|
private $includeAll = true; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Determines how entity names should be formatted. |
49
|
|
|
* |
50
|
|
|
* @var string |
51
|
|
|
*/ |
52
|
|
|
private $entityFormat = 'dash'; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Determines how field key names should be formatted. |
56
|
|
|
* |
57
|
|
|
* @var string |
58
|
|
|
*/ |
59
|
|
|
private $fieldKeyFormat = 'camelcase'; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Constructor. |
63
|
|
|
* |
64
|
|
|
* @param Validator|null $validator |
65
|
|
|
*/ |
66
|
|
|
public function __construct(Validator $validator = null) |
67
|
|
|
{ |
68
|
|
|
$this->validator = $validator ?: new Validator(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Gets the validator service. |
73
|
|
|
* |
74
|
|
|
* @return Validator |
75
|
|
|
*/ |
76
|
|
|
public function getValidator() |
77
|
|
|
{ |
78
|
|
|
return $this->validator; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Gets the root API endpoint shared by all requests. |
83
|
|
|
* |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
|
|
public function getRootEndpoint() |
87
|
|
|
{ |
88
|
|
|
return $this->rootEndpoint; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Sets the root API endpoint shared by all requests. |
93
|
|
|
* |
94
|
|
|
* @param string $endpoint |
95
|
|
|
* @return self |
96
|
|
|
*/ |
97
|
|
|
public function setRootEndpoint($endpoint) |
98
|
|
|
{ |
99
|
|
|
$this->rootEndpoint = sprintf('/%s', trim($endpoint, '/')); |
100
|
|
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Sets the entity type format. |
105
|
|
|
* |
106
|
|
|
* @param string $format |
107
|
|
|
* @return self |
108
|
|
|
*/ |
109
|
|
|
public function setEntityFormat($format) |
110
|
|
|
{ |
111
|
|
|
$this->validator->isFormatValid($format); |
112
|
|
|
$this->entityFormat = $format; |
113
|
|
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Gets the entity type format. |
118
|
|
|
* |
119
|
|
|
* @return string |
120
|
|
|
*/ |
121
|
|
|
public function getEntityFormat() |
122
|
|
|
{ |
123
|
|
|
return $this->entityFormat; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Sets the field key format. |
128
|
|
|
* |
129
|
|
|
* @param string $format |
130
|
|
|
* @return self |
131
|
|
|
*/ |
132
|
|
|
public function setFieldKeyFormat($format) |
133
|
|
|
{ |
134
|
|
|
$this->validator->isFormatValid($format); |
135
|
|
|
$this->fieldKeyFormat = $format; |
136
|
|
|
return $this; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Gets the field key format. |
141
|
|
|
* |
142
|
|
|
* @return string |
143
|
|
|
*/ |
144
|
|
|
public function getFieldKeyFormat() |
145
|
|
|
{ |
146
|
|
|
return $this->fieldKeyFormat; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Sets the scheme for all API requests. |
151
|
|
|
* |
152
|
|
|
* @param string $scheme |
153
|
|
|
* @return self |
154
|
|
|
*/ |
155
|
|
|
public function setScheme($scheme) |
156
|
|
|
{ |
157
|
|
|
$this->scheme = $scheme; |
158
|
|
|
return $this; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Gets the scheme for all API requests. |
163
|
|
|
* |
164
|
|
|
* @return string |
165
|
|
|
*/ |
166
|
|
|
public function getScheme() |
167
|
|
|
{ |
168
|
|
|
return $this->scheme; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Sets the hostname for all API requests. |
173
|
|
|
* |
174
|
|
|
* @param string $host |
175
|
|
|
* @return self |
176
|
|
|
*/ |
177
|
|
|
public function setHost($host) |
178
|
|
|
{ |
179
|
|
|
$this->host = $host; |
180
|
|
|
return $this; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Gets the hostname for all API requests. |
185
|
|
|
* |
186
|
|
|
* @return string |
187
|
|
|
*/ |
188
|
|
|
public function getHost() |
189
|
|
|
{ |
190
|
|
|
return $this->host; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Whether all relationships should be included (side-loaded) by default. |
195
|
|
|
* |
196
|
|
|
* @param bool |
197
|
|
|
*/ |
198
|
|
|
public function includeAllByDefault() |
199
|
|
|
{ |
200
|
|
|
return $this->includeAll; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Gets the default pagination criteria. |
205
|
|
|
* |
206
|
|
|
* @return array |
207
|
|
|
*/ |
208
|
|
|
public function getDefaultPagination() |
209
|
|
|
{ |
210
|
|
|
return [ |
211
|
|
|
'offset' => 0, |
212
|
|
|
'limit' => 25, |
213
|
|
|
]; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Gets the default sorting criteria. |
218
|
|
|
* |
219
|
|
|
* @return array |
220
|
|
|
*/ |
221
|
|
|
public function getDefaultSorting() |
222
|
|
|
{ |
223
|
|
|
return ['id' => -1]; |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
|