@@ -120,7 +120,6 @@ return static function (Slim\App $app) {
120
120
}
121
121
```
122
122
123
-
124
123
## Error handling
125
124
126
125
The default error handling middleware is added. It’s configured to silently log
@@ -179,3 +178,62 @@ return new WonderNetwork\SlimKernel\ServiceFactory\SymfonyConsoleServiceFactory(
179
178
'acme v1.0',
180
179
);
181
180
```
181
+
182
+
183
+ ## Convenience methods to access strongly typed input argumets
184
+
185
+ ``` php
186
+ // HTTP
187
+ $requestParams = WonderNetwork\SlimKernel\Http\RequestParams::of($serverRequest);
188
+ $requestParams->post->requireString('userId');
189
+ $requestParams->query->int('limit', 10);
190
+ $requestParams->server->bool('HTTPS');
191
+ // simpler interface, since route arguments are all string:
192
+ WonderNetwork\SlimKernel\Http\RouteArgument::get($serverRequest, 'userId');
193
+ WonderNetwork\SlimKernel\Http\RouteArgument::find($serverRequest, 'userId');
194
+ WonderNetwork\SlimKernel\Http\RouteArgument::maybe($serverRequest, 'userId');
195
+ // CLI
196
+ $cliParams = WonderNetwork\SlimKernel\Cli\InputParams::ofInput($input);
197
+ $cliParams->options
198
+ $cliParams->arguments
199
+ ```
200
+
201
+ This package is aimed at ** accessing trusted payloads** . In other words, it does
202
+ not aim at validating an unknown payload, but rather asserts that the structure
203
+ is correct, so that we can use semantic methods to get strong type guarantees.
204
+ The package does not try to handle any situation -- except for basic type casting,
205
+ it throws if it encounters some unexpected input.
206
+
207
+ * _ Request Params_ : created from ` RequestInterface ` and represent request body,
208
+ query and server params. Each field is returned as an [ Array Accessor] ( #array-accessor )
209
+
210
+ * _ Input Params_ : created from ` InputInterface ` and represent command line
211
+ arguments and options. Each field is returned as an [ Array Accessor] ( #array-accessor )
212
+
213
+ * _ Route Argument_ : created from ` RequestInterface ` and represent the arguments
214
+ matched by slim routing.
215
+
216
+ ### Array Accessor
217
+
218
+ * ` string(key, default = '') `
219
+ * gets the interpolated string value of given field
220
+ * returns default on missing and null values
221
+ * throws on non-scalar values
222
+ * ` maybeString(key) ` see above, but returns null as the default
223
+ * ` requireString(key) ` see above, but throws as the default
224
+ * ` int(key, default = 0) ` similar to string, casts numeric strings to ints
225
+ * ` maybeInt() ` and ` requireInt() ` similarly to string methods
226
+ * ` bool(key) ` interpolates ` 1 ` and ` 0 ` to boolean
227
+ * ` maybeBool() ` and ` requireBool() ` see above
228
+ * ` array(key) `
229
+ * returns a mixed array on given key
230
+ * returns an empty array if key does not exist
231
+ * throws if key exists but does not explicitly contain an array
232
+ * ` maybeArray(key) ` see above, but returns null by default
233
+ * ` at(key) `
234
+ * returns an array accessor representing a nested structure
235
+ * uses null object pattern, so returns an empty accessor if the
236
+ key does not exist or is not an array
237
+ * ` allString() ` , ` allInt() ` , ` allBool() `
238
+ * ensures all items of payload match a given type
239
+ * returns an array of scalars of that type (` string[] ` , ` int[] ` , ` bool[] ` )
0 commit comments