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
feature #11716 [OptionsResolver] Added a light-weight, low-level API for basic option resolving (webmozart)
This PR was merged into the 2.6-dev branch.
Discussion
----------
[OptionsResolver] Added a light-weight, low-level API for basic option resolving
| Q | A
| ------------- | ---
| Bug fix? | no
| New feature? | yes
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets | #11705
| License | MIT
| Doc PR | symfony/symfony-docs#4159
See [the updated documentation](https://github.com/webmozart/symfony-docs/blob/issue11705/components/options_resolver.rst) for details on the usage of the simple API.
The most important motivation for this change is DX and speed. The basic features of the component should be easily usable in a wide variety of use cases without impacting performance.
For DX reasons, I added the static methods to the `Options` class, which makes the code concise and easy to read and understand:
```php
use Symfony\Component\OptionsResolver\Options;
$options = Options::validateRequired($options, 'format');
$options = Options::validateTypes($options, array(
'format' => array('string', 'int'),
'calendar' => 'int',
));
$options = Options::validateValues($options, array(
'calendar' => array(
\IntlDateFormatter::GREGORIAN,
\IntlDateFormatter::TRADITIONAL,
),
));
$options = Options::resolve($options, array(
'format' => null,
'calendar' => \IntlDateFormatter::GREGORIAN,
));
```
If you need to distribute the option configuration, this PR also extracts the configuration part of the `OptionsResolver` class into a new class `OptionsConfig`, which can be passed around. When the configuration is complete, pass the config object to `Options::resolve()` as second argument:
```php
$config = new OptionsConfig();
$config->setDefaults(array(
'format' => \IntlDateFormatter::MEDIUM,
'calendar' => \IntlDateFormatter::GREGORIAN,
));
$options = Options::resolve($options, $config);
```
Consequently - since `OptionsResolver` extends `OptionsConfig` - the two following statements now become identical:
```php
$options = $resolver->resolve($options);
$options = Options::resolve($options, $resolver);
```
Commits
-------
9066025 [OptionsResolver] Added a light-weight, low-level API for basic option resolving
if (is_array($optionValues) && !in_array($options[$option], $optionValues, true)) {
278
+
thrownewInvalidOptionsException(sprintf('The option "%s" has the value "%s", but is expected to be one of "%s"', $option, $options[$option], implode('", "', $optionValues)));
279
+
}
280
+
281
+
if (is_callable($optionValues) && !call_user_func($optionValues, $options[$option])) {
282
+
thrownewInvalidOptionsException(sprintf('The option "%s" has the value "%s", which it is not valid', $option, $options[$option]));
283
+
}
284
+
}
285
+
}
286
+
}
287
+
288
+
/**
289
+
* Constructs a new object with a set of default options.
290
+
*
291
+
* @param array $options A list of option names and values
0 commit comments