OptionsResolverInterface.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. /**
  12. * @author Bernhard Schussek <bschussek@gmail.com>
  13. */
  14. interface OptionsResolverInterface
  15. {
  16. /**
  17. * Sets default option values.
  18. *
  19. * The options can either be values of any types or closures that
  20. * evaluate the option value lazily. These closures must have one
  21. * of the following signatures:
  22. *
  23. * <code>
  24. * function (Options $options)
  25. * function (Options $options, $value)
  26. * </code>
  27. *
  28. * The second parameter passed to the closure is the previously
  29. * set default value, in case you are overwriting an existing
  30. * default value.
  31. *
  32. * The closures should return the lazily created option value.
  33. *
  34. * @param array $defaultValues A list of option names as keys and default
  35. * values or closures as values.
  36. *
  37. * @return OptionsResolverInterface The resolver instance.
  38. */
  39. public function setDefaults(array $defaultValues);
  40. /**
  41. * Replaces default option values.
  42. *
  43. * Old defaults are erased, which means that closures passed here cannot
  44. * access the previous default value. This may be useful to improve
  45. * performance if the previous default value is calculated by an expensive
  46. * closure.
  47. *
  48. * @param array $defaultValues A list of option names as keys and default
  49. * values or closures as values.
  50. *
  51. * @return OptionsResolverInterface The resolver instance.
  52. */
  53. public function replaceDefaults(array $defaultValues);
  54. /**
  55. * Sets optional options.
  56. *
  57. * This method declares valid option names without setting default values for them.
  58. * If these options are not passed to {@link resolve()} and no default has been set
  59. * for them, they will be missing in the final options array. This can be helpful
  60. * if you want to determine whether an option has been set or not because otherwise
  61. * {@link resolve()} would trigger an exception for unknown options.
  62. *
  63. * @param array $optionNames A list of option names.
  64. *
  65. * @return OptionsResolverInterface The resolver instance.
  66. *
  67. * @throws Exception\OptionDefinitionException When trying to pass default values.
  68. */
  69. public function setOptional(array $optionNames);
  70. /**
  71. * Sets required options.
  72. *
  73. * If these options are not passed to {@link resolve()} and no default has been set for
  74. * them, an exception will be thrown.
  75. *
  76. * @param array $optionNames A list of option names.
  77. *
  78. * @return OptionsResolverInterface The resolver instance.
  79. *
  80. * @throws Exception\OptionDefinitionException When trying to pass default values.
  81. */
  82. public function setRequired(array $optionNames);
  83. /**
  84. * Sets allowed values for a list of options.
  85. *
  86. * @param array $allowedValues A list of option names as keys and arrays
  87. * with values acceptable for that option as
  88. * values.
  89. *
  90. * @return OptionsResolverInterface The resolver instance.
  91. *
  92. * @throws Exception\InvalidOptionsException If an option has not been defined
  93. * (see {@link isKnown()}) for which
  94. * an allowed value is set.
  95. */
  96. public function setAllowedValues(array $allowedValues);
  97. /**
  98. * Adds allowed values for a list of options.
  99. *
  100. * The values are merged with the allowed values defined previously.
  101. *
  102. * @param array $allowedValues A list of option names as keys and arrays
  103. * with values acceptable for that option as
  104. * values.
  105. *
  106. * @return OptionsResolverInterface The resolver instance.
  107. *
  108. * @throws Exception\InvalidOptionsException If an option has not been defined
  109. * (see {@link isKnown()}) for which
  110. * an allowed value is set.
  111. */
  112. public function addAllowedValues(array $allowedValues);
  113. /**
  114. * Sets allowed types for a list of options.
  115. *
  116. * @param array $allowedTypes A list of option names as keys and type
  117. * names passed as string or array as values.
  118. *
  119. * @return OptionsResolverInterface The resolver instance.
  120. *
  121. * @throws Exception\InvalidOptionsException If an option has not been defined for
  122. * which an allowed type is set.
  123. */
  124. public function setAllowedTypes(array $allowedTypes);
  125. /**
  126. * Adds allowed types for a list of options.
  127. *
  128. * The types are merged with the allowed types defined previously.
  129. *
  130. * @param array $allowedTypes A list of option names as keys and type
  131. * names passed as string or array as values.
  132. *
  133. * @return OptionsResolverInterface The resolver instance.
  134. *
  135. * @throws Exception\InvalidOptionsException If an option has not been defined for
  136. * which an allowed type is set.
  137. */
  138. public function addAllowedTypes(array $allowedTypes);
  139. /**
  140. * Sets normalizers that are applied on resolved options.
  141. *
  142. * The normalizers should be closures with the following signature:
  143. *
  144. * <code>
  145. * function (Options $options, $value)
  146. * </code>
  147. *
  148. * The second parameter passed to the closure is the value of
  149. * the option.
  150. *
  151. * The closure should return the normalized value.
  152. *
  153. * @param array $normalizers An array of closures.
  154. *
  155. * @return OptionsResolverInterface The resolver instance.
  156. */
  157. public function setNormalizers(array $normalizers);
  158. /**
  159. * Returns whether an option is known.
  160. *
  161. * An option is known if it has been passed to either {@link setDefaults()},
  162. * {@link setRequired()} or {@link setOptional()} before.
  163. *
  164. * @param string $option The name of the option.
  165. *
  166. * @return Boolean Whether the option is known.
  167. */
  168. public function isKnown($option);
  169. /**
  170. * Returns whether an option is required.
  171. *
  172. * An option is required if it has been passed to {@link setRequired()},
  173. * but not to {@link setDefaults()}. That is, the option has been declared
  174. * as required and no default value has been set.
  175. *
  176. * @param string $option The name of the option.
  177. *
  178. * @return Boolean Whether the option is required.
  179. */
  180. public function isRequired($option);
  181. /**
  182. * Returns the combination of the default and the passed options.
  183. *
  184. * @param array $options The custom option values.
  185. *
  186. * @return array A list of options and their values.
  187. *
  188. * @throws Exception\InvalidOptionsException If any of the passed options has not
  189. * been defined or does not contain an
  190. * allowed value.
  191. * @throws Exception\MissingOptionsException If a required option is missing.
  192. * @throws Exception\OptionDefinitionException If a cyclic dependency is detected
  193. * between two lazy options.
  194. */
  195. public function resolve(array $options = array());
  196. }