Constants

VALIDATION_FUNCTIONS

VALIDATION_FUNCTIONS = ['bool' => 'is_bool', 'boolean' => 'is_bool', 'int' => 'is_int', 'integer' => 'is_int', 'long' => 'is_int', 'float' => 'is_float', 'double' => 'is_float', 'real' => 'is_float', 'numeric' => 'is_numeric', 'string' => 'is_string', 'scalar' => 'is_scalar', 'array' => 'is_array', 'iterable' => 'is_iterable', 'countable' => 'is_countable', 'callable' => 'is_callable', 'object' => 'is_object', 'resource' => 'is_resource']

Properties

$defined

$defined

The names of all defined options.

$defaults

$defaults

The default option values.

$nested

$nested : \Closure[][]

A list of closure for nested options.

Type

Closure[][]

$required

$required

The names of required options.

$resolved

$resolved

The resolved option values.

$normalizers

$normalizers : \Closure[][]

A list of normalizer closures.

Type

Closure[][]

$allowedValues

$allowedValues

A list of accepted values for each option.

$allowedTypes

$allowedTypes

A list of accepted types for each option.

$info

$info

A list of info messages for each option.

$lazy

$lazy

A list of closures for evaluating lazy options.

$calling

$calling

A list of lazy options whose closure is currently being called.

This list helps detecting circular dependencies between lazy options.

$deprecated

$deprecated

A list of deprecated options.

$given

$given

The list of options provided by the user.

$locked

$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.

$parentsOptions

$parentsOptions

Methods

