/src/Symfony/Component/OptionsResolver/OptionsResolver.php

https://github.com/r1pp3rj4ck/symfony · PHP · 352 lines · 181 code · 57 blank · 114 comment · 13 complexity · ebfa12a9b861f40c7491658a4d4db48b MD5 · raw file

  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\OptionDefinitionException;
  12. use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
  13. use Symfony\Component\OptionsResolver\Exception\MissingOptionsException;
  14. /**
  15. * Helper for merging default and concrete option values.
  16. *
  17. * @author Bernhard Schussek <bschussek@gmail.com>
  18. * @author Tobias Schultze <http://tobion.de>
  19. */
  20. class OptionsResolver implements OptionsResolverInterface
  21. {
  22. /**
  23. * The default option values.
  24. * @var Options
  25. */
  26. private $defaultOptions;
  27. /**
  28. * The options known by the resolver.
  29. * @var array
  30. */
  31. private $knownOptions = array();
  32. /**
  33. * The options without defaults that are required to be passed to resolve().
  34. * @var array
  35. */
  36. private $requiredOptions = array();
  37. /**
  38. * A list of accepted values for each option.
  39. * @var array
  40. */
  41. private $allowedValues = array();
  42. /**
  43. * A list of accepted types for each option.
  44. * @var array
  45. */
  46. private $allowedTypes = array();
  47. /**
  48. * Creates a new instance.
  49. */
  50. public function __construct()
  51. {
  52. $this->defaultOptions = new Options();
  53. }
  54. /**
  55. * Clones the resolver.
  56. */
  57. public function __clone()
  58. {
  59. $this->defaultOptions = clone $this->defaultOptions;
  60. }
  61. /**
  62. * {@inheritdoc}
  63. */
  64. public function setDefaults(array $defaultValues)
  65. {
  66. foreach ($defaultValues as $option => $value) {
  67. $this->defaultOptions->overload($option, $value);
  68. $this->knownOptions[$option] = true;
  69. unset($this->requiredOptions[$option]);
  70. }
  71. return $this;
  72. }
  73. /**
  74. * {@inheritdoc}
  75. */
  76. public function replaceDefaults(array $defaultValues)
  77. {
  78. foreach ($defaultValues as $option => $value) {
  79. $this->defaultOptions->set($option, $value);
  80. $this->knownOptions[$option] = true;
  81. unset($this->requiredOptions[$option]);
  82. }
  83. return $this;
  84. }
  85. /**
  86. * {@inheritdoc}
  87. */
  88. public function setOptional(array $optionNames)
  89. {
  90. foreach ($optionNames as $key => $option) {
  91. if (!is_int($key)) {
  92. throw new OptionDefinitionException('You should not pass default values to setOptional()');
  93. }
  94. $this->knownOptions[$option] = true;
  95. }
  96. return $this;
  97. }
  98. /**
  99. * {@inheritdoc}
  100. */
  101. public function setRequired(array $optionNames)
  102. {
  103. foreach ($optionNames as $key => $option) {
  104. if (!is_int($key)) {
  105. throw new OptionDefinitionException('You should not pass default values to setRequired()');
  106. }
  107. $this->knownOptions[$option] = true;
  108. // set as required if no default has been set already
  109. if (!isset($this->defaultOptions[$option])) {
  110. $this->requiredOptions[$option] = true;
  111. }
  112. }
  113. return $this;
  114. }
  115. /**
  116. * {@inheritdoc}
  117. */
  118. public function setAllowedValues(array $allowedValues)
  119. {
  120. $this->validateOptionsExistence($allowedValues);
  121. $this->allowedValues = array_replace($this->allowedValues, $allowedValues);
  122. return $this;
  123. }
  124. /**
  125. * {@inheritdoc}
  126. */
  127. public function addAllowedValues(array $allowedValues)
  128. {
  129. $this->validateOptionsExistence($allowedValues);
  130. $this->allowedValues = array_merge_recursive($this->allowedValues, $allowedValues);
  131. return $this;
  132. }
  133. /**
  134. * {@inheritdoc}
  135. */
  136. public function setAllowedTypes(array $allowedTypes)
  137. {
  138. $this->validateOptionsExistence($allowedTypes);
  139. $this->allowedTypes = array_replace($this->allowedTypes, $allowedTypes);
  140. return $this;
  141. }
  142. /**
  143. * {@inheritdoc}
  144. */
  145. public function addAllowedTypes(array $allowedTypes)
  146. {
  147. $this->validateOptionsExistence($allowedTypes);
  148. $this->allowedTypes = array_merge_recursive($this->allowedTypes, $allowedTypes);
  149. return $this;
  150. }
  151. /**
  152. * {@inheritdoc}
  153. */
  154. public function setNormalizers(array $normalizers)
  155. {
  156. $this->validateOptionsExistence($normalizers);
  157. foreach ($normalizers as $option => $normalizer) {
  158. $this->defaultOptions->setNormalizer($option, $normalizer);
  159. }
  160. return $this;
  161. }
  162. /**
  163. * {@inheritdoc}
  164. */
  165. public function isKnown($option)
  166. {
  167. return isset($this->knownOptions[$option]);
  168. }
  169. /**
  170. * {@inheritdoc}
  171. */
  172. public function isRequired($option)
  173. {
  174. return isset($this->requiredOptions[$option]);
  175. }
  176. /**
  177. * {@inheritdoc}
  178. */
  179. public function resolve(array $options = array())
  180. {
  181. $this->validateOptionsExistence($options);
  182. $this->validateOptionsCompleteness($options);
  183. // Make sure this method can be called multiple times
  184. $combinedOptions = clone $this->defaultOptions;
  185. // Override options set by the user
  186. foreach ($options as $option => $value) {
  187. $combinedOptions->set($option, $value);
  188. }
  189. // Resolve options
  190. $resolvedOptions = $combinedOptions->all();
  191. $this->validateOptionValues($resolvedOptions);
  192. $this->validateOptionTypes($resolvedOptions);
  193. return $resolvedOptions;
  194. }
  195. /**
  196. * Validates that the given option names exist and throws an exception
  197. * otherwise.
  198. *
  199. * @param array $options An list of option names as keys.
  200. *
  201. * @throws InvalidOptionsException If any of the options has not been defined.
  202. */
  203. private function validateOptionsExistence(array $options)
  204. {
  205. $diff = array_diff_key($options, $this->knownOptions);
  206. if (count($diff) > 0) {
  207. ksort($this->knownOptions);
  208. ksort($diff);
  209. throw new InvalidOptionsException(sprintf(
  210. (count($diff) > 1 ? 'The options "%s" do not exist.' : 'The option "%s" does not exist.').' Known options are: "%s"',
  211. implode('", "', array_keys($diff)),
  212. implode('", "', array_keys($this->knownOptions))
  213. ));
  214. }
  215. }
  216. /**
  217. * Validates that all required options are given and throws an exception
  218. * otherwise.
  219. *
  220. * @param array $options An list of option names as keys.
  221. *
  222. * @throws MissingOptionsException If a required option is missing.
  223. */
  224. private function validateOptionsCompleteness(array $options)
  225. {
  226. $diff = array_diff_key($this->requiredOptions, $options);
  227. if (count($diff) > 0) {
  228. ksort($diff);
  229. throw new MissingOptionsException(sprintf(
  230. count($diff) > 1 ? 'The required options "%s" are missing.' : 'The required option "%s" is missing.',
  231. implode('", "', array_keys($diff))
  232. ));
  233. }
  234. }
  235. /**
  236. * Validates that the given option values match the allowed values and
  237. * throws an exception otherwise.
  238. *
  239. * @param array $options A list of option values.
  240. *
  241. * @throws InvalidOptionsException If any of the values does not match the
  242. * allowed values of the option.
  243. */
  244. private function validateOptionValues(array $options)
  245. {
  246. foreach ($this->allowedValues as $option => $allowedValues) {
  247. if (isset($options[$option])) {
  248. if (is_array($allowedValues) && !in_array($options[$option], $allowedValues, true)) {
  249. throw new InvalidOptionsException(sprintf('The option "%s" has the value "%s", but is expected to be one of "%s"', $option, $options[$option], implode('", "', $allowedValues)));
  250. }
  251. if (is_callable($allowedValues) && !call_user_func($allowedValues, $options[$option])) {
  252. throw new InvalidOptionsException(sprintf('The option "%s" has the value "%s", which it is not valid', $option, $options[$option]));
  253. }
  254. }
  255. }
  256. }
  257. /**
  258. * Validates that the given options match the allowed types and
  259. * throws an exception otherwise.
  260. *
  261. * @param array $options A list of options.
  262. *
  263. * @throws InvalidOptionsException If any of the types does not match the
  264. * allowed types of the option.
  265. */
  266. private function validateOptionTypes(array $options)
  267. {
  268. foreach ($this->allowedTypes as $option => $allowedTypes) {
  269. if (!array_key_exists($option, $options)) {
  270. continue;
  271. }
  272. $value = $options[$option];
  273. $allowedTypes = (array) $allowedTypes;
  274. foreach ($allowedTypes as $type) {
  275. $isFunction = 'is_'.$type;
  276. if (function_exists($isFunction) && $isFunction($value)) {
  277. continue 2;
  278. } elseif ($value instanceof $type) {
  279. continue 2;
  280. }
  281. }
  282. $printableValue = is_object($value)
  283. ? get_class($value)
  284. : (is_array($value)
  285. ? 'Array'
  286. : (string) $value);
  287. throw new InvalidOptionsException(sprintf(
  288. 'The option "%s" with value "%s" is expected to be of type "%s"',
  289. $option,
  290. $printableValue,
  291. implode('", "', $allowedTypes)
  292. ));
  293. }
  294. }
  295. }