steevanb/symfony-options-resolver

Add features to Symfony OptionsResolver component.

1.1.0 2019-12-06 11:16 UTC

This package is auto-updated.

Last update: 2024-12-06 23:10:22 UTC


README

version doctrine php Lines Total Downloads Scrutinizer

symfony-options-resolver

Add features to Symfony OptionsResolver component.

Changelog

Installation

composer require "steevanb/symfony-options-resolver": "^1.1.0"

Examples

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']);