This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/OptionsResolver
Fabien Potencier 9eb7cb1b7b feature #35400 [RFC][DX][OptionsResolver] Allow setting info message per option (yceruto)
This PR was merged into the 5.1-dev branch.

Discussion
----------

[RFC][DX][OptionsResolver] Allow setting info message per option

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       | -
| License       | MIT
| Doc PR        | TODO

This is a DX proposal that will help in debugging/errors to better understand the meaning of one defined option.

This is how you'd use it:
```php
$resolver = new OptionsResolver();
$resolver->setDefined('date');
$resolver->setAllowedTypes('date', \DateTime::class);
$resolver->setInfo('date', 'A future date'); // <-- NEW
// ...
```
This information may be useful for those options where their name cannot be intuitive enough, or their purpose is too complex. Here is an example (based on the example above):
```php
// ...
$resolver->setAllowedValues('date', static function ($value): bool {
    return $value >= new \DateTime('now');
});
```
So, if you introduce a date value that does not match the criteria, you will get this error message:

**Before:**
```
The option "date" with value DateTime is invalid.
```
Note that the allowed value is not printable in this case, hence the error message cannot be clear at all.

**After:**
```
The option "date" with value DateTime is invalid. Info: A future date.
```
Although a more accurate error message can be triggered within the `\Closure` if desired.

Also you'll see this info message on `debug:form` command (see tests), then you have in advance the informative description of any option.

What do you think?

Commits
-------

0477a06d8a Allow setting one info message per option
2020-02-10 09:05:33 +01:00
..
Debug [OptionsResolver] Add a new method addNormalizer and normalization hierarchy 2019-03-31 13:47:13 +02:00
Exception Mark ExceptionInterfaces throwable 2018-08-29 17:58:00 +02:00
Tests Allow setting one info message per option 2020-02-09 19:42:47 -05:00
.gitattributes Add .gitignore to .gitattributes 2019-10-12 01:35:04 +01:00
.gitignore
CHANGELOG.md Allow setting one info message per option 2020-02-09 19:42:47 -05:00
composer.json Leverage trigger_deprecation() from symfony/deprecation-contracts 2020-02-08 15:04:50 +01:00
LICENSE Update year in license files 2020-01-01 12:03:25 +01:00
OptionConfigurator.php Allow setting one info message per option 2020-02-09 19:42:47 -05:00
Options.php [OptionsResolver] fix adding $triggerDeprecation to Options::offsetGet() 2019-06-09 18:15:03 +02:00
OptionsResolver.php feature #35400 [RFC][DX][OptionsResolver] Allow setting info message per option (yceruto) 2020-02-10 09:05:33 +01:00
phpunit.xml.dist Bump phpunit XSD version to 5.2 2018-11-11 12:18:13 +01:00
README.md Fixed readme of OptionsResolver 2016-05-06 22:13:08 +02:00

OptionsResolver Component

The OptionsResolver component is array_replace on steroids. It allows you to create an options system with required options, defaults, validation (type, value), normalization and more.

Resources