diff --git a/src/Symfony/Component/OptionsResolver/OptionConfigurator.php b/src/Symfony/Component/OptionsResolver/OptionConfigurator.php new file mode 100644 index 0000000000000..98e8c0eff71f9 --- /dev/null +++ b/src/Symfony/Component/OptionsResolver/OptionConfigurator.php @@ -0,0 +1,107 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\OptionsResolver; + +/** + * Creates Options for the OptionResolver + * + * @author Simon D. Mueller + */ +final class OptionConfigurator +{ + /** + * @var string + */ + private $name; + /** + * @var OptionsResolver + */ + private $resolver; + + /** + * OptionConfigurator constructor. + */ + public function __construct(string $name, OptionsResolver $resolver) + { + $this->name = $name; + $this->resolver = $resolver; + $this->resolver->setDefined($name); + } + + /** + * @param $value + * + * @return OptionConfigurator + */ + public function default($value): self + { + $this->resolver->setDefault($this->name, $value); + + return $this; + } + + /** + * @return OptionConfigurator + */ + public function required(): self + { + $this->resolver->setRequired($this->name); + + return $this; + } + + /** + * @param $message + * + * @return OptionConfigurator + */ + public function deprecated($message): self + { + $this->resolver->setDeprecated($this->name, $message); + + return $this; + } + + /** + * @param $type + * + * @return OptionConfigurator + */ + public function allowedTypes($type): self + { + $this->resolver->setAllowedTypes($this->name, $type); + + return $this; + } + + /** + * @param $values + * + * @return OptionConfigurator + */ + public function allowedValues($values): self + { + $this->resolver->setAllowedValues($this->name, $values); + + return $this; + } + + /** + * @return OptionConfigurator + */ + public function normalize(callable $normalizer): self + { + $this->resolver->setNormalizer($this->name, $normalizer); + + return $this; + } +} diff --git a/src/Symfony/Component/OptionsResolver/OptionsResolver.php b/src/Symfony/Component/OptionsResolver/OptionsResolver.php index 837f7536f01a2..47d3b8b0b1519 100644 --- a/src/Symfony/Component/OptionsResolver/OptionsResolver.php +++ b/src/Symfony/Component/OptionsResolver/OptionsResolver.php @@ -111,6 +111,11 @@ class OptionsResolver implements Options 'double' => 'float', ]; + public function define(string $optionName): OptionConfigurator + { + return new OptionConfigurator($optionName, $this); + } + /** * Sets the default value of a given option. * @@ -829,8 +834,7 @@ public function resolve(array $options = []) /** * Returns the resolved value of an option. * - * @param string $option The option name - * @param bool $triggerDeprecation Whether to trigger the deprecation or not (true by default) + * @param string $option The option name * * @return mixed The option value *