laminas-validator icon indicating copy to clipboard operation
laminas-validator copied to clipboard

proposal: refactoring of options configuration

Open codisart opened this issue 4 years ago • 5 comments

Q A
QA yes

Description

This pull request is my proposal to refactor the options system for the validator objects. I have two propositions.

The first one is to use a simple trait with a single public method to reduce the duplication of the code that transform function argumetns into an array of options

The second one is to use a full fleged object to do the transformation. It is in my opinion heavier to use but I think it could be the base of more refactoring of the options system. Maybe adding validation.

codisart avatar Apr 18 '20 16:04 codisart

perhaps drop the trait, but rather add static methods to ValidatorOptionsObject for factory patterns.

glensc avatar Aug 11 '20 19:08 glensc

Hello @glensc, thank you for the review.

I like your idea, what do you think of

$options = ValidatorOptions::asArray(['min', 'max', 'inclusive'], func_get_args());
class ValidatorOptions
{
    public static asArray(array $keys, array $args) {
        $this->shouldHaveOnlyStringValues($keys);

        $result = [];
        foreach ($args as $arg) {
            $result[array_shift($keys)] = $arg;
        }
        return $result;
    }

    private function shouldHaveOnlyStringValues(array $keys) : void
    {
        foreach ($keys as $key) {
            if (! is_string($key)) {
                throw new \InvalidArgumentException('The values of $keys should be only strings');
            }
        }
    }
}

codisart avatar Oct 01 '20 21:10 codisart

@codisart Could you please create an RFC issue instead of a PR? I think we might want to have some more feedback on what we want to change and having an issue without a concrete implementation detail could engage more people in participating the discussion.

boesing avatar Nov 18 '20 00:11 boesing

Hello @boesing,

I never did a RFC issue. Do you have an exemple to follow ?

codisart avatar Nov 18 '20 09:11 codisart

@codisart Sure, have a look at discourse for now, as we are trying to move RFCs to github issues since the november TSC meeting. 👍

boesing avatar Nov 18 '20 14:11 boesing