8000 [OptionsResolver] Support simple option validation · Issue #11705 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
[OptionsResolver] Support simple option validation #11705
Closed
@webmozart

Description

@webmozart

For many projects or even Symfony components, the full functionality of the OptionsResolver is overkill. Nevertheless, the component contains code that is very useful, but not easily accessible.

I suggest to make the validate* methods in the OptionsResolver class accessible through a simple low-level API such as:

use Symfony\Component\OptionsResolver\Options;

// throws MissingOptionsException if the option(s) is (are) missing
Options::require($options, 'toTz');

// throws InvalidOptionsException if any of the options have an invalid type
Options::validateTypes($options, array(
    'fromTz' => 'string',
    'toTz' => 'string',
    'dateFormat' => 'string',
    'timeFormat' => 'string',
    'calendar' => array('null', 'integer'),
));

// throws InvalidOptionsException if any of the options contain an invalid value
Options::validateValues($options, array(
    'calendar' => array(
        \IntlDateFormatter::GREGORIAN,
        \IntlDateFormatter::TRADITIONAL,
    ),
));

// throws InvalidOptionsException if any of the given options is not known
$options = Options::resolve($options, array(
    'fromTz' => null,
    'toTz' => null,
    'dateFormat' => \IntlDateFormatter::MEDIUM,
    'timeFormat' => \IntlDateFormatter::SHORT,
    'calendar' => \IntlDateFormatter::GREGORIAN,
));

Opinions?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0