setDefault()

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 {@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(['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']
}

Parameters

string $option

The name of the option

mixed $value

The default value of the option

Throws

\Symfony\Component\OptionsResolver\Exception\AccessException

If called from a lazy option or normalizer

Returns

$this —

setDefaults()

setDefaults(array  $defaults) : $this

Sets a list of default values.

Parameters

array $defaults

The default values to set

Throws

\Symfony\Component\OptionsResolver\Exception\AccessException

If called from a lazy option or normalizer

Returns

$this —

hasDefault()

hasDefault(string  $option) : bool

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.

Parameters

string $option

The option name

Returns

bool —

Whether a default value is set

setRequired()

setRequired(string|string[]  $optionNames) : $this

Marks one or more options as required.

Parameters

string|string[] $optionNames

One or more option names

Throws

\Symfony\Component\OptionsResolver\Exception\AccessException

If called from a lazy option or normalizer

Returns

$this —

isRequired()

isRequired(string  $option) : bool

Returns whether an option is required.

An option is required if it was passed to {@link setRequired()}.

Parameters

string $option

The name of the option

Returns

bool —

Whether the option is required

getRequiredOptions()

getRequiredOptions() : string[]

Returns the names of all required options.

Returns

string[] —

The names of the required options

isMissing()

isMissing(string  $option) : bool

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.

Parameters

string $option

The name of the option

Returns

bool —

Whether the option is missing

getMissingOptions()

getMissingOptions() : string[]

Returns the names of all options missing a default value.

Returns

string[] —

The names of the missing options

setDefined()

setDefined(string|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 {@link resolve()}. When not passed, the option will not be included in the resolved options.

Parameters

string|string[] $optionNames

One or more option names

Throws

\Symfony\Component\OptionsResolver\Exception\AccessException

If called from a lazy option or normalizer

Returns

$this —

isDefined()

isDefined(string  $option) : bool

Returns whether an option is defined.

Returns true for any option passed to {@link setDefault()}, {@link setRequired()} or {@link setDefined()}.

Parameters

string $option

The option name

Returns

bool —

Whether the option is defined

getDefinedOptions()

getDefinedOptions() : string[]

Returns the names of all defined options.

Returns

string[] —

The names of the defined options

isNested()

isNested(string  $option) : bool

Parameters

string $option

Returns

bool —

setDeprecated()

setDeprecated(string  $option) : self

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.

Parameters

string $option

Returns

self —

isDeprecated()

isDeprecated(string  $option) : bool

Parameters

string $option

Returns

bool —

setNormalizer()

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 {@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.

Parameters

string $option

The option name

\Closure $normalizer

The normalizer

Throws

\Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException

If the option is undefined

\Symfony\Component\OptionsResolver\Exception\AccessException

If called from a lazy option or normalizer

Returns

$this —

addNormalizer()

addNormalizer(string  $option, \Closure  $normalizer, bool  $forcePrepend = false) : $this

Adds a normalizer for an option.

The normalizer should be a closure with the following signature:

function (Options $options, $value): mixed {
    // ...
}

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.

Parameters

string $option

The option name

\Closure $normalizer

The normalizer

bool $forcePrepend

If set to true, prepend instead of appending

Throws

\Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException

If the option is undefined

\Symfony\Component\OptionsResolver\Exception\AccessException

If called from a lazy option or normalizer

Returns

$this —

setAllowedValues()

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.

Parameters

string $option

The option name

mixed $allowedValues

One or more acceptable values/closures

Throws

\Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException

If the option is undefined

\Symfony\Component\OptionsResolver\Exception\AccessException

If called from a lazy option or normalizer

Returns

$this —

addAllowedValues()

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.

Parameters

string $option

The option name

mixed $allowedValues

One or more acceptable values/closures

Throws

\Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException

If the option is undefined

\Symfony\Component\OptionsResolver\Exception\AccessException

If called from a lazy option or normalizer

Returns

$this —

setAllowedTypes()

setAllowedTypes(string  $option, string|string[]  $allowedTypes) : $this

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.

Parameters

string $option

The option name

string|string[] $allowedTypes

One or more accepted types

Throws

\Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException

If the option is undefined

\Symfony\Component\OptionsResolver\Exception\AccessException

If called from a lazy option or normalizer

Returns

$this —

addAllowedTypes()

addAllowedTypes(string  $option, string|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_() function exists is acceptable. Additionally, fully-qualified class or interface names may be passed.

Parameters

string $option

The option name

string|string[] $allowedTypes

One or more accepted types

Throws

\Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException

If the option is undefined

\Symfony\Component\OptionsResolver\Exception\AccessException

If called from a lazy option or normalizer

Returns

$this —

define()

define(string  $option) : \Symfony\Component\OptionsResolver\OptionConfigurator

Defines an option configurator with the given name.

Parameters

string $option

Returns

\Symfony\Component\OptionsResolver\OptionConfigurator —

setInfo()

setInfo(string  $option, string  $info) : $this

Sets an info message for an option.

Parameters

string $option
string $info

Throws

\Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException

If the option is undefined

\Symfony\Component\OptionsResolver\Exception\AccessException

If called from a lazy option or normalizer

Returns

$this —

getInfo()

getInfo(string  $option) : ?string

Gets the info message for an option.

Parameters

string $option

Returns

?string —

remove()

remove(string|string[]  $optionNames) : $this

Removes the option with the given name.

Undefined options are ignored.

Parameters

string|string[] $optionNames

One or more option names

Throws

\Symfony\Component\OptionsResolver\Exception\AccessException

If called from a lazy option or normalizer

Returns

$this —

clear()

clear() : $this

Removes all options.

Throws

\Symfony\Component\OptionsResolver\Exception\AccessException

If called from a lazy option or normalizer

Returns

$this —

resolve()

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.

Parameters

array $options

A map of option names to values

Throws

\Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException

If an option name is undefined

\Symfony\Component\OptionsResolver\Exception\InvalidOptionsException

If an option doesn't fulfill the specified validation rules

\Symfony\Component\OptionsResolver\Exception\MissingOptionsException

If a required option is missing

\Symfony\Component\OptionsResolver\Exception\OptionDefinitionException

If there is a cyclic dependency between lazy options and/or normalizers

\Symfony\Component\OptionsResolver\Exception\NoSuchOptionException

If a lazy option reads an unavailable option

\Symfony\Component\OptionsResolver\Exception\AccessException

If called from a lazy option or normalizer

Returns

array —

The merged and validated options

offsetGet()

offsetGet(string  $option, bool  $triggerDeprecation = true) : mixed

Returns the resolved value of an option.

Parameters

string $option

The option name

bool $triggerDeprecation

Whether to trigger the deprecation or not (true by default)

Throws

\Symfony\Component\OptionsResolver\Exception\AccessException

If accessing this method outside of {@link resolve()}

\Symfony\Component\OptionsResolver\Exception\NoSuchOptionException

If the option is not set

\Symfony\Component\OptionsResolver\Exception\InvalidOptionsException

If the option doesn't fulfill the specified validation rules

\Symfony\Component\OptionsResolver\Exception\OptionDefinitionException

If there is a cyclic dependency between lazy options and/or normalizers

Returns

mixed —

The option value

offsetExists()

offsetExists(string  $option) : bool

Returns whether a resolved option with the given name exists.

Parameters

string $option

The option name

Throws

\Symfony\Component\OptionsResolver\Exception\AccessException

If accessing this method outside of {@link resolve()}

Returns

bool —

Whether the option is set

offsetSet()

offsetSet(mixed  $option, mixed  $value) : mixed

Not supported.

Parameters

mixed $option
mixed $value

Throws

\Symfony\Component\OptionsResolver\Exception\AccessException

Returns

mixed —

offsetUnset()

offsetUnset(mixed  $option) : mixed

Not supported.

Parameters

mixed $option

Throws

\Symfony\Component\OptionsResolver\Exception\AccessException

Returns

mixed —

count()

count() : int

Returns the number of set options.

This may be only a subset of the defined options.

Throws

\Symfony\Component\OptionsResolver\Exception\AccessException

If accessing this method outside of {@link resolve()}

Returns

int —

Number of options

verifyTypes()

verifyTypes(string  $type, mixed  $value, array  $invalidTypes, int  $level) : bool

Parameters

string $type
mixed $value
array $invalidTypes
int $level

Returns

bool —

formatValue()

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 (").

Parameters

mixed $value

The value to format as string

Returns

string —

formatValues()

formatValues(array  $values) : string

Returns a string representation of a list of values.

Each of the values is converted to a string using {@link formatValue()}. The values are then concatenated with commas.

Parameters

array $values

Returns

string —

formatOptions()

formatOptions(array  $options) : string

Parameters

array $options

Returns

string —

getParameterClassName()

getParameterClassName(\ReflectionParameter  $parameter) : ?string

Parameters

\ReflectionParameter $parameter

Returns

?string —