You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

OptionsResolverIntrospector.php 2.9 KiB

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