class OptionsResolver implements Options
Validates options and merges them with default values.
Methods
OptionsResolver | setDefault(string $option, mixed $value) Sets the default value of a given option. | |
OptionsResolver | setDefaults(array $defaults) Sets a list of default values. | |
bool | hasDefault(string $option) Returns whether a default value is set for an option. | |
OptionsResolver | setRequired(string|string[] $optionNames) Marks one or more options as required. | |
bool | isRequired(string $option) Returns whether an option is required. | |
string[] | getRequiredOptions() Returns the names of all required options. | |
bool | isMissing(string $option) Returns whether an option is missing a default value. | |
string[] | getMissingOptions() Returns the names of all options missing a default value. | |
OptionsResolver | setDefined(string|string[] $optionNames) Defines a valid option name. | |
bool | isDefined(string $option) Returns whether an option is defined. | |
string[] | getDefinedOptions() Returns the names of all defined options. | |
OptionsResolver | setNormalizer(string $option, Closure $normalizer) Sets the normalizer for an option. | |
OptionsResolver | setAllowedValues(string $option, mixed $allowedValues) Sets allowed values for an option. | |
OptionsResolver | addAllowedValues(string $option, mixed $allowedValues) Adds allowed values for an option. | |
OptionsResolver | setAllowedTypes(string $option, string|string[] $allowedTypes) Sets allowed types for an option. | |
OptionsResolver | addAllowedTypes(string $option, string|string[] $allowedTypes) Adds allowed types for an option. | |
OptionsResolver | remove(string|string[] $optionNames) Removes the option with the given name. | |
OptionsResolver | clear() Removes all options. | |
array | resolve(array $options = array()) Merges options with the default values stored in the container and validates them. | |
mixed | offsetGet(string $option) Returns the resolved value of an option. | |
bool | offsetExists(string $option) Returns whether a resolved option with the given name exists. | |
offsetSet($option, $value) Not supported. | ||
offsetUnset($option) Not supported. | ||
int | count() Returns the number of set options. |
Details
OptionsResolver setDefault(string $option, mixed $value)
Sets the default value of a given option.
If the default value should be set based on other options, you can pass a closure with the following signature:
function (Options $options) {
// ...
}
The closure will be evaluated when {@link resolve()} is called. The closure has access to the resolved values of other options through the passed {@link Options} instance:
function (Options $options) {
if (isset($options['port'])) {
// ...
}
}
If you want to access the previously set default value, add a second argument to the closure's signature:
$options->setDefault('name', 'Default Name');
$options->setDefault('name', function (Options $options, $previousValue) {
// 'Default Name' === $previousValue
});
This is mostly useful if the configuration of the {@link Options} object is spread across different locations of your code, such as base and sub-classes.
OptionsResolver setDefaults(array $defaults)
Sets a list of default values.
bool hasDefault(string $option)
Returns whether a default value is set for an option.
Returns true if {@link setDefault()} was called for this option. An option is also considered set if it was set to null.
OptionsResolver setRequired(string|string[] $optionNames)
Marks one or more options as required.
bool isRequired(string $option)
Returns whether an option is required.
An option is required if it was passed to {@link setRequired()}.
string[] getRequiredOptions()
Returns the names of all required options.
bool isMissing(string $option)
Returns whether an option is missing a default value.
An option is missing if it was passed to {@link setRequired()}, but not to {@link setDefault()}. This option must be passed explicitly to {@link resolve()}, otherwise an exception will be thrown.
string[] getMissingOptions()
Returns the names of all options missing a default value.
OptionsResolver setDefined(string|string[] $optionNames)
Defines a valid option name.
Defines an option name without setting a default value. The option will be accepted when passed to {@link resolve()}. When not passed, the option will not be included in the resolved options.
bool isDefined(string $option)
Returns whether an option is defined.
Returns true for any option passed to {@link setDefault()}, {@link setRequired()} or {@link setDefined()}.
string[] getDefinedOptions()
Returns the names of all defined options.
OptionsResolver setNormalizer(string $option, Closure $normalizer)
Sets the normalizer for an option.
The normalizer should be a closure with the following signature:
php
function (Options $options, $value) {
// ...
}
The closure is invoked when {@link resolve()} is called. The closure has access to the resolved values of other options through the passed {@link Options} instance.
The second parameter passed to the closure is the value of the option.
The resolved option value is set to the return value of the closure.
OptionsResolver setAllowedValues(string $option, mixed $allowedValues)
Sets allowed values for an option.
Instead of passing values, you may also pass a closures with the following signature:
function ($value) {
// return true or false
}
The closure receives the value as argument and should return true to accept the value and false to reject the value.
OptionsResolver addAllowedValues(string $option, mixed $allowedValues)
Adds allowed values for an option.
The values are merged with the allowed values defined previously.
Instead of passing values, you may also pass a closures with the following signature:
function ($value) {
// return true or false
}
The closure receives the value as argument and should return true to accept the value and false to reject the value.
OptionsResolver setAllowedTypes(string $option, string|string[] $allowedTypes)
Sets allowed types for an option.
Any type for which a corresponding is_() function exists is acceptable. Additionally, fully-qualified class or interface names may be passed.
OptionsResolver addAllowedTypes(string $option, string|string[] $allowedTypes)
Adds allowed types for an option.
The types are merged with the allowed types defined previously.
Any type for which a corresponding is_() function exists is acceptable. Additionally, fully-qualified class or interface names may be passed.
OptionsResolver remove(string|string[] $optionNames)
Removes the option with the given name.
Undefined options are ignored.
OptionsResolver clear()
Removes all options.
array resolve(array $options = array())
Merges options with the default values stored in the container and validates them.
Exceptions are thrown if:
- Undefined options are passed;
- Required options are missing;
- Options have invalid types;
- Options have invalid values.
mixed offsetGet(string $option)
Returns the resolved value of an option.
bool offsetExists(string $option)
Returns whether a resolved option with the given name exists.
offsetSet($option, $value)
Not supported.
offsetUnset($option)
Not supported.
int count()
Returns the number of set options.
This may be only a subset of the defined options.
Please login to continue.