$defined
$defined :
The names of all defined options.
Validates options and merges them with default values.
$locked :
Whether the instance is locked for reading.
Once locked, the options cannot be changed anymore. This is necessary in order to avoid inconsistencies during the resolving process. If any option is changed after being read, all evaluated lazy options that depend on this option would become invalid.
setDefault(string $option, mixed $value) : $this
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 \Symfony\Component\OptionsResolver\resolve() is called. The closure has access to the resolved values of other options through the passed 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 Options object is spread across different locations of your code, such as base and sub-classes.
string | $option | The name of the option |
mixed | $value | The default value of the option |
If called from a lazy option or normalizer
setDefaults(array $defaults) : $this
Sets a list of default values.
The options can either be values of any types or closures that evaluate the option value lazily. These closures must have one of the following signatures:
function (Options $options)
function (Options $options, $value)
The second parameter passed to the closure is the previously set default value, in case you are overwriting an existing default value.
The closures should return the lazily created option value.
array | $defaults | The default values to set |
If called from a lazy option or normalizer
hasDefault(string $option) : boolean
Returns whether a default value is set for an option.
Returns true if \Symfony\Component\OptionsResolver\setDefault() was called for this option. An option is also considered set if it was set to null.
string | $option | The option name |
Whether a default value is set
setRequired(string|array<mixed,string> $optionNames) : $this
Marks one or more options as required.
If these options are not passed to \Symfony\Component\OptionsResolver\resolve() and no default has been set for them, an exception will be thrown.
string|array<mixed,string> | $optionNames | One or more option names |
If called from a lazy option or normalizer
isMissing(string $option) : boolean
Returns whether an option is missing a default value.
An option is missing if it was passed to \Symfony\Component\OptionsResolver\setRequired(), but not to \Symfony\Component\OptionsResolver\setDefault(). This option must be passed explicitly to \Symfony\Component\OptionsResolver\resolve(), otherwise an exception will be thrown.
string | $option | The name of the option |
Whether the option is missing
setDefined(string|array<mixed,string> $optionNames) : $this
Defines a valid option name.
Defines an option name without setting a default value. The option will be accepted when passed to \Symfony\Component\OptionsResolver\resolve(). When not passed, the option will not be included in the resolved options.
string|array<mixed,string> | $optionNames | One or more option names |
If called from a lazy option or normalizer
isDefined(string $option) : boolean
Returns whether an option is defined.
Returns true for any option passed to \Symfony\Component\OptionsResolver\setDefault(), \Symfony\Component\OptionsResolver\setRequired() or \Symfony\Component\OptionsResolver\setDefined().
string | $option | The option name |
Whether the option is defined
setNormalizer(string $option, \Closure $normalizer) : $this
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 \Symfony\Component\OptionsResolver\resolve() is called. The closure has access to the resolved values of other options through the passed 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.
string | $option | The option name |
\Closure | $normalizer | The normalizer |
If the option is undefined
If called from a lazy option or normalizer
setNormalizers(array $normalizers) : $this
Sets the normalizers for an array of options.
The normalizers should be closures with the following signature:
function (Options $options, $value)
The second parameter passed to the closure is the value of the option.
The closure should return the normalized value.
array | $normalizers | An array of closures |
If the option is undefined
If called from a lazy option or normalizer
setAllowedValues(string $option, mixed $allowedValues = null) : $this
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.
string | $option | The option name |
mixed | $allowedValues | One or more acceptable values/closures |
If the option is undefined
If called from a lazy option or normalizer
None found |
addAllowedValues(string $option, mixed $allowedValues = null) : $this
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.
string | $option | The option name |
mixed | $allowedValues | One or more acceptable values/closures |
If the option is undefined
If called from a lazy option or normalizer
None found |
setAllowedTypes(string $option, string|array<mixed,string> $allowedTypes = null) : $this
Sets allowed types for an option.
Any type for which a corresponding is_
string | $option | The option name |
string|array<mixed,string> | $allowedTypes | One or more accepted types |
If the option is undefined
If called from a lazy option or normalizer
None found |
addAllowedTypes(string $option, string|array<mixed,string> $allowedTypes = null) : $this
Adds allowed types for an option.
The types are merged with the allowed types defined previously.
Any type for which a corresponding is_
string | $option | The option name |
string|array<mixed,string> | $allowedTypes | One or more accepted types |
If the option is undefined
If called from a lazy option or normalizer
None found |
remove(string|array<mixed,string> $optionNames) : $this
Removes the option with the given name.
Undefined options are ignored.
string|array<mixed,string> | $optionNames | One or more option names |
If called from a lazy option or normalizer
None found |
clear() : $this
Removes all options.
If called from a lazy option or normalizer
None found |
resolve(array $options = array()) : array
Merges options with the default values stored in the container and validates them.
Exceptions are thrown if:
array | $options | A map of option names to values |
If an option name is undefined
If an option doesn't fulfill the specified validation rules
If a required option is missing
If there is a cyclic dependency between lazy options and/or normalizers
If a lazy option reads an unavailable option
If called from a lazy option or normalizer
The merged and validated options
None found |
offsetGet(string $option) : mixed
Returns the resolved value of an option.
string | $option | The option name |
If accessing this method outside of {@link resolve()}
If the option is not set
If the option doesn't fulfill the specified validation rules
If there is a cyclic dependency between lazy options and/or normalizers
The option value
None found |
offsetExists(string $option) : boolean
Returns whether a resolved option with the given name exists.
string | $option | The option name |
If accessing this method outside of {@link resolve()}
Whether the option is set
None found |
offsetSet( $option, $value)
Not supported.
$option | ||
$value |
None found |
offsetUnset( $option)
Not supported.
$option |
None found |
count() : integer
Returns the number of set options.
This may be only a subset of the defined options.
If accessing this method outside of {@link resolve()}
Number of options
None found |
None found |
replace(array $defaults)
Shortcut for {@link clear()} and {@link setDefaults()}.
array | $defaults |
None found |
None found |
None found |
None found |
replaceDefaults(array $defaultValues) : $this
Shortcut for {@link clear()} and {@link setDefaults()}.
Old defaults are erased, which means that closures passed here cannot access the previous default value. This may be useful to improve performance if the previous default value is calculated by an expensive closure.
array | $defaultValues | A list of option names as keys and default values or closures as values |
None found |
setOptional(array $optionNames) : $this
Alias of {@link setDefined()}.
This method declares valid option names without setting default values for them. If these options are not passed to \Symfony\Component\OptionsResolver\resolve() and no default has been set for them, they will be missing in the final options array. This can be helpful if you want to determine whether an option has been set or not because otherwise \Symfony\Component\OptionsResolver\resolve() would trigger an exception for unknown options.
array | $optionNames | A list of option names |
None found |
isKnown(string $option) : boolean
Alias of {@link isDefined()}.
An option is known if it has been passed to either \Symfony\Component\OptionsResolver\setDefaults(), \Symfony\Component\OptionsResolver\setRequired() or \Symfony\Component\OptionsResolver\setOptional() before.
string | $option | The name of the option |
Whether the option is known
None found |
formatTypeOf(mixed $value) : string
Returns a string representation of the type of the value.
This method should be used if you pass the type of a value as message parameter to a constraint violation. Note that such parameters should usually not be included in messages aimed at non-technical people.
mixed | $value | The value to return the type of |
The type of the value
None found |
formatValue(mixed $value) : string
Returns a string representation of the value.
This method returns the equivalent PHP tokens for most scalar types (i.e. "false" for false, "1" for 1 etc.). Strings are always wrapped in double quotes (").
mixed | $value | The value to format as string |
The string representation of the passed value
None found |
formatValues(array $values) : string
Returns a string representation of a list of values.
Each of the values is converted to a string using \Symfony\Component\OptionsResolver\formatValue(). The values are then concatenated with commas.
array | $values | A list of values |
The string representation of the value list
None found |