OptionsResolver.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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]) && !in_array($options[$option], $allowedValues, true)) {
  248. throw new InvalidOptionsException(sprintf('The option "%s" has the value "%s", but is expected to be one of "%s"', $option, $options[$option], implode('", "', $allowedValues)));
  249. }
  250. }
  251. }
  252. /**
  253. * Validates that the given options match the allowed types and
  254. * throws an exception otherwise.
  255. *
  256. * @param array $options A list of options.
  257. *
  258. * @throws InvalidOptionsException If any of the types does not match the
  259. * allowed types of the option.
  260. */
  261. private function validateOptionTypes(array $options)
  262. {
  263. foreach ($this->allowedTypes as $option => $allowedTypes) {
  264. if (!array_key_exists($option, $options)) {
  265. continue;
  266. }
  267. $value = $options[$option];
  268. $allowedTypes = (array) $allowedTypes;
  269. foreach ($allowedTypes as $type) {
  270. $isFunction = 'is_'.$type;
  271. if (function_exists($isFunction) && $isFunction($value)) {
  272. continue 2;
  273. } elseif ($value instanceof $type) {
  274. continue 2;
  275. }
  276. }
  277. $printableValue = is_object($value)
  278. ? get_class($value)
  279. : (is_array($value)
  280. ? 'Array'
  281. : (string) $value);
  282. throw new InvalidOptionsException(sprintf(
  283. 'The option "%s" with value "%s" is expected to be of type "%s"',
  284. $option,
  285. $printableValue,
  286. implode('", "', $allowedTypes)
  287. ));
  288. }
  289. }
  290. }