OptionsResolverWrapper
class OptionsResolverWrapper extends OptionsResolver
Methods
Sets the default value of a given option.
Returns whether a default value is set for an option.
Marks one or more options as required.
Returns whether an option is missing a default value.
Returns the names of all options missing a default value.
Deprecates an option, allowed types or values.
Sets allowed values for an option.
Adds allowed values for an option.
Sets allowed types for an option.
Adds allowed types for an option.
Removes the option with the given name.
Merges options with the default values stored in the container and validates them.
Returns whether a resolved option with the given name exists.
No description
Details
$this
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.
If you want to define nested options, you can pass a closure with the following signature:
$options->setDefault('database', function (OptionsResolver $resolver) {
$resolver->setDefined(array('dbname', 'host', 'port', 'user', 'pass'));
}
To get access to the parent options, add a second argument to the closure's signature:
function (OptionsResolver $resolver, Options $parent) {
// 'default' === $parent['connection']
}
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.
bool
isRequired(string $option)
Returns whether an option is required.
An option is required if it was passed to {@link setRequired()}.
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.
$this
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()}.
OptionsResolver
setDeprecated(string $option, $deprecationMessage = 'The option "%name%" is deprecated.')
Deprecates an option, allowed types or values.
Instead of passing the message, you may also pass a closure with the following signature:
function (Options $options, $value): string {
// ...
}
The closure receives the value as argument and should return a string. Return an empty string to ignore the option deprecation.
The closure is invoked when {@link resolve()} is called. The parameter passed to the closure is the value of the option after validating it and before normalizing it.
$this
setNormalizer(string $option, Closure $normalizer)
Sets the normalizer for an option.
The normalizer should be a closure with the following signature:
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.
$this
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.
$this
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.
$this
setAllowedTypes(string $option, string|string[] $allowedTypes)
Sets allowed types for an option.
Any type for which a corresponding is_
$this
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_
$this
remove(string|string[] $optionNames)
Removes the option with the given name.
Undefined options are ignored.