$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
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.
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
setAllowedValues(string $option, mixed $allowedValues) : $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
addAllowedValues(string $option, mixed $allowedValues) : $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
setAllowedTypes(string $option, string|array<mixed,string> $allowedTypes) : $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
addAllowedTypes(string $option, string|array<mixed,string> $allowedTypes) : $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
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
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
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
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
formatTypeOf(mixed $value, string $type) : 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 |
string | $type |
The type of the value
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
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