Properties

$defined

$defined

The names of all defined options.

$defaults

$defaults

The default option values.

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

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

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

$typeAliases

$typeAliases

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.

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

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 —

setNormalizers()

setNormalizers(array  $normalizers) : $this

Sets the normalizers for an array of options.

Parameters

array $normalizers

An array of 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 —

setAllowedValues()

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.

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

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 = null) : $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 = 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_() 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 —

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()) : 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) : mixed

Returns the resolved value of an option.

Parameters

string $option

The option name

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

set()

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

Alias of {@link setDefault()}.

Parameters

mixed $option
mixed $value

Returns

mixed —

replace()

replace(array  $defaults) : mixed

Shortcut for {@link clear()} and {@link setDefaults()}.

Parameters

array $defaults

Returns

mixed —

overload()

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

Alias of {@link setDefault()}.

Parameters

mixed $option
mixed $value

Returns

mixed —

get()

get(mixed  $option) : mixed

Alias of {@link offsetGet()}.

Parameters

mixed $option

Returns

mixed —

has()

has(mixed  $option) : mixed

Alias of {@link offsetExists()}.

Parameters

mixed $option

Returns

mixed —

replaceDefaults()

replaceDefaults(array  $defaultValues) : $this

Shortcut for {@link clear()} and {@link setDefaults()}.

Parameters

array $defaultValues

A list of option names as keys and default values or closures as values

Returns

$this —

setOptional()

setOptional(array  $optionNames) : $this

Alias of {@link setDefined()}.

Parameters

array $optionNames

A list of option names

Returns

$this —

isKnown()

isKnown(mixed  $option) : bool

Alias of {@link isDefined()}.

Parameters

mixed $option

The name of the option

Returns

bool —

Whether the option is known

formatTypeOf()

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.

Parameters

mixed $value

The value to return the type of

Returns

string —

The type of the value

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 —

The string representation of the passed value

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

A list of values

Returns

string —

The string representation of the value list