mirror of
https://github.com/Respect/Validation.git
synced 2026-03-18 08:09:51 +01:00
Currently, we convert the properties of a rule into parameters and pass them to the exceptions. That complicates things for a few reasons: 1. The exception knows too much: there's a lot of information in an object, and the exception would only need a few parameters to work correctly. 2. Any variable change becomes a backward compatibility break: if we change the name of the variable type in a rule, even if it's a private one, we may need to change the template, which is a backward compatibility break. 3. The factory is bloated because of introspection tricks: it reads the properties from the class, even from the parent, and then passes it to the exception. Of course, that means we introduce another method to `Validatable`, but in most cases, extending `AbstractRule` is enough to create a new rule. Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
35 lines
653 B
PHP
35 lines
653 B
PHP
<?php
|
|
|
|
/*
|
|
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
|
|
* SPDX-License-Identifier: MIT
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Respect\Validation\Rules;
|
|
|
|
final class Multiple extends AbstractRule
|
|
{
|
|
public function __construct(
|
|
private readonly int $multipleOf
|
|
) {
|
|
}
|
|
|
|
public function validate(mixed $input): bool
|
|
{
|
|
if ($this->multipleOf == 0) {
|
|
return $input == 0;
|
|
}
|
|
|
|
return $input % $this->multipleOf == 0;
|
|
}
|
|
|
|
/**
|
|
* @return array<string, mixed>
|
|
*/
|
|
public function getParams(): array
|
|
{
|
|
return ['multipleOf' => $this->multipleOf];
|
|
}
|
|
}
|