mirror of
https://github.com/Respect/Validation.git
synced 2024-06-28 10:10:09 +02:00
Added Roman Numbers Validator
This commit is contained in:
parent
32de8bae81
commit
84d8fcdff4
50
library/Respect/Validation/Exceptions/RomanException.php
Normal file
50
library/Respect/Validation/Exceptions/RomanException.php
Normal file
|
@ -0,0 +1,50 @@
|
|||
<?php
|
||||
|
||||
namespace Respect\Validation\Exceptions;
|
||||
|
||||
class RomanException extends ValidationException
|
||||
{
|
||||
|
||||
public static $defaultTemplates = array(
|
||||
self::MODE_DEFAULT => array(
|
||||
self::STANDARD => '{{name}} must be a valid roman number',
|
||||
),
|
||||
self::MODE_NEGATIVE => array(
|
||||
self::STANDARD => '{{name}} must not be a valid roman number',
|
||||
)
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* LICENSE
|
||||
*
|
||||
* Copyright (c) 2011, Jean Pimentel.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* * Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* * Neither the name of Alexandre Gomes Gaigalas nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
*/
|
49
library/Respect/Validation/Rules/Roman.php
Normal file
49
library/Respect/Validation/Rules/Roman.php
Normal file
|
@ -0,0 +1,49 @@
|
|||
<?php
|
||||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
class Roman extends AbstractRule
|
||||
{
|
||||
|
||||
public function validate($input)
|
||||
{
|
||||
if(empty($input))
|
||||
return false;
|
||||
|
||||
return (bool) preg_match('/^M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$/', $input);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* LICENSE
|
||||
*
|
||||
* Copyright (c) 2011, Jean Pimentel
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* * Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* * Neither the name of Alexandre Gomes Gaigalas nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
*/
|
67
tests/library/Respect/Validation/Rules/Roman.php
Normal file
67
tests/library/Respect/Validation/Rules/Roman.php
Normal file
|
@ -0,0 +1,67 @@
|
|||
<?php
|
||||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
class RomanTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
protected $romanValidator;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
$this->romanValidator = new Roman;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForRoman
|
||||
*
|
||||
*/
|
||||
public function test_valid_romans_should_return_True($input)
|
||||
{
|
||||
$this->assertTrue($this->romanValidator->validate($input));
|
||||
$this->assertTrue($this->romanValidator->assert($input));
|
||||
$this->assertTrue($this->romanValidator->check($input));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForNotRoman
|
||||
* @expectedException Respect\Validation\Exceptions\RomanException
|
||||
*/
|
||||
public function test_invalid_romans_should_throw_RomanException($input)
|
||||
{
|
||||
$this->assertFalse($this->romanValidator->validate($input));
|
||||
$this->assertFalse($this->romanValidator->assert($input));
|
||||
}
|
||||
|
||||
public function providerForRoman()
|
||||
{
|
||||
return array(
|
||||
array('III'),
|
||||
array('IV'),
|
||||
array('VI'),
|
||||
array('XIX'),
|
||||
array('XLII'),
|
||||
array('LXII'),
|
||||
array('CXLIX'),
|
||||
array('CLIII'),
|
||||
array('MCCXXXIV'),
|
||||
array('MMXXIV'),
|
||||
array('MCMLXXV'),
|
||||
array('MMMMCMXCIX'),
|
||||
);
|
||||
}
|
||||
|
||||
public function providerForNotRoman()
|
||||
{
|
||||
return array(
|
||||
array(null),
|
||||
array('IIII'),
|
||||
array('IVVVX'),
|
||||
array('CCDC'), //
|
||||
array('MXM'),
|
||||
array('XIIIIIIII'),
|
||||
array('MIMIMI'),
|
||||
);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue