Skip to content

Commit 3ac975c

Browse files
committed
Document the array accessor
1 parent ed6f90d commit 3ac975c

File tree

4 files changed

+65
-5
lines changed

4 files changed

+65
-5
lines changed

Readme.md

+59-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ return static function (Slim\App $app) {
120120
}
121121
```
122122

123-
124123
## Error handling
125124

126125
The default error handling middleware is added. It’s configured to silently log
@@ -179,3 +178,62 @@ return new WonderNetwork\SlimKernel\ServiceFactory\SymfonyConsoleServiceFactory(
179178
'acme v1.0',
180179
);
181180
```
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[]`)

src/Accessor/ArrayAccessor.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function tryAtAny(string ...$keys): ArrayAccessor {
6363
/**
6464
* @return string[]
6565
*/
66-
public function allStrings(): array {
66+
public function allString(): array {
6767
return map(
6868
$this->payload,
6969
function ($value, $key) {

src/Http/RequestParams.php

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
/**
1313
* @property ArrayAccessor $params
14+
* @property ArrayAccessor $post
1415
* @property ArrayAccessor $query
1516
* @property ArrayAccessor $server
1617
*/
@@ -38,6 +39,7 @@ public function exceptionFactory(string $message): HttpBadRequestException {
3839
public function __get(string $name): ArrayAccessor {
3940
switch ($name) {
4041
case 'params':
42+
case 'post':
4143
return $this->params;
4244
case 'query':
4345
return $this->query;

tests/Accessor/ArrayAccessorTest.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -173,21 +173,21 @@ public function testBoolBailsOnInvalidString(): void {
173173
public function testAtNoKey(): void {
174174
self::assertEquals(
175175
[],
176-
ArrayAccessor::of([])->at('weekdays')->allStrings(),
176+
ArrayAccessor::of([])->at('weekdays')->allString(),
177177
);
178178
}
179179

180180
public function testAtEmptyArray(): void {
181181
self::assertEquals(
182182
[],
183-
ArrayAccessor::of(['weekdays' => []])->at('weekdays')->allStrings(),
183+
ArrayAccessor::of(['weekdays' => []])->at('weekdays')->allString(),
184184
);
185185
}
186186

187187
public function testAtFilledArray(): void {
188188
self::assertEquals(
189189
['monday'],
190-
ArrayAccessor::of(['weekdays' => ['monday']])->at('weekdays')->allStrings(),
190+
ArrayAccessor::of(['weekdays' => ['monday']])->at('weekdays')->allString(),
191191
);
192192
}
193193

0 commit comments

Comments
 (0)