Added Even/Odd Validator

This commit is contained in:
Jean Pimentel 2011-09-22 00:49:42 -03:00
parent 1144ce97bd
commit afb16fcb60
6 changed files with 350 additions and 0 deletions

View file

@ -0,0 +1,50 @@
<?php
namespace Respect\Validation\Exceptions;
class EvenException extends ValidationException
{
public static $defaultTemplates = array(
self::MODE_DEFAULT => array(
self::STANDARD => '{{name}} must be an even number',
),
self::MODE_NEGATIVE => array(
self::STANDARD => '{{name}} must not be an even 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.
*
*/

View file

@ -0,0 +1,50 @@
<?php
namespace Respect\Validation\Exceptions;
class OddException extends ValidationException
{
public static $defaultTemplates = array(
self::MODE_DEFAULT => array(
self::STANDARD => '{{name}} must be an odd number',
),
self::MODE_NEGATIVE => array(
self::STANDARD => '{{name}} must not be an odd 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.
*
*/

View file

@ -0,0 +1,50 @@
<?php
namespace Respect\Validation\Rules;
use Respect\Validation\Rules\Int;
class Even extends AbstractRule
{
public function validate($input)
{
$int = new Int();
$int->assert($input);
return ($input % 2 == 0);
}
}
/**
* 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.
*
*/

View file

@ -0,0 +1,50 @@
<?php
namespace Respect\Validation\Rules;
use Respect\Validation\Rules\Int;
class Odd extends AbstractRule
{
public function validate($input)
{
$int = new Int();
$int->assert($input);
return ($input % 2 != 0);
}
}
/**
* 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.
*
*/

View file

@ -0,0 +1,75 @@
<?php
namespace Respect\Validation\Rules;
class EvenTest extends \PHPUnit_Framework_TestCase
{
protected $object;
protected function setUp()
{
$this->object = new Even;
}
/**
* @dataProvider providerForEven
*
*/
public function testEven($input)
{
$this->assertTrue($this->object->assert($input));
}
/**
* @dataProvider providerForNotEven
* @expectedException Respect\Validation\Exceptions\EvenException
*/
public function testNotEven($input)
{
$this->assertTrue($this->object->assert($input));
}
/**
* @dataProvider providerForNotInt
* @expectedException Respect\Validation\Exceptions\IntException
*/
public function testNotInt($input)
{
$this->assertTrue($this->object->assert($input));
}
public function providerForEven()
{
return array(
array(-2),
array(-0),
array(0),
array(32),
);
}
public function providerForNotEven()
{
return array(
array(-5),
array(-1),
array(1),
array(13),
);
}
public function providerForNotInt()
{
return array(
array(null),
array('a'),
array(' '),
array('Foo'),
array(''),
array('1.44'),
array(1e-5),
);
}
}

View file

@ -0,0 +1,75 @@
<?php
namespace Respect\Validation\Rules;
class OddTest extends \PHPUnit_Framework_TestCase
{
protected $object;
protected function setUp()
{
$this->object = new Odd;
}
/**
* @dataProvider providerForOdd
*
*/
public function testOdd($input)
{
$this->assertTrue($this->object->assert($input));
}
/**
* @dataProvider providerForNotOdd
* @expectedException Respect\Validation\Exceptions\OddException
*/
public function testNotOdd($input)
{
$this->assertTrue($this->object->assert($input));
}
/**
* @dataProvider providerForNotInt
* @expectedException Respect\Validation\Exceptions\IntException
*/
public function testNotInt($input)
{
$this->assertTrue($this->object->assert($input));
}
public function providerForOdd()
{
return array(
array(-5),
array(-1),
array(1),
array(13),
);
}
public function providerForNotOdd()
{
return array(
array(-2),
array(-0),
array(0),
array(32),
);
}
public function providerForNotInt()
{
return array(
array(null),
array('a'),
array(' '),
array('Foo'),
array(''),
array('1.44'),
array(1e-5),
);
}
}