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 #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
thrownewAccessException('The Info message cannot be set from a lazy option or normalizer.');
735
+
}
736
+
737
+
if (!isset($this->defined[$option])) {
738
+
thrownewUndefinedOptionsException(sprintf('The option "%s" does not exist. Defined options are: "%s".', $this->formatOptions([$option]), implode('", "', array_keys($this->defined))));
739
+
}
740
+
741
+
$this->info[$option] = $info;
742
+
743
+
return$this;
744
+
}
745
+
746
+
/**
747
+
* Gets the info message for an option.
748
+
*/
749
+
publicfunctiongetInfo(string$option): ?string
750
+
{
751
+
if (!isset($this->defined[$option])) {
752
+
thrownewUndefinedOptionsException(sprintf('The option "%s" does not exist. Defined options are: "%s".', $this->formatOptions([$option]), implode('", "', array_keys($this->defined))));
753
+
}
754
+
755
+
return$this->info[$option] ?? null;
756
+
}
757
+
718
758
/**
719
759
* Removes the option with the given name.
720
760
*
@@ -734,7 +774,7 @@ public function remove($optionNames)
0 commit comments