mirror of
https://github.com/Respect/Validation.git
synced 2024-05-29 11:52:34 +02:00
Create "Fibonacci" rule
This commit is contained in:
parent
1bd8114150
commit
ce3f885862
17
docs/Fibonacci.md
Normal file
17
docs/Fibonacci.md
Normal file
|
@ -0,0 +1,17 @@
|
|||
# Fibonacci
|
||||
|
||||
- `v::fibonacci()`
|
||||
|
||||
Validates whether the input follows the Fibonacci integer sequence.
|
||||
|
||||
```php
|
||||
v::fibonacci()->validate(1); // true
|
||||
v::fibonacci()->validate('34'); // true
|
||||
v::fibonacci()->validate(6); // false
|
||||
```
|
||||
|
||||
***
|
||||
See also:
|
||||
|
||||
* [PrimeNumber](PrimeNumber.md)
|
||||
* [PerfectSquare](PerfectSquare.md)
|
|
@ -52,6 +52,7 @@
|
|||
* [BoolType](BoolType.md)
|
||||
* [Even](Even.md)
|
||||
* [Factor](Factor.md)
|
||||
* [Fibonacci](Fibonacci.md)
|
||||
* [Finite](Finite.md)
|
||||
* [FloatVal](FloatVal.md)
|
||||
* [FloatType](FloatType.md)
|
||||
|
@ -238,6 +239,7 @@
|
|||
* [Extension](Extension.md)
|
||||
* [Factor](Factor.md)
|
||||
* [FalseVal](FalseVal.md)
|
||||
* [Fibonacci](Fibonacci.md)
|
||||
* [File](File.md)
|
||||
* [FilterVar](FilterVar.md)
|
||||
* [Finite](Finite.md)
|
||||
|
|
24
library/Exceptions/FibonacciException.php
Normal file
24
library/Exceptions/FibonacciException.php
Normal file
|
@ -0,0 +1,24 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Respect/Validation.
|
||||
*
|
||||
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
|
||||
*
|
||||
* For the full copyright and license information, please view the "LICENSE.md"
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Respect\Validation\Exceptions;
|
||||
|
||||
class FibonacciException extends ValidationException
|
||||
{
|
||||
public static $defaultTemplates = [
|
||||
self::MODE_DEFAULT => [
|
||||
self::STANDARD => '{{name}} must be a valid Fibonacci number',
|
||||
],
|
||||
self::MODE_NEGATIVE => [
|
||||
self::STANDARD => '{{name}} must not be a valid Fibonacci number',
|
||||
],
|
||||
];
|
||||
}
|
37
library/Rules/Fibonacci.php
Normal file
37
library/Rules/Fibonacci.php
Normal file
|
@ -0,0 +1,37 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Respect/Validation.
|
||||
*
|
||||
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
|
||||
*
|
||||
* For the full copyright and license information, please view the "LICENSE.md"
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
/**
|
||||
* @author Samuel Heinzmann <samuel.heinzman@swisscom.com>
|
||||
*/
|
||||
class Fibonacci extends AbstractRule
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function validate($input)
|
||||
{
|
||||
if (!is_numeric($input)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$sequence = [0, 1];
|
||||
$position = 1;
|
||||
while ($input > $sequence[$position]) {
|
||||
++$position;
|
||||
$sequence[$position] = $sequence[$position - 1] + $sequence[$position - 2];
|
||||
}
|
||||
|
||||
return $sequence[$position] === (int) $input;
|
||||
}
|
||||
}
|
|
@ -63,6 +63,7 @@ use Respect\Validation\Rules\Key;
|
|||
* @method static Validator extension(string $extension)
|
||||
* @method static Validator factor(int $dividend)
|
||||
* @method static Validator falseVal()
|
||||
* @method static Validator fibonacci()
|
||||
* @method static Validator file()
|
||||
* @method static Validator filterVar(int $filter, mixed $options = null)
|
||||
* @method static Validator finite()
|
||||
|
|
60
tests/unit/Rules/FibonacciTest.php
Normal file
60
tests/unit/Rules/FibonacciTest.php
Normal file
|
@ -0,0 +1,60 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Respect/Validation.
|
||||
*
|
||||
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
|
||||
*
|
||||
* For the full copyright and license information, please view the "LICENSE.md"
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
/**
|
||||
* @group rule
|
||||
* @covers Respect\Validation\Rules\Fibonacci
|
||||
*/
|
||||
class FibonacciTest extends RuleTestCase
|
||||
{
|
||||
public function providerForValidInput()
|
||||
{
|
||||
$rule = new Fibonacci();
|
||||
|
||||
return [
|
||||
[$rule, 1],
|
||||
[$rule, 2],
|
||||
[$rule, 3],
|
||||
[$rule, 5],
|
||||
[$rule, 8.0],
|
||||
[$rule, '3'],
|
||||
[$rule, 21],
|
||||
[$rule, 21.0],
|
||||
[$rule, '21.0'],
|
||||
[$rule, 34],
|
||||
[$rule, '34'],
|
||||
[$rule, 1346269],
|
||||
[$rule, 10610209857723],
|
||||
];
|
||||
}
|
||||
|
||||
public function providerForInvalidInput()
|
||||
{
|
||||
$rule = new Fibonacci();
|
||||
|
||||
return [
|
||||
[$rule, 0],
|
||||
[$rule, 1346268],
|
||||
[$rule, ''],
|
||||
[$rule, null],
|
||||
[$rule, 7],
|
||||
[$rule, -1],
|
||||
[$rule, 5.2],
|
||||
[$rule, '-1'],
|
||||
[$rule, 'a'],
|
||||
[$rule, ' '],
|
||||
[$rule, false],
|
||||
[$rule, true],
|
||||
];
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue