Create "Fibonacci" rule

This commit is contained in:
Samuel Heinzmann 2016-01-23 11:45:40 +01:00 committed by Henrique Moody
parent 1bd8114150
commit ce3f885862
6 changed files with 141 additions and 0 deletions

17
docs/Fibonacci.md Normal file
View 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)

View file

@ -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)

View 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',
],
];
}

View 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;
}
}

View file

@ -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()

View 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],
];
}
}