Validation rules: min/max/between: Use string length to compare strings
This commit is contained in:
parent
faf74150e9
commit
8d090438b6
|
@ -0,0 +1,10 @@
|
|||
<?php
|
||||
|
||||
namespace Engelsystem\Http\Validation\Rules;
|
||||
|
||||
use Respect\Validation\Rules\Between as RespectBetween;
|
||||
|
||||
class Between extends RespectBetween
|
||||
{
|
||||
use StringInputLength;
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
<?php
|
||||
|
||||
namespace Engelsystem\Http\Validation\Rules;
|
||||
|
||||
use Respect\Validation\Rules\Max as RespectMax;
|
||||
|
||||
class Max extends RespectMax
|
||||
{
|
||||
use StringInputLength;
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
<?php
|
||||
|
||||
namespace Engelsystem\Http\Validation\Rules;
|
||||
|
||||
use Respect\Validation\Rules\Min as RespectMin;
|
||||
|
||||
class Min extends RespectMin
|
||||
{
|
||||
use StringInputLength;
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
<?php
|
||||
|
||||
namespace Engelsystem\Http\Validation\Rules;
|
||||
|
||||
use DateTime;
|
||||
use Illuminate\Support\Str;
|
||||
use Throwable;
|
||||
|
||||
trait StringInputLength
|
||||
{
|
||||
/**
|
||||
* Use the input length of a string
|
||||
*
|
||||
* @param mixed $input
|
||||
* @return bool
|
||||
*/
|
||||
public function validate($input): bool
|
||||
{
|
||||
if (
|
||||
is_string($input)
|
||||
&& !is_numeric($input)
|
||||
&& !$this->isDateTime($input)
|
||||
) {
|
||||
$input = Str::length($input);
|
||||
}
|
||||
|
||||
return parent::validate($input);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $input
|
||||
* @return bool
|
||||
*/
|
||||
protected function isDateTime($input): bool
|
||||
{
|
||||
try {
|
||||
new DateTime($input);
|
||||
} catch (Throwable $e) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
<?php
|
||||
|
||||
namespace Engelsystem\Test\Unit\Http\Validation\Rules;
|
||||
|
||||
use Engelsystem\Http\Validation\Rules\Between;
|
||||
use Engelsystem\Test\Unit\TestCase;
|
||||
|
||||
class BetweenTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @covers \Engelsystem\Http\Validation\Rules\Between
|
||||
*/
|
||||
public function testValidate()
|
||||
{
|
||||
$rule = new Between(3, 10);
|
||||
$this->assertFalse($rule->validate(1));
|
||||
$this->assertFalse($rule->validate('11'));
|
||||
$this->assertTrue($rule->validate(5));
|
||||
$this->assertFalse($rule->validate('AS'));
|
||||
$this->assertFalse($rule->validate('TestContentThatCounts'));
|
||||
$this->assertTrue($rule->validate('TESTING'));
|
||||
|
||||
$rule = new Between('2042-01-01', '2042-10-10');
|
||||
$this->assertFalse($rule->validate('2000-01-01'));
|
||||
$this->assertFalse($rule->validate('3000-01-01'));
|
||||
$this->assertTrue($rule->validate('2042-05-11'));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
<?php
|
||||
|
||||
namespace Engelsystem\Test\Unit\Http\Validation\Rules;
|
||||
|
||||
use Engelsystem\Http\Validation\Rules\Max;
|
||||
use Engelsystem\Test\Unit\TestCase;
|
||||
|
||||
class MaxTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @covers \Engelsystem\Http\Validation\Rules\Max
|
||||
*/
|
||||
public function testValidate()
|
||||
{
|
||||
$rule = new Max(3);
|
||||
$this->assertFalse($rule->validate(10));
|
||||
$this->assertFalse($rule->validate('22'));
|
||||
$this->assertTrue($rule->validate(3));
|
||||
$this->assertFalse($rule->validate('TEST'));
|
||||
$this->assertTrue($rule->validate('AS'));
|
||||
|
||||
$rule = new Max('2042-01-01');
|
||||
$this->assertFalse($rule->validate('2100-01-01'));
|
||||
$this->assertTrue($rule->validate('2000-01-01'));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
<?php
|
||||
|
||||
namespace Engelsystem\Test\Unit\Http\Validation\Rules;
|
||||
|
||||
use Engelsystem\Http\Validation\Rules\Min;
|
||||
use Engelsystem\Test\Unit\TestCase;
|
||||
|
||||
class MinTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @covers \Engelsystem\Http\Validation\Rules\Min
|
||||
*/
|
||||
public function testValidate()
|
||||
{
|
||||
$rule = new Min(3);
|
||||
$this->assertFalse($rule->validate(1));
|
||||
$this->assertFalse($rule->validate('2'));
|
||||
$this->assertTrue($rule->validate(3));
|
||||
$this->assertFalse($rule->validate('AS'));
|
||||
$this->assertTrue($rule->validate('TEST'));
|
||||
|
||||
$rule = new Min('2042-01-01');
|
||||
$this->assertFalse($rule->validate('2000-01-01'));
|
||||
$this->assertTrue($rule->validate('2345-01-01'));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
<?php
|
||||
|
||||
namespace Engelsystem\Test\Unit\Http\Validation\Rules;
|
||||
|
||||
use Engelsystem\Test\Unit\Http\Validation\Rules\Stub\UsesStringInputLength;
|
||||
use Engelsystem\Test\Unit\TestCase;
|
||||
|
||||
class StringInputLengthTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @covers \Engelsystem\Http\Validation\Rules\StringInputLength::validate
|
||||
* @covers \Engelsystem\Http\Validation\Rules\StringInputLength::isDateTime
|
||||
* @dataProvider validateProvider
|
||||
* @param mixed $input
|
||||
* @param mixed $expectedInput
|
||||
*/
|
||||
public function testValidate($input, $expectedInput)
|
||||
{
|
||||
$rule = new UsesStringInputLength();
|
||||
$rule->validate($input);
|
||||
|
||||
$this->assertEquals($expectedInput, $rule->lastInput);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array[]
|
||||
*/
|
||||
public function validateProvider()
|
||||
{
|
||||
return [
|
||||
['TEST', 4],
|
||||
['?', 1],
|
||||
['2042-01-01 00:00', '2042-01-01 00:00'],
|
||||
['3', '3'],
|
||||
];
|
||||
}
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
<?php
|
||||
|
||||
namespace Engelsystem\Test\Unit\Http\Validation\Rules\Stub;
|
||||
|
||||
class ParentClassImplementation
|
||||
{
|
||||
/** @var bool */
|
||||
public $validateResult = true;
|
||||
|
||||
/** @var mixed */
|
||||
public $lastInput;
|
||||
|
||||
/**
|
||||
* @param mixed $input
|
||||
* @return bool
|
||||
*/
|
||||
public function validate($input): bool
|
||||
{
|
||||
$this->lastInput = $input;
|
||||
|
||||
return $this->validateResult;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
<?php
|
||||
|
||||
namespace Engelsystem\Test\Unit\Http\Validation\Rules\Stub;
|
||||
|
||||
use Engelsystem\Http\Validation\Rules\StringInputLength;
|
||||
|
||||
class UsesStringInputLength extends ParentClassImplementation
|
||||
{
|
||||
use StringInputLength;
|
||||
}
|
|
@ -50,9 +50,10 @@ class ValidatorTest extends TestCase
|
|||
));
|
||||
|
||||
$this->assertFalse($val->validate(
|
||||
['lorem' => 2],
|
||||
['lorem' => 'required|min:3|max:10']
|
||||
['lorem' => 'OMG'],
|
||||
['lorem' => 'required|min:4|max:10']
|
||||
));
|
||||
$this->assertEquals(['lorem' => ['validation.lorem.min']], $val->getErrors());
|
||||
$this->assertFalse($val->validate(
|
||||
['lorem' => 42],
|
||||
['lorem' => 'required|min:3|max:10']
|
||||
|
|
Loading…
Reference in New Issue