mirror of
https://github.com/Respect/Validation.git
synced 2024-06-07 16:22:16 +02:00
StartsWith and EndsWith validators
This commit is contained in:
parent
62f19fe798
commit
b12b81685e
12
library/Respect/Validation/Exceptions/EndsWithException.php
Normal file
12
library/Respect/Validation/Exceptions/EndsWithException.php
Normal file
|
@ -0,0 +1,12 @@
|
|||
<?php
|
||||
|
||||
namespace Respect\Validation\Exceptions;
|
||||
|
||||
class EndsWithException extends ValidationException
|
||||
{
|
||||
|
||||
public static $defaultTemplates = array(
|
||||
self::STANDARD => '{{name}} must end with ({{endValue}})',
|
||||
);
|
||||
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
<?php
|
||||
|
||||
namespace Respect\Validation\Exceptions;
|
||||
|
||||
class StartsWithException extends ValidationException
|
||||
{
|
||||
|
||||
public static $defaultTemplates = array(
|
||||
self::STANDARD => '{{name}} must start with ({{startValue}})',
|
||||
);
|
||||
|
||||
}
|
43
library/Respect/Validation/Rules/EndsWith.php
Normal file
43
library/Respect/Validation/Rules/EndsWith.php
Normal file
|
@ -0,0 +1,43 @@
|
|||
<?php
|
||||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
class EndsWith extends AbstractRule
|
||||
{
|
||||
|
||||
public $endValue;
|
||||
public $identical;
|
||||
|
||||
public function __construct($endValue, $identical=false)
|
||||
{
|
||||
$this->endValue = $endValue;
|
||||
$this->identical = $identical;
|
||||
}
|
||||
|
||||
public function validate($input)
|
||||
{
|
||||
if ($this->identical)
|
||||
return $this->validateIdentical($input);
|
||||
else
|
||||
return $this->validateEquals($input);
|
||||
}
|
||||
|
||||
protected function validateEquals($input)
|
||||
{
|
||||
if (is_array($input))
|
||||
return end($input) == $this->endValue;
|
||||
else
|
||||
return mb_strripos($input, $this->endValue, -1)
|
||||
=== mb_strlen($input) - mb_strlen($this->endValue) ;
|
||||
}
|
||||
|
||||
protected function validateIdentical($input)
|
||||
{
|
||||
if (is_array($input))
|
||||
return end($input) === $this->endValue;
|
||||
else
|
||||
return mb_strrpos($input, $this->endValue)
|
||||
=== mb_strlen($input) - mb_strlen($this->endValue);
|
||||
}
|
||||
|
||||
}
|
41
library/Respect/Validation/Rules/StartsWith.php
Normal file
41
library/Respect/Validation/Rules/StartsWith.php
Normal file
|
@ -0,0 +1,41 @@
|
|||
<?php
|
||||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
class StartsWith extends AbstractRule
|
||||
{
|
||||
|
||||
public $startValue;
|
||||
public $identical;
|
||||
|
||||
public function __construct($startValue, $identical=false)
|
||||
{
|
||||
$this->startValue = $startValue;
|
||||
$this->identical = $identical;
|
||||
}
|
||||
|
||||
public function validate($input)
|
||||
{
|
||||
if ($this->identical)
|
||||
return $this->validateIdentical($input);
|
||||
else
|
||||
return $this->validateEquals($input);
|
||||
}
|
||||
|
||||
protected function validateEquals($input)
|
||||
{
|
||||
if (is_array($input))
|
||||
return reset($input) == $this->startValue;
|
||||
else
|
||||
return 0 === mb_stripos($input, $this->startValue);
|
||||
}
|
||||
|
||||
protected function validateIdentical($input)
|
||||
{
|
||||
if (is_array($input))
|
||||
return reset($input) === $this->startValue;
|
||||
else
|
||||
return 0 === mb_strpos($input, $this->startValue);
|
||||
}
|
||||
|
||||
}
|
|
@ -11,6 +11,7 @@ class NegativeTest extends \PHPUnit_Framework_TestCase
|
|||
* Toggle this to show an example of all validation
|
||||
* messages on the PHPUnit console.
|
||||
*/
|
||||
|
||||
protected $showMessages = false;
|
||||
protected $targetName = 'My Field';
|
||||
|
||||
|
@ -18,6 +19,7 @@ class NegativeTest extends \PHPUnit_Framework_TestCase
|
|||
{
|
||||
try {
|
||||
$validator->assert($invalidValue);
|
||||
$this->fail();
|
||||
} catch (ValidationException $e) {
|
||||
if ($this->showMessages)
|
||||
echo 'Default: ' . $e->getFullMessage() . PHP_EOL;
|
||||
|
@ -25,6 +27,7 @@ class NegativeTest extends \PHPUnit_Framework_TestCase
|
|||
$validator->setName($this->targetName);
|
||||
try {
|
||||
$validator->assert($invalidValue);
|
||||
$this->fail();
|
||||
} catch (ValidationException $e) {
|
||||
if ($this->showMessages)
|
||||
echo 'Named: ' . $e->getFullMessage() . PHP_EOL . PHP_EOL;
|
||||
|
@ -189,6 +192,16 @@ class NegativeTest extends \PHPUnit_Framework_TestCase
|
|||
$this->doTestValidator(v::string(), null);
|
||||
}
|
||||
|
||||
public function testSartsWith()
|
||||
{
|
||||
$this->doTestValidator(v::startsWith('Xello'), 'Hello World');
|
||||
}
|
||||
|
||||
public function testEndsWith()
|
||||
{
|
||||
$this->doTestValidator(v::endsWith('Yorld'), 'Hello World');
|
||||
}
|
||||
|
||||
public function testAllOf()
|
||||
{
|
||||
$this->doTestValidator(v::allOf(
|
||||
|
|
53
tests/library/Respect/Validation/Rules/EndsWithTest.php
Normal file
53
tests/library/Respect/Validation/Rules/EndsWithTest.php
Normal file
|
@ -0,0 +1,53 @@
|
|||
<?php
|
||||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
class EndsWithTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
/**
|
||||
* @dataProvider providerForEndsWith
|
||||
*
|
||||
*/
|
||||
public function testEndsWith($start, $input)
|
||||
{
|
||||
$v = new EndsWith($start);
|
||||
$this->assertTrue($v->assert($input));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForNotEndsWith
|
||||
* @expectedException Respect\Validation\Exceptions\EndsWithException
|
||||
*/
|
||||
public function testNotEndsWith($start, $input, $caseSensitive=false)
|
||||
{
|
||||
$v = new EndsWith($start, $caseSensitive);
|
||||
$this->assertFalse($v->assert($input));
|
||||
}
|
||||
|
||||
public function providerForEndsWith()
|
||||
{
|
||||
return array(
|
||||
array('foo', array('bar', 'foo')),
|
||||
array('foo', 'barbazFOO'),
|
||||
array('foo', 'barbazfoo'),
|
||||
array('foo', 'foobazfoo'),
|
||||
array('1', array(2, 3, 1)),
|
||||
array('1', array(2, 3, '1'), true),
|
||||
);
|
||||
}
|
||||
|
||||
public function providerForNotEndsWith()
|
||||
{
|
||||
return array(
|
||||
array('bat', array('bar', 'foo')),
|
||||
array('foo', 'barfaabaz'),
|
||||
array('foo', 'barbazFOO', true),
|
||||
array('foo', 'faabarbaz'),
|
||||
array('foo', 'baabazfaa'),
|
||||
array('foo', 'baafoofaa'),
|
||||
array('1', array(1, '1', 3), true),
|
||||
);
|
||||
}
|
||||
|
||||
}
|
53
tests/library/Respect/Validation/Rules/StartsWithTest.php
Normal file
53
tests/library/Respect/Validation/Rules/StartsWithTest.php
Normal file
|
@ -0,0 +1,53 @@
|
|||
<?php
|
||||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
class StartsWithTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
/**
|
||||
* @dataProvider providerForStartsWith
|
||||
*
|
||||
*/
|
||||
public function testStartsWith($start, $input)
|
||||
{
|
||||
$v = new StartsWith($start);
|
||||
$this->assertTrue($v->assert($input));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForNotStartsWith
|
||||
* @expectedException Respect\Validation\Exceptions\StartsWithException
|
||||
*/
|
||||
public function testNotStartsWith($start, $input, $caseSensitive=false)
|
||||
{
|
||||
$v = new StartsWith($start, $caseSensitive);
|
||||
$this->assertFalse($v->assert($input));
|
||||
}
|
||||
|
||||
public function providerForStartsWith()
|
||||
{
|
||||
return array(
|
||||
array('foo', array('foo', 'bar')),
|
||||
array('foo', 'FOObarbaz'),
|
||||
array('foo', 'foobarbaz'),
|
||||
array('foo', 'foobazfoo'),
|
||||
array('1', array(1, 2, 3)),
|
||||
array('1', array('1', 2, 3), true),
|
||||
);
|
||||
}
|
||||
|
||||
public function providerForNotStartsWith()
|
||||
{
|
||||
return array(
|
||||
array('bat', array('foo', 'bar')),
|
||||
array('foo', 'barfaabaz'),
|
||||
array('foo', 'FOObarbaz', true),
|
||||
array('foo', 'faabarbaz'),
|
||||
array('foo', 'baabazfaa'),
|
||||
array('foo', 'baafoofaa'),
|
||||
array('1', array(1, '1', 3), true),
|
||||
);
|
||||
}
|
||||
|
||||
}
|
|
@ -157,6 +157,15 @@ class ValidatorTest extends \PHPUnit_Framework_TestCase
|
|||
v::string()->assert('Hello World');
|
||||
}
|
||||
|
||||
public function testSartsWith()
|
||||
{
|
||||
v::startsWith('Hello')->assert('Hello World');
|
||||
}
|
||||
public function testEndsWith()
|
||||
{
|
||||
v::endsWith('World')->assert('Hello World');
|
||||
}
|
||||
|
||||
public function testAllOf()
|
||||
{
|
||||
v::allOf(
|
||||
|
|
Loading…
Reference in a new issue