Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #33848 [OptionsResolver] Add new OptionConfigurator class to …
…define options with fluent interface (lmillucci, yceruto) This PR was merged into the 5.1-dev branch. Discussion ---------- [OptionsResolver] Add new OptionConfigurator class to define options with fluent interface | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | Fix #33735 | License | MIT | Doc PR | symfony/symfony-docs#12426 - [x] submit changes to the documentation This PR adds OptionConfigurator to the OptionsResolver Commits ------- 1ff5640 [OptionsResolver] Add new OptionConfigurator class to define options with fluent interface
- Loading branch information
Showing
4 changed files
with
177 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
src/Symfony/Component/OptionsResolver/OptionConfigurator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\OptionsResolver; | ||
|
||
use Symfony\Component\OptionsResolver\Exception\AccessException; | ||
|
||
final class OptionConfigurator | ||
{ | ||
private $name; | ||
private $resolver; | ||
|
||
public function __construct(string $name, OptionsResolver $resolver) | ||
{ | ||
$this->name = $name; | ||
$this->resolver = $resolver; | ||
$this->resolver->setDefined($name); | ||
} | ||
|
||
/** | ||
* Adds allowed types for this option. | ||
* | ||
* @param string ...$types One or more accepted types | ||
* | ||
* @return $this | ||
* | ||
* @throws AccessException If called from a lazy option or normalizer | ||
*/ | ||
public function allowedTypes(string ...$types): self | ||
{ | ||
$this->resolver->setAllowedTypes($this->name, $types); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Sets allowed values for this option. | ||
* | ||
* @param mixed ...$values One or more acceptable values/closures | ||
* | ||
* @return $this | ||
* | ||
* @throws AccessException If called from a lazy option or normalizer | ||
*/ | ||
public function allowedValues(...$values): self | ||
{ | ||
$this->resolver->setAllowedValues($this->name, $values); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Sets the default value for this option. | ||
* | ||
* @param mixed $value The default value of the option | ||
* | ||
* @return $this | ||
* | ||
* @throws AccessException If called from a lazy option or normalizer | ||
*/ | ||
public function default($value): self | ||
{ | ||
$this->resolver->setDefault($this->name, $value); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Defines an option configurator with the given name. | ||
*/ | ||
public function define(string $option): self | ||
{ | ||
return $this->resolver->define($option); | ||
} | ||
|
||
/** | ||
* Marks this option as deprecated. | ||
* | ||
* @return $this | ||
* | ||
* @param string|\Closure $deprecationMessage | ||
*/ | ||
public function deprecated($deprecationMessage = 'The option "%name%" is deprecated.'): self | ||
{ | ||
$this->resolver->setDeprecated($this->name, $deprecationMessage); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Sets the normalizer for this option. | ||
* | ||
* @param \Closure $normalizer The normalizer | ||
* | ||
* @return $this | ||
* | ||
* @throws AccessException If called from a lazy option or normalizer | ||
*/ | ||
public function normalize(\Closure $normalizer): self | ||
{ | ||
$this->resolver->setNormalizer($this->name, $normalizer); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Marks this option as required. | ||
* | ||
* @return $this | ||
* | ||
* @throws AccessException If called from a lazy option or normalizer | ||
*/ | ||
public function required(): self | ||
{ | ||
$this->resolver->setRequired($this->name); | ||
|
||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters