You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+108Lines changed: 108 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -141,6 +141,114 @@ When invoking a Parameter `$param($arg)` or `$param->__invoke($arg)` it will tri
141
141
* Will fill-in any missing optional parameters with their default values.
142
142
* Will exclude any extra unexpected parameters.
143
143
144
+
## Arguments
145
+
146
+
The Arguments object is the counterpart of Parameters. It holds the actual arguments validated against a Parameters instance, enabling type-safe interaction by parameter name or position. It supports nested data structures and optional parameters.
147
+
148
+
```php
149
+
use function Chevere\Parameter\parameters;
150
+
use function Chevere\Parameter\arguments;
151
+
use function Chevere\Parameter\int;
152
+
use function Chevere\Parameter\string;
153
+
154
+
$parameters = parameters(
155
+
id: int(min: 1),
156
+
name: string('/^[A-Z]{1}\w+$/'),
157
+
)->withOptional(
158
+
email: string(),
159
+
);
160
+
$data = [
161
+
'id' => 1,
162
+
'name' => 'Pepe'
163
+
];
164
+
$arguments = arguments($parameters, $data);
165
+
```
166
+
167
+
## Put arguments
168
+
169
+
Use method `withPut()` to create a new Arguments instance with an added or replaced argument by name or position.
Use method `toArray()` to retrieve the arguments as an array.
178
+
179
+
```php
180
+
$array = $arguments->toArray();
181
+
```
182
+
183
+
Use method `toArrayFill()` to retrieve the arguments as an array including optional parameters with their default values.
184
+
185
+
```php
186
+
$array = $arguments->toArrayFill();
187
+
```
188
+
189
+
## Check if argument exists
190
+
191
+
Use method `has()` to check if an argument exists by name or position.
192
+
193
+
```php
194
+
$arguments->has('id'); // true
195
+
$arguments->has('poto'); // false
196
+
```
197
+
198
+
## Get argument value (mixed)
199
+
200
+
Use method `get()` to retrieve argument value by name or position. Return type as `mixed`.
201
+
202
+
```php
203
+
$id = $arguments->get('id'); // 1
204
+
```
205
+
206
+
## Get argument value (typed)
207
+
208
+
Use method `required()` to retrieve a explicit required argument value by name or position. It returns as `CastInterface`, enabling typed access to the value. The method `optional()` can be used to retrieve an optional argument value.
Use method `nested()` to retrieve nested Arguments instances, enabling type-safe access to nested data structures.
218
+
219
+
```php
220
+
use function Chevere\Parameter\parameters;
221
+
use function Chevere\Parameter\arrayp;
222
+
use function Chevere\Parameter\string;
223
+
224
+
$parameters = parameters(
225
+
meta: arrayp(
226
+
custom_data: arrayp(
227
+
product: string(),
228
+
product_id_external: string(),
229
+
)
230
+
),
231
+
);
232
+
$data = [
233
+
'meta' => [
234
+
'custom_data' => [
235
+
'product' => 'Book',
236
+
'product_id_external' => 'book_987654321',
237
+
],
238
+
],
239
+
];
240
+
$arguments = arguments($parameters, $data);
241
+
$product = $arguments
242
+
->nested('meta', 'custom_data')
243
+
->required('product')->string(); // 'Book'
244
+
```
245
+
246
+
When working with objects implementing `ParametersAccessInterface` (for example, `ArrayParameterInterface`) you can use method `parameters()` to retrieve the Parameters instance used to validate the Arguments instance.
247
+
248
+
```php
249
+
$parameters = $arguments->parameters();
250
+
```
251
+
144
252
## Attribute usage
145
253
146
254
Attribute usage refers to the use of attributes to define parameters and return rules. You can use attribute notation for class properties, methods/functions parameters and return value.
0 commit comments