2022-12-24 22:10:40 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2024-01-31 22:15:08 +08:00
|
|
|
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
|
|
|
|
* SPDX-License-Identifier: MIT
|
2022-12-24 22:10:40 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Respect\Validation\Rules;
|
|
|
|
|
|
|
|
use function abs;
|
|
|
|
use function is_integer;
|
|
|
|
use function is_numeric;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validates if the input is a factor of the defined dividend.
|
|
|
|
*
|
|
|
|
* @author Danilo Correa <danilosilva87@gmail.com>
|
|
|
|
* @author David Meister <thedavidmeister@gmail.com>
|
|
|
|
* @author Henrique Moody <henriquemoody@gmail.com>
|
|
|
|
*/
|
|
|
|
final class Factor extends AbstractRule
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
private $dividend;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes the rule.
|
|
|
|
*/
|
|
|
|
public function __construct(int $dividend)
|
|
|
|
{
|
|
|
|
$this->dividend = $dividend;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function validate($input): bool
|
|
|
|
{
|
|
|
|
// Every integer is a factor of zero, and zero is the only integer that
|
|
|
|
// has zero for a factor.
|
|
|
|
if ($this->dividend === 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Factors must be integers that are not zero.
|
|
|
|
if (!is_numeric($input) || (int) $input != $input || $input == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$input = (int) abs((int) $input);
|
|
|
|
$dividend = (int) abs($this->dividend);
|
|
|
|
|
|
|
|
// The dividend divided by the input must be an integer if input is a
|
|
|
|
// factor of the dividend.
|
|
|
|
return is_integer($dividend / $input);
|
|
|
|
}
|
|
|
|
}
|