OptionsResolverIntrospector.php
103 lines
| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * This file is part of the Symfony package. |
| 5 | * |
| 6 | * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | * |
| 8 | * For the full copyright and license information, please view the LICENSE |
| 9 | * file that was distributed with this source code. |
| 10 | */ |
| 11 | |
| 12 | namespace Symfony\Component\OptionsResolver\Debug; |
| 13 | |
| 14 | use Symfony\Component\OptionsResolver\Exception\NoConfigurationException; |
| 15 | use Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException; |
| 16 | use Symfony\Component\OptionsResolver\OptionsResolver; |
| 17 | |
| 18 | /** |
| 19 | * @author Maxime Steinhausser <maxime.steinhausser@gmail.com> |
| 20 | * |
| 21 | * @final |
| 22 | */ |
| 23 | class OptionsResolverIntrospector |
| 24 | { |
| 25 | private $get; |
| 26 | |
| 27 | public function __construct(OptionsResolver $optionsResolver) |
| 28 | { |
| 29 | $this->get = \Closure::bind(function ($property, $option, $message) { |
| 30 | /** @var OptionsResolver $this */ |
| 31 | if (!$this->isDefined($option)) { |
| 32 | throw new UndefinedOptionsException(sprintf('The option "%s" does not exist.', $option)); |
| 33 | } |
| 34 | |
| 35 | if (!\array_key_exists($option, $this->{$property})) { |
| 36 | throw new NoConfigurationException($message); |
| 37 | } |
| 38 | |
| 39 | return $this->{$property}[$option]; |
| 40 | }, $optionsResolver, $optionsResolver); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * @param string $option |
| 45 | * |
| 46 | * @return mixed |
| 47 | * |
| 48 | * @throws NoConfigurationException on no configured value |
| 49 | */ |
| 50 | public function getDefault($option) |
| 51 | { |
| 52 | return \call_user_func($this->get, 'defaults', $option, sprintf('No default value was set for the "%s" option.', $option)); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * @param string $option |
| 57 | * |
| 58 | * @return \Closure[] |
| 59 | * |
| 60 | * @throws NoConfigurationException on no configured closures |
| 61 | */ |
| 62 | public function getLazyClosures($option) |
| 63 | { |
| 64 | return \call_user_func($this->get, 'lazy', $option, sprintf('No lazy closures were set for the "%s" option.', $option)); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * @param string $option |
| 69 | * |
| 70 | * @return string[] |
| 71 | * |
| 72 | * @throws NoConfigurationException on no configured types |
| 73 | */ |
| 74 | public function getAllowedTypes($option) |
| 75 | { |
| 76 | return \call_user_func($this->get, 'allowedTypes', $option, sprintf('No allowed types were set for the "%s" option.', $option)); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * @param string $option |
| 81 | * |
| 82 | * @return mixed[] |
| 83 | * |
| 84 | * @throws NoConfigurationException on no configured values |
| 85 | */ |
| 86 | public function getAllowedValues($option) |
| 87 | { |
| 88 | return \call_user_func($this->get, 'allowedValues', $option, sprintf('No allowed values were set for the "%s" option.', $option)); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * @param string $option |
| 93 | * |
| 94 | * @return \Closure |
| 95 | * |
| 96 | * @throws NoConfigurationException on no configured normalizer |
| 97 | */ |
| 98 | public function getNormalizer($option) |
| 99 | { |
| 100 | return \call_user_func($this->get, 'normalizers', $option, sprintf('No normalizer was set for the "%s" option.', $option)); |
| 101 | } |
| 102 | } |
| 103 |