laminas-validator
laminas-validator copied to clipboard
proposal: refactoring of options configuration
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.
perhaps drop the trait, but rather add static methods to ValidatorOptionsObject for factory patterns.
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 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.
Hello @boesing,
I never did a RFC issue. Do you have an exemple to follow ?
@codisart Sure, have a look at discourse for now, as we are trying to move RFCs to github issues since the november TSC meeting. 👍