OptionConfigurator.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. * @return $this
  26. *
  27. * @throws AccessException If called from a lazy option or normalizer
  28. */
  29. public function allowedTypes(string ...$types): self
  30. {
  31. $this->resolver->setAllowedTypes($this->name, $types);
  32. return $this;
  33. }
  34. /**
  35. * Sets allowed values for this option.
  36. *
  37. * @param mixed ...$values One or more acceptable values/closures
  38. *
  39. * @return $this
  40. *
  41. * @throws AccessException If called from a lazy option or normalizer
  42. */
  43. public function allowedValues(...$values): self
  44. {
  45. $this->resolver->setAllowedValues($this->name, $values);
  46. return $this;
  47. }
  48. /**
  49. * Sets the default value for this option.
  50. *
  51. * @param mixed $value The default value of the option
  52. *
  53. * @return $this
  54. *
  55. * @throws AccessException If called from a lazy option or normalizer
  56. */
  57. public function default($value): self
  58. {
  59. $this->resolver->setDefault($this->name, $value);
  60. return $this;
  61. }
  62. /**
  63. * Defines an option configurator with the given name.
  64. */
  65. public function define(string $option): self
  66. {
  67. return $this->resolver->define($option);
  68. }
  69. /**
  70. * Marks this option as deprecated.
  71. *
  72. * @param string $package The name of the composer package that is triggering the deprecation
  73. * @param string $version The version of the package that introduced the deprecation
  74. * @param string|\Closure $message The deprecation message to use
  75. *
  76. * @return $this
  77. */
  78. public function deprecated(string $package, string $version, $message = 'The option "%name%" is deprecated.'): self
  79. {
  80. $this->resolver->setDeprecated($this->name, $package, $version, $message);
  81. return $this;
  82. }
  83. /**
  84. * Sets the normalizer for this option.
  85. *
  86. * @return $this
  87. *
  88. * @throws AccessException If called from a lazy option or normalizer
  89. */
  90. public function normalize(\Closure $normalizer): self
  91. {
  92. $this->resolver->setNormalizer($this->name, $normalizer);
  93. return $this;
  94. }
  95. /**
  96. * Marks this option as required.
  97. *
  98. * @return $this
  99. *
  100. * @throws AccessException If called from a lazy option or normalizer
  101. */
  102. public function required(): self
  103. {
  104. $this->resolver->setRequired($this->name);
  105. return $this;
  106. }
  107. /**
  108. * Sets an info message for an option.
  109. *
  110. * @return $this
  111. *
  112. * @throws AccessException If called from a lazy option or normalizer
  113. */
  114. public function info(string $info): self
  115. {
  116. $this->resolver->setInfo($this->name, $info);
  117. return $this;
  118. }
  119. }