OptionConfigurator.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 string $name;
  15. private OptionsResolver $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): static
  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(mixed ...$values): static
  44. {
  45. $this->resolver->setAllowedValues($this->name, $values);
  46. return $this;
  47. }
  48. /**
  49. * Sets the default value for this option.
  50. *
  51. * @return $this
  52. *
  53. * @throws AccessException If called from a lazy option or normalizer
  54. */
  55. public function default(mixed $value): static
  56. {
  57. $this->resolver->setDefault($this->name, $value);
  58. return $this;
  59. }
  60. /**
  61. * Defines an option configurator with the given name.
  62. */
  63. public function define(string $option): self
  64. {
  65. return $this->resolver->define($option);
  66. }
  67. /**
  68. * Marks this option as deprecated.
  69. *
  70. * @param string $package The name of the composer package that is triggering the deprecation
  71. * @param string $version The version of the package that introduced the deprecation
  72. * @param string|\Closure $message The deprecation message to use
  73. *
  74. * @return $this
  75. */
  76. public function deprecated(string $package, string $version, string|\Closure $message = 'The option "%name%" is deprecated.'): static
  77. {
  78. $this->resolver->setDeprecated($this->name, $package, $version, $message);
  79. return $this;
  80. }
  81. /**
  82. * Sets the normalizer for this option.
  83. *
  84. * @return $this
  85. *
  86. * @throws AccessException If called from a lazy option or normalizer
  87. */
  88. public function normalize(\Closure $normalizer): static
  89. {
  90. $this->resolver->setNormalizer($this->name, $normalizer);
  91. return $this;
  92. }
  93. /**
  94. * Marks this option as required.
  95. *
  96. * @return $this
  97. *
  98. * @throws AccessException If called from a lazy option or normalizer
  99. */
  100. public function required(): static
  101. {
  102. $this->resolver->setRequired($this->name);
  103. return $this;
  104. }
  105. /**
  106. * Sets an info message for an option.
  107. *
  108. * @return $this
  109. *
  110. * @throws AccessException If called from a lazy option or normalizer
  111. */
  112. public function info(string $info): static
  113. {
  114. $this->resolver->setInfo($this->name, $info);
  115. return $this;
  116. }
  117. /**
  118. * Sets whether ignore undefined options.
  119. *
  120. * @return $this
  121. */
  122. public function ignoreUndefined(bool $ignore = true): static
  123. {
  124. $this->resolver->setIgnoreUndefined($ignore);
  125. return $this;
  126. }
  127. }