mirror of
https://github.com/Respect/Validation.git
synced 2024-06-15 20:15:06 +02:00
Added Lowercase Validator
This commit is contained in:
parent
cf1fc85caa
commit
ce2584c18a
50
library/Respect/Validation/Exceptions/LowercaseException.php
Normal file
50
library/Respect/Validation/Exceptions/LowercaseException.php
Normal file
|
@ -0,0 +1,50 @@
|
|||
<?php
|
||||
|
||||
namespace Respect\Validation\Exceptions;
|
||||
|
||||
class LowercaseException extends ValidationException
|
||||
{
|
||||
|
||||
public static $defaultTemplates = array(
|
||||
self::MODE_DEFAULT => array(
|
||||
self::STANDARD => '{{name}} must be lowercase',
|
||||
),
|
||||
self::MODE_NEGATIVE => array(
|
||||
self::STANDARD => '{{name}} must not be lowercase',
|
||||
)
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
*/
|
13
library/Respect/Validation/Rules/Lowercase.php
Normal file
13
library/Respect/Validation/Rules/Lowercase.php
Normal file
|
@ -0,0 +1,13 @@
|
|||
<?php
|
||||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
class Lowercase extends AbstractRule
|
||||
{
|
||||
|
||||
public function validate($input)
|
||||
{
|
||||
return $input === strtolower($input);
|
||||
}
|
||||
|
||||
}
|
52
tests/library/Respect/Validation/Rules/LowercaseTest.php
Normal file
52
tests/library/Respect/Validation/Rules/LowercaseTest.php
Normal file
|
@ -0,0 +1,52 @@
|
|||
<?php
|
||||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
class LowercaseTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
/**
|
||||
* @dataProvider providerForValidLowercase
|
||||
*/
|
||||
public function test_valid_lowercase_should_return_true($input)
|
||||
{
|
||||
$lowercase = new Lowercase();
|
||||
$this->assertTrue($lowercase->validate($input));
|
||||
$this->assertTrue($lowercase->assert($input));
|
||||
$this->assertTrue($lowercase->check($input));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForInvalidLowercase
|
||||
* @expectedException Respect\Validation\Exceptions\LowercaseException
|
||||
*/
|
||||
public function test_invalid_lowercase_should_throw_exception($input)
|
||||
{
|
||||
$lowercase = new Lowercase();
|
||||
$this->assertFalse($lowercase->validate($input));
|
||||
$this->assertFalse($lowercase->assert($input));
|
||||
}
|
||||
|
||||
public function providerForValidLowercase()
|
||||
{
|
||||
return array(
|
||||
array('lowercase'),
|
||||
array('lowercase-with-dashes'),
|
||||
array('lowercase with spaces'),
|
||||
array('lowercase with numbers 123'),
|
||||
array('lowercase with specials characters like ã ç ê'),
|
||||
array('with specials characters like # $ % & * +'),
|
||||
);
|
||||
}
|
||||
|
||||
public function providerForInvalidLowercase()
|
||||
{
|
||||
return array(
|
||||
array('UPPERCASE'),
|
||||
array('CamelCase'),
|
||||
array('First Character Uppercase'),
|
||||
array('With Numbers 1 2 3'),
|
||||
);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue