4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\OptionsResolver
;
15 * @author Bernhard Schussek <bschussek@gmail.com>
17 interface OptionsResolverInterface
20 * Sets default option values.
22 * The options can either be values of any types or closures that
23 * evaluate the option value lazily. These closures must have one
24 * of the following signatures:
27 * function (Options $options)
28 * function (Options $options, $value)
31 * The second parameter passed to the closure is the previously
32 * set default value, in case you are overwriting an existing
35 * The closures should return the lazily created option value.
37 * @param array $defaultValues A list of option names as keys and default
38 * values or closures as values.
40 * @return OptionsResolverInterface The resolver instance.
42 public function setDefaults(array $defaultValues);
45 * Replaces default option values.
47 * Old defaults are erased, which means that closures passed here cannot
48 * access the previous default value. This may be useful to improve
49 * performance if the previous default value is calculated by an expensive
52 * @param array $defaultValues A list of option names as keys and default
53 * values or closures as values.
55 * @return OptionsResolverInterface The resolver instance.
57 public function replaceDefaults(array $defaultValues);
60 * Sets optional options.
62 * This method declares valid option names without setting default values for them.
63 * If these options are not passed to {@link resolve()} and no default has been set
64 * for them, they will be missing in the final options array. This can be helpful
65 * if you want to determine whether an option has been set or not because otherwise
66 * {@link resolve()} would trigger an exception for unknown options.
68 * @param array $optionNames A list of option names.
70 * @return OptionsResolverInterface The resolver instance.
72 * @throws Exception\OptionDefinitionException When trying to pass default values.
74 public function setOptional(array $optionNames);
77 * Sets required options.
79 * If these options are not passed to {@link resolve()} and no default has been set for
80 * them, an exception will be thrown.
82 * @param array $optionNames A list of option names.
84 * @return OptionsResolverInterface The resolver instance.
86 * @throws Exception\OptionDefinitionException When trying to pass default values.
88 public function setRequired(array $optionNames);
91 * Sets allowed values for a list of options.
93 * @param array $allowedValues A list of option names as keys and arrays
94 * with values acceptable for that option as
97 * @return OptionsResolverInterface The resolver instance.
99 * @throws Exception\InvalidOptionsException If an option has not been defined
100 * (see {@link isKnown()}) for which
101 * an allowed value is set.
103 public function setAllowedValues(array $allowedValues);
106 * Adds allowed values for a list of options.
108 * The values are merged with the allowed values defined previously.
110 * @param array $allowedValues A list of option names as keys and arrays
111 * with values acceptable for that option as
114 * @return OptionsResolverInterface The resolver instance.
116 * @throws Exception\InvalidOptionsException If an option has not been defined
117 * (see {@link isKnown()}) for which
118 * an allowed value is set.
120 public function addAllowedValues(array $allowedValues);
123 * Sets allowed types for a list of options.
125 * @param array $allowedTypes A list of option names as keys and type
126 * names passed as string or array as values.
128 * @return OptionsResolverInterface The resolver instance.
130 * @throws Exception\InvalidOptionsException If an option has not been defined for
131 * which an allowed type is set.
133 public function setAllowedTypes(array $allowedTypes);
136 * Adds allowed types for a list of options.
138 * The types are merged with the allowed types defined previously.
140 * @param array $allowedTypes A list of option names as keys and type
141 * names passed as string or array as values.
143 * @return OptionsResolverInterface The resolver instance.
145 * @throws Exception\InvalidOptionsException If an option has not been defined for
146 * which an allowed type is set.
148 public function addAllowedTypes(array $allowedTypes);
151 * Sets normalizers that are applied on resolved options.
153 * The normalizers should be closures with the following signature:
156 * function (Options $options, $value)
159 * The second parameter passed to the closure is the value of
162 * The closure should return the normalized value.
164 * @param array $normalizers An array of closures.
166 * @return OptionsResolverInterface The resolver instance.
168 public function setNormalizers(array $normalizers);
171 * Returns whether an option is known.
173 * An option is known if it has been passed to either {@link setDefaults()},
174 * {@link setRequired()} or {@link setOptional()} before.
176 * @param string $option The name of the option.
178 * @return Boolean Whether the option is known.
180 public function isKnown($option);
183 * Returns whether an option is required.
185 * An option is required if it has been passed to {@link setRequired()},
186 * but not to {@link setDefaults()}. That is, the option has been declared
187 * as required and no default value has been set.
189 * @param string $option The name of the option.
191 * @return Boolean Whether the option is required.
193 public function isRequired($option);
196 * Returns the combination of the default and the passed options.
198 * @param array $options The custom option values.
200 * @return array A list of options and their values.
202 * @throws Exception\InvalidOptionsException If any of the passed options has not
203 * been defined or does not contain an
205 * @throws Exception\MissingOptionsException If a required option is missing.
206 * @throws Exception\OptionDefinitionException If a cyclic dependency is detected
207 * between two lazy options.
209 public function resolve(array $options = array());