OptionConfigurator.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\OptionsResolver;
  11. use Symfony\Component\OptionsResolver\Exception\AccessException;
  12. final class OptionConfigurator
  13. {
  14. private $name;
  15. private $resolver;
  16. public function __construct(string $name, OptionsResolver $resolver)
  17. {
  18. $this->name = $name;
  19. $this->resolver = $resolver;
  20. $this->resolver->setDefined($name);
  21. }
  22. /**
  23. * Adds allowed types for this option.
  24. *
  25. * @param string ...$types One or more accepted types
  26. *
  27. * @return $this
  28. *
  29. * @throws AccessException If called from a lazy option or normalizer
  30. */
  31. public function allowedTypes(string ...$types): self
  32. {
  33. $this->resolver->setAllowedTypes($this->name, $types);
  34. return $this;
  35. }
  36. /**
  37. * Sets allowed values for this option.
  38. *
  39. * @param mixed ...$values One or more acceptable values/closures
  40. *
  41. * @return $this
  42. *
  43. * @throws AccessException If called from a lazy option or normalizer
  44. */
  45. public function allowedValues(...$values): self
  46. {
  47. $this->resolver->setAllowedValues($this->name, $values);
  48. return $this;
  49. }
  50. /**
  51. * Sets the default value for this option.
  52. *
  53. * @param mixed $value The default value of the option
  54. *
  55. * @return $this
  56. *
  57. * @throws AccessException If called from a lazy option or normalizer
  58. */
  59. public function default($value): self
  60. {
  61. $this->resolver->setDefault($this->name, $value);
  62. return $this;
  63. }
  64. /**
  65. * Defines an option configurator with the given name.
  66. */
  67. public function define(string $option): self
  68. {
  69. return $this->resolver->define($option);
  70. }
  71. /**
  72. * Marks this option as deprecated.
  73. *
  74. * @param string $package The name of the composer package that is triggering the deprecation
  75. * @param string $version The version of the package that introduced the deprecation
  76. * @param string|\Closure $message The deprecation message to use
  77. *
  78. * @return $this
  79. */
  80. public function deprecated(string $package, string $version, $message = 'The option "%name%" is deprecated.'): self
  81. {
  82. $this->resolver->setDeprecated($this->name, $package, $version, $message);
  83. return $this;
  84. }
  85. /**
  86. * Sets the normalizer for this option.
  87. *
  88. * @param \Closure $normalizer The normalizer
  89. *
  90. * @return $this
  91. *
  92. * @throws AccessException If called from a lazy option or normalizer
  93. */
  94. public function normalize(\Closure $normalizer): self
  95. {
  96. $this->resolver->setNormalizer($this->name, $normalizer);
  97. return $this;
  98. }
  99. /**
  100. * Marks this option as required.
  101. *
  102. * @return $this
  103. *
  104. * @throws AccessException If called from a lazy option or normalizer
  105. */
  106. public function required(): self
  107. {
  108. $this->resolver->setRequired($this->name);
  109. return $this;
  110. }
  111. /**
  112. * Sets an info message for an option.
  113. *
  114. * @return $this
  115. *
  116. * @throws AccessException If called from a lazy option or normalizer
  117. */
  118. public function info(string $info): self
  119. {
  120. $this->resolver->setInfo($this->name, $info);
  121. return $this;
  122. }
  123. }