Skip to content

Commit be618a8

Browse files
committed
document arguments
1 parent e801a85 commit be618a8

2 files changed

Lines changed: 130 additions & 0 deletions

File tree

README.md

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,114 @@ When invoking a Parameter `$param($arg)` or `$param->__invoke($arg)` it will tri
141141
* Will fill-in any missing optional parameters with their default values.
142142
* Will exclude any extra unexpected parameters.
143143

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.
170+
171+
```php
172+
$arguments = $arguments->withPut('email', 'mail@chevere.org');
173+
```
174+
175+
## Arguments array
176+
177+
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.
209+
210+
```php
211+
$id = $arguments->required('id')->int(); // 1
212+
$email = $arguments->optional('email')?->string(); // null
213+
```
214+
215+
## Nested arguments
216+
217+
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+
144252
## Attribute usage
145253

146254
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.

tests/ParametersTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
use PHPUnit\Framework\Attributes\DataProvider;
3434
use PHPUnit\Framework\TestCase;
3535
use ReflectionMethod;
36+
use function Chevere\Parameter\arguments;
3637
use function Chevere\Parameter\int;
3738
use function Chevere\Parameter\reflectionToParameters;
3839
use function Chevere\Parameter\string;
@@ -432,4 +433,25 @@ public function testWithIsVariadic(): void
432433
$this->assertNotEquals($with, $parameters);
433434
$this->assertTrue($with->isVariadic());
434435
}
436+
437+
public function testReadme(): void
438+
{
439+
$parameters = new Parameters(
440+
id: int(min: 1),
441+
name: string('/^[A-Z]{1}\w+$/'),
442+
)->withOptional(
443+
'email',
444+
string(),
445+
);
446+
$data = [
447+
'id' => 1,
448+
'name' => 'Pepe',
449+
];
450+
$arguments = arguments($parameters, $data);
451+
$this->assertTrue($arguments->has('id'));
452+
$this->assertFalse($arguments->has('poto'));
453+
$this->assertSame(1, $arguments->get('id'));
454+
$this->assertSame(1, $arguments->required('id')->int());
455+
$this->assertNull($arguments->optional('email')?->string());
456+
}
435457
}

0 commit comments

Comments
 (0)