OptionsResolverIntrospector.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 \Closure $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. * @throws NoConfigurationException on no configured value
  37. */
  38. public function getDefault(string $option): mixed
  39. {
  40. return ($this->get)('defaults', $option, sprintf('No default value was set for the "%s" option.', $option));
  41. }
  42. /**
  43. * @return \Closure[]
  44. *
  45. * @throws NoConfigurationException on no configured closures
  46. */
  47. public function getLazyClosures(string $option): array
  48. {
  49. return ($this->get)('lazy', $option, sprintf('No lazy closures were set for the "%s" option.', $option));
  50. }
  51. /**
  52. * @return string[]
  53. *
  54. * @throws NoConfigurationException on no configured types
  55. */
  56. public function getAllowedTypes(string $option): array
  57. {
  58. return ($this->get)('allowedTypes', $option, sprintf('No allowed types were set for the "%s" option.', $option));
  59. }
  60. /**
  61. * @return mixed[]
  62. *
  63. * @throws NoConfigurationException on no configured values
  64. */
  65. public function getAllowedValues(string $option): array
  66. {
  67. return ($this->get)('allowedValues', $option, sprintf('No allowed values were set for the "%s" option.', $option));
  68. }
  69. /**
  70. * @throws NoConfigurationException on no configured normalizer
  71. */
  72. public function getNormalizer(string $option): \Closure
  73. {
  74. return current($this->getNormalizers($option));
  75. }
  76. /**
  77. * @throws NoConfigurationException when no normalizer is configured
  78. */
  79. public function getNormalizers(string $option): array
  80. {
  81. return ($this->get)('normalizers', $option, sprintf('No normalizer was set for the "%s" option.', $option));
  82. }
  83. /**
  84. * @throws NoConfigurationException on no configured deprecation
  85. */
  86. public function getDeprecation(string $option): array
  87. {
  88. return ($this->get)('deprecated', $option, sprintf('No deprecation was set for the "%s" option.', $option));
  89. }
  90. }