$defined
$defined
The names of all defined options.
Validates options and merges them with default values.
$normalizers : \Closure[]
A list of normalizer closures.
$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 {@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.
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) : 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.
string | $option | The option name |
Whether a default value is set
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.
string | $option | The name of the option |
Whether the option is missing
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.
string|string[] | $optionNames | One or more option names |
If called from a lazy option or normalizer
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.
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.
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|string[] $allowedTypes = null) : $this
Sets allowed types for an option.
Any type for which a corresponding is_
string | $option | The option name |
string|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|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|string[] | $allowedTypes | One or more accepted types |
If the option is undefined
If called from a lazy option or normalizer
None found |
remove(string|string[] $optionNames) : $this
Removes the option with the given name.
Undefined options are ignored.
string|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) : bool
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(mixed $option, mixed $value) : mixed
Not supported.
mixed | $option | |
mixed | $value |
None found |
offsetUnset(mixed $option) : mixed
Not supported.
mixed | $option |
None found |
count() : int
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 |
set(mixed $option, mixed $value) : mixed
Alias of {@link setDefault()}.
mixed | $option | |
mixed | $value |
None found |
replace(array $defaults) : mixed
Shortcut for {@link clear()} and {@link setDefaults()}.
array | $defaults |
None found |
overload(mixed $option, mixed $value) : mixed
Alias of {@link setDefault()}.
mixed | $option | |
mixed | $value |
None found |
get(mixed $option) : mixed
Alias of {@link offsetGet()}.
mixed | $option |
None found |
has(mixed $option) : mixed
Alias of {@link offsetExists()}.
mixed | $option |
None found |
replaceDefaults(array $defaultValues) : $this
Shortcut for {@link clear()} and {@link setDefaults()}.
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()}.
array | $optionNames | A list of option names |
None found |
isKnown(mixed $option) : bool
Alias of {@link isDefined()}.
mixed | $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 {@link formatValue()}. The values are then concatenated with commas.
array | $values | A list of values |
The string representation of the value list
None found |