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
This library provides a simple solution for processing and validating option arrays in PHP.
3
+
**Strict, Fluent, and Type-Safe Option Validation for PHP.**
4
+
5
+
Stop guessing what's in your `$options` array. This library provides a robust, fluent API to define, validate, and resolve options with strict type enforcement and custom validation logic. Designed for developers who value clarity and code quality.
4
6
5
7
## Installation
6
8
7
9
To install this library, use [Composer](https://getcomposer.org/)
Define the required options for your class using `OptionsResolver` with the expected options:
25
+
Define the options for your class using `OptionsResolver` with the expected options. You can use static factory methods on the `Option` class to define types easily.
22
26
23
27
```php
24
28
<?php
@@ -28,194 +32,224 @@ use PhpDevCommunity\Resolver\OptionsResolver;
28
32
29
33
class Database
30
34
{
35
+
private array $options;
36
+
31
37
public function __construct(array $options = [])
32
38
{
33
39
$resolver = new OptionsResolver([
34
-
new Option('host'),
35
-
new Option('username'),
36
-
new Option('password'),
37
-
new Option('dbname'),
40
+
Option::string('host')->setOptional('localhost'),
41
+
Option::string('username')->required(),
42
+
Option::string('password')->required(),
43
+
Option::string('dbname')->required(),
44
+
Option::int('port')->setOptional(3306),
38
45
]);
39
46
40
-
try {
41
-
$this->options = $resolver->resolve($options);
42
-
} catch (InvalidArgumentException $e) {
43
-
throw new InvalidArgumentException("Error: " . $e->getMessage());
44
-
}
47
+
$this->options = $resolver->resolve($options);
45
48
}
46
49
}
47
50
48
51
// Example usage:
49
52
try {
50
53
$database = new Database([
51
-
'host' => 'localhost',
52
-
'dbname' => 'app',
54
+
'username' => 'root',
55
+
'password' => 'secret',
56
+
'dbname' => 'app_db',
53
57
]);
58
+
// 'host' will be 'localhost' and 'port' will be 3306
54
59
} catch (InvalidArgumentException $e) {
55
-
echo "Error: " . $e->getMessage(); // Displays: "Error: The required option 'username' is missing."
60
+
echo "Error: " . $e->getMessage();
56
61
}
57
62
```
58
63
59
-
### Defining Default Options
64
+
### Available Types
65
+
60
66
61
-
You can also set default values for your options using `setDefaultValue` for each option:
62
67
68
+
The `Option` class provides several static factory methods to enforce types automatically. Here are examples for each type:
69
+
70
+
#### String
63
71
```php
64
-
<?php
72
+
Option::string('host')->setOptional('localhost');
73
+
```
65
74
66
-
class Database
67
-
{
68
-
public function __construct(array $options = [])
69
-
{
70
-
$resolver = new OptionsResolver([
71
-
(new Option('host'))->setDefaultValue('localhost'),
72
-
(new Option('username'))->setDefaultValue('root'),
73
-
(new Option('password'))->setDefaultValue('root'),
The `resolve()` method throws an `InvalidArgumentException` if:
130
+
* A required option is missing.
131
+
* An undefined option is provided.
132
+
* An option value is invalid (wrong type or failed custom validation).
133
+
134
+
### License
135
+
136
+
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
137
+
138
+
---
139
+
140
+
## Documentation (Français)
141
+
142
+
### Usage de base
143
+
144
+
Définissez les options attendues pour votre classe en utilisant `OptionsResolver`. Vous pouvez utiliser les méthodes statiques de la classe `Option` pour définir les types facilement.
132
145
133
146
```php
134
147
<?php
135
148
149
+
use PhpDevCommunity\Resolver\Option;
150
+
use PhpDevCommunity\Resolver\OptionsResolver;
151
+
136
152
class Database
137
153
{
154
+
private array $options;
155
+
138
156
public function __construct(array $options = [])
139
157
{
140
158
$resolver = new OptionsResolver([
141
-
(new Option('host'))->validator(static function($value) {
142
-
return is_string($value);
143
-
})->setDefaultValue('localhost'),
144
-
145
-
(new Option('username'))->validator(static function($value) {
146
-
return is_string($value);
147
-
})->setDefaultValue('root'),
148
-
149
-
(new Option('password'))->validator(static function($value) {
150
-
return is_string($value);
151
-
})->setDefaultValue('root'),
152
-
153
-
(new Option('dbname'))->validator(static function($value) {
154
-
return is_string($value);
155
-
})->setDefaultValue('app'),
156
-
157
-
(new Option('driver'))->validator(static function($value) {
throw new InvalidArgumentException("Error: " . $e->getMessage());
166
-
}
166
+
$this->options = $resolver->resolve($options);
167
167
}
168
168
}
169
169
170
-
// Example usage with an invalid driver value:
170
+
// Exemple d'utilisation :
171
171
try {
172
172
$database = new Database([
173
-
'host' => '192.168.1.200',
174
173
'username' => 'root',
175
-
'password' => 'root',
176
-
'dbname' => 'my-app',
177
-
'driver' => 'pdo_sqlite',
174
+
'password' => 'secret',
175
+
'dbname' => 'app_db',
178
176
]);
177
+
// 'host' vaudra 'localhost' et 'port' vaudra 3306
179
178
} catch (InvalidArgumentException $e) {
180
-
echo "Error: " . $e->getMessage(); // Displays: "Error: The option 'driver' with value 'pdo_sqlite' is invalid."
179
+
echo "Erreur : " . $e->getMessage();
181
180
}
182
181
```
183
182
184
-
Certainly! Let's focus specifically on the use of `Option::new()` to instantiate options in a fluent manner:
183
+
### Types Disponibles
185
184
186
-
---
187
185
188
-
## Instantiating Options with `Option::new()`
189
186
190
-
You can use `Option::new()` to create and configure option instances in a fluent style before adding them to the `OptionsResolver`. Here's an example that demonstrates this approach:
187
+
La classe `Option` fournit plusieurs méthodes statiques pour forcer les types automatiquement. Voici des exemples pour chaque type :
La méthode `resolve()` lance une `InvalidArgumentException` si :
249
+
* Une option requise est manquante.
250
+
* Une option non définie est fournie.
251
+
* Une valeur d'option est invalide (mauvais type ou échec de validation personnalisée).
215
252
216
-
- We use `Option::new('option1')` to create an `Option` instance named `'option1'`.
217
-
- Similarly, we use `Option::new('option2')->setDefaultValue('default')` to create an `Option` instance named `'option2'` with a default value of `'default'`.
218
-
- Both options are then added to the `OptionsResolver` when it's instantiated.
219
-
- Finally, we resolve the options by passing an array of values, and only `'option1'` is provided with a value (`'value1'`).
253
+
### Licence
220
254
221
-
Using `Option::new()` provides a concise and clear way to create and configure option instances before resolving them with specific values.
255
+
Ce projet est sous licence MIT. Voir le fichier [LICENSE](LICENSE) pour plus de détails.
0 commit comments