Skip to content

Commit 534e3f3

Browse files
committed
converts requests to use the dto component.
1 parent 0cf61ba commit 534e3f3

File tree

8 files changed

+235
-517
lines changed

8 files changed

+235
-517
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"ext-json": "*",
1616
"neuron-php/application": "0.8.*",
1717
"neuron-php/routing": "0.8.*",
18+
"neuron-php/dto": "^0.0.7",
1819
"league/commonmark": "^2.6",
1920
"neuron-php/cli": "0.8.*",
2021
"robmorgan/phinx": "^0.16",

examples/config/requests/login.yaml

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,20 @@ request:
66
username:
77
required: true
88
type: string
9-
minLength: 3
10-
maxLength: 20
9+
length:
10+
min: 3
11+
max: 20
1112
password:
1213
required: true
1314
type: string
14-
minLength: 8
15-
maxLength: 50
15+
length:
16+
min: 8
17+
max: 50
1618
age:
1719
type: integer
18-
minimum: 18
19-
maximum: 40
20+
range:
21+
min: 18
22+
max: 40
2023
birthdate:
2124
type: string
2225
format: date
@@ -28,20 +31,24 @@ request:
2831
street:
2932
required: true
3033
type: string
31-
minLength: 3
32-
maxLength: 10
34+
length:
35+
min: 3
36+
max: 10
3337
city:
3438
required: true
3539
type: string
36-
minLength: 3
37-
maxLength: 20
40+
length:
41+
min: 3
42+
max: 20
3843
state:
3944
required: true
4045
type: string
41-
minLength: 2
42-
maxLength: 2
46+
length:
47+
min: 2
48+
max: 2
4349
zip:
4450
required: true
4551
type: string
46-
minLength: 3
47-
maxLength: 20
52+
length:
53+
min: 3
54+
max: 20

examples/config/requests/test.yaml

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ request:
2727
integer:
2828
required: true
2929
type: integer
30-
minValue: 1
31-
maxValue: 100
30+
range:
31+
min: 1
32+
max: 100
3233
ip_address:
3334
required: true
3435
type: ip_address
@@ -45,18 +46,21 @@ request:
4546
subparam1:
4647
required: true
4748
type: string
48-
minLength: 3
49-
maxLength: 20
49+
length:
50+
min: 3
51+
max: 20
5052
subparam2:
5153
required: true
5254
type: string
53-
minLength: 3
54-
maxLength: 20
55+
length:
56+
min: 3
57+
max: 20
5558
string:
5659
required: true
5760
type: string
58-
minLength: 3
59-
maxLength: 20
61+
length:
62+
min: 3
63+
max: 20
6064
time:
6165
required: true
6266
type: time

readme.md

Lines changed: 46 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -187,23 +187,27 @@ routes:
187187

188188
### Request Handling
189189

190-
Create request parameter definitions for validation:
190+
Create request DTO definitions for validation:
191191

192192
```yaml
193193
# config/requests/user_profile.yaml
194-
parameters:
195-
id:
196-
type: integer
197-
required: true
198-
min_value: 1
194+
request:
195+
method: GET
196+
properties:
197+
id:
198+
type: integer
199+
required: true
200+
range:
201+
min: 1
199202
```
200203

201-
Access parameters in controllers:
204+
Access validated data in controllers:
202205

203206
```php
204207
public function profile(Request $request): string
205208
{
206-
$userId = $request->getParam('id')->getValue();
209+
$dto = $request->getDto();
210+
$userId = $dto->id;
207211
// ...
208212
}
209213
```
@@ -448,7 +452,7 @@ class ProductController extends Base
448452
449453
public function details(Request $request): string
450454
{
451-
$id = $request->getParam('id')->getValue();
455+
$id = $request->getRouteParameter('id');
452456
$product = $this->getProduct($id);
453457
454458
if (!$product) {
@@ -470,40 +474,48 @@ class ProductController extends Base
470474
}
471475
```
472476

473-
### Request Parameter Validation
477+
### Request Validation with DTOs
474478

475-
Define parameters in YAML:
479+
Define request DTOs in YAML:
476480

477481
```yaml
478482
# config/requests/product_create.yaml
479-
parameters:
480-
name:
481-
type: string
482-
required: true
483-
min_length: 3
484-
max_length: 100
485-
486-
price:
487-
type: currency
488-
required: true
489-
min_value: 0
490-
491-
category_id:
492-
type: integer
493-
required: true
494-
495-
description:
496-
type: string
497-
required: false
498-
max_length: 1000
483+
request:
484+
method: POST
485+
headers:
486+
Content-Type: application/json
487+
properties:
488+
name:
489+
type: string
490+
required: true
491+
length:
492+
min: 3
493+
max: 100
494+
495+
price:
496+
type: currency
497+
required: true
498+
range:
499+
min: 0
500+
501+
category_id:
502+
type: integer
503+
required: true
504+
505+
description:
506+
type: string
507+
required: false
508+
length:
509+
max: 1000
499510
```
500511

501-
Available parameter types:
512+
Available property types:
502513
- `string`, `integer`, `float`, `boolean`
503514
- `email`, `url`, `uuid`
504-
- `date`, `datetime`
505-
- `currency`, `phone_number`
515+
- `date`, `date_time`, `time`
516+
- `currency`, `us_phone_number`, `intl_phone_number`
506517
- `array`, `object`
518+
- `ip_address`, `ein`, `upc`, `name`, `numeric`
507519

508520
### Error Handling
509521

0 commit comments

Comments
 (0)