OptionsResolverInterface.php 7.0 KB

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