OptionsResolverIntrospector.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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\Debug;
  11. use Symfony\Component\OptionsResolver\Exception\NoConfigurationException;
  12. use Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException;
  13. use Symfony\Component\OptionsResolver\OptionsResolver;
  14. /**
  15. * @author Maxime Steinhausser <maxime.steinhausser@gmail.com>
  16. *
  17. * @final
  18. */
  19. class OptionsResolverIntrospector
  20. {
  21. private $get;
  22. public function __construct(OptionsResolver $optionsResolver)
  23. {
  24. $this->get = \Closure::bind(function ($property, $option, $message) {
  25. /** @var OptionsResolver $this */
  26. if (!$this->isDefined($option)) {
  27. throw new UndefinedOptionsException(sprintf('The option "%s" does not exist.', $option));
  28. }
  29. if (!\array_key_exists($option, $this->{$property})) {
  30. throw new NoConfigurationException($message);
  31. }
  32. return $this->{$property}[$option];
  33. }, $optionsResolver, $optionsResolver);
  34. }
  35. /**
  36. * @return mixed
  37. *
  38. * @throws NoConfigurationException on no configured value
  39. */
  40. public function getDefault(string $option)
  41. {
  42. return ($this->get)('defaults', $option, sprintf('No default value was set for the "%s" option.', $option));
  43. }
  44. /**
  45. * @return \Closure[]
  46. *
  47. * @throws NoConfigurationException on no configured closures
  48. */
  49. public function getLazyClosures(string $option): array
  50. {
  51. return ($this->get)('lazy', $option, sprintf('No lazy closures were set for the "%s" option.', $option));
  52. }
  53. /**
  54. * @return string[]
  55. *
  56. * @throws NoConfigurationException on no configured types
  57. */
  58. public function getAllowedTypes(string $option): array
  59. {
  60. return ($this->get)('allowedTypes', $option, sprintf('No allowed types were set for the "%s" option.', $option));
  61. }
  62. /**
  63. * @return mixed[]
  64. *
  65. * @throws NoConfigurationException on no configured values
  66. */
  67. public function getAllowedValues(string $option): array
  68. {
  69. return ($this->get)('allowedValues', $option, sprintf('No allowed values were set for the "%s" option.', $option));
  70. }
  71. /**
  72. * @throws NoConfigurationException on no configured normalizer
  73. */
  74. public function getNormalizer(string $option): \Closure
  75. {
  76. return current($this->getNormalizers($option));
  77. }
  78. /**
  79. * @throws NoConfigurationException when no normalizer is configured
  80. */
  81. public function getNormalizers(string $option): array
  82. {
  83. return ($this->get)('normalizers', $option, sprintf('No normalizer was set for the "%s" option.', $option));
  84. }
  85. /**
  86. * @return string|\Closure
  87. *
  88. * @throws NoConfigurationException on no configured deprecation
  89. *
  90. * @deprecated since Symfony 5.1, use "getDeprecation()" instead.
  91. */
  92. public function getDeprecationMessage(string $option)
  93. {
  94. trigger_deprecation('symfony/options-resolver', '5.1', 'The "%s()" method is deprecated, use "getDeprecation()" instead.', __METHOD__);
  95. return $this->getDeprecation($option)['message'];
  96. }
  97. /**
  98. * @throws NoConfigurationException on no configured deprecation
  99. */
  100. public function getDeprecation(string $option): array
  101. {
  102. return ($this->get)('deprecated', $option, sprintf('No deprecation was set for the "%s" option.', $option));
  103. }
  104. }