validator icon indicating copy to clipboard operation
validator copied to clipboard

Callable rule attribute interface

Open vjik opened this issue 2 years ago • 4 comments

Callable rule class may look like this:

final class MyRule {
    public function __invoke(mixed $value, object $rule, ValidationContext $context): Result {
        // ...
    }
}

For simple custom rules is good solution, but there is one problem. It is not possible usage as attribute.

We can add attribute Attribute:

#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
final class MyRule {
    // ...

It's not enough, because validator parse attributes that implement RuleInterface only.

Suggest add new interface CallableRuleAttributeInterface and parse implementing its attributes in addition to RuleInterface.

vjik avatar Dec 15 '22 12:12 vjik

What the reason to have two the same interfaces but with different methods' names?

RuleInterface: validate($value, $rule, $context): Result

CallableRuleInterface: __invoke($value, $rule, $context): Result

xepozz avatar Dec 15 '22 14:12 xepozz

CallableRuleInterface is not rule, it's callable only.

vjik avatar Dec 15 '22 14:12 vjik

What do you mean by it's callable only?

xepozz avatar Dec 15 '22 16:12 xepozz

What do you mean by it's callable only?

It's not rule. Object does not implement RuleInterface.

vjik avatar Dec 16 '22 10:12 vjik