Add features to Symfony OptionsResolver component.
composer require "steevanb/symfony-options-resolver": "^1.1.0"
Define an optional option with a default value:
use steevanb\SymfonyOptionsResolver\OptionsResolver;
$optionsResolver = new OptionsResolver();
# Configure an optional option
$optionsResolver->configureOption('foo', ['string'], 'default', ['default', 'value1', 'value2']);
# Equivalent to call original OptionsResolver methods:
$optionsResolver
->setDefined('foo')
->setAllowedTypes('foo', ['string'])
->setDefault('foo', 'default')
->setAllowedValues('foo', ['default', 'value1', 'value2']);
Define a required option:
use steevanb\SymfonyOptionsResolver\OptionsResolver;
$optionsResolver = new OptionsResolver();
# Configure a required option
$optionsResolver->configureRequiredOption('foo', ['string'], ['value1', 'value2']);
# Equivalent to call original OptionsResolver methods:
$optionsResolver
->setRequired('foo')
->setAllowedTypes('foo', ['string'])
->setAllowedValues('foo', ['value1', 'value2']);
Allow unknown keys:
use steevanb\SymfonyOptionsResolver\OptionsResolver;
# This will not throw an exception because extraKey is not configured
(new OptionsResolver())
->configureRequiredOption('foo', ['string'], ['value1', 'value2'])
->setAllowUnknownKeys(true)
->resolve(['foo' => 'value1', 'extraKey' => 'extraValue']);