mirror of
https://github.com/Respect/Validation.git
synced 2024-06-26 17:20:16 +02:00
Apply contribution guidelines to "Uppercase" rule
Also make sure that "Uppercase" only accepts strings. Co-Authored-By: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
parent
df9ae14100
commit
d1932b2e7f
|
@ -2,10 +2,20 @@
|
|||
|
||||
- `Uppercase()`
|
||||
|
||||
Validates if string characters are uppercase in the input:
|
||||
Validates whether the characters in the input are uppercase.
|
||||
|
||||
```php
|
||||
v::stringType()->uppercase()->validate('W3C'); // true
|
||||
v::uppercase()->validate('W3C'); // true
|
||||
```
|
||||
|
||||
This rule does not validate if the input a numeric value, so `123` and `%` will
|
||||
be valid. Please add more validations to the chain if you want to refine your
|
||||
validation.
|
||||
|
||||
```php
|
||||
v::not(v::numericVal())->uppercase()->validate('42'); // false
|
||||
v::alnum()->uppercase()->validate('#$%!'); // false
|
||||
v::not(v::numericVal())->alnum()->uppercase()->validate('W3C'); // true
|
||||
```
|
||||
|
||||
## Changelog
|
||||
|
@ -17,4 +27,6 @@ Version | Description
|
|||
***
|
||||
See also:
|
||||
|
||||
- [Alnum](Alnum.md)
|
||||
- [Lowercase](Lowercase.md)
|
||||
- [NumericVal](NumericVal.md)
|
||||
|
|
|
@ -13,8 +13,16 @@ declare(strict_types=1);
|
|||
|
||||
namespace Respect\Validation\Exceptions;
|
||||
|
||||
class UppercaseException extends ValidationException
|
||||
/**
|
||||
* @author Danilo Benevides <danilobenevides01@gmail.com>
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
* @author Jean Pimentel <jeanfap@gmail.com>
|
||||
*/
|
||||
final class UppercaseException extends ValidationException
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $defaultTemplates = [
|
||||
self::MODE_DEFAULT => [
|
||||
self::STANDARD => '{{name}} must be uppercase',
|
||||
|
|
|
@ -13,10 +13,29 @@ declare(strict_types=1);
|
|||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
class Uppercase extends AbstractRule
|
||||
use function is_string;
|
||||
use function mb_detect_encoding;
|
||||
use function mb_strtoupper;
|
||||
|
||||
/**
|
||||
* Validates whether the characters in the input are uppercase.
|
||||
*
|
||||
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
|
||||
* @author Danilo Benevides <danilobenevides01@gmail.com>
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
* @author Jean Pimentel <jeanfap@gmail.com>
|
||||
*/
|
||||
final class Uppercase extends AbstractRule
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function validate($input): bool
|
||||
{
|
||||
if (!is_string($input)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $input === mb_strtoupper($input, mb_detect_encoding($input));
|
||||
}
|
||||
}
|
||||
|
|
38
tests/integration/rules/uppercase.phpt
Normal file
38
tests/integration/rules/uppercase.phpt
Normal file
|
@ -0,0 +1,38 @@
|
|||
--FILE--
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\NestedValidationException;
|
||||
use Respect\Validation\Exceptions\UppercaseException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
try {
|
||||
v::uppercase()->check('lowercase');
|
||||
} catch (UppercaseException $exception) {
|
||||
echo $exception->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::uppercase()->assert('lowercase');
|
||||
} catch (NestedValidationException $exception) {
|
||||
echo $exception->getFullMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::uppercase())->check('UPPERCASE');
|
||||
} catch (UppercaseException $exception) {
|
||||
echo $exception->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::uppercase())->assert('UPPERCASE');
|
||||
} catch (NestedValidationException $exception) {
|
||||
echo $exception->getFullMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
?>
|
||||
--EXPECTF--
|
||||
"lowercase" must be uppercase
|
||||
- "lowercase" must be uppercase
|
||||
"UPPERCASE" must not be uppercase
|
||||
- "UPPERCASE" must not be uppercase
|
|
@ -13,58 +13,58 @@ declare(strict_types=1);
|
|||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Respect\Validation\Test\RuleTestCase;
|
||||
use stdClass;
|
||||
|
||||
/**
|
||||
* @group rule
|
||||
* @group rule
|
||||
*
|
||||
* @covers \Respect\Validation\Rules\Uppercase
|
||||
* @covers \Respect\Validation\Exceptions\UppercaseException
|
||||
*
|
||||
* @author Danilo Benevides <danilobenevides01@gmail.com>
|
||||
* @author Gabriel Caruso <carusogabriel34@gmail.com>
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
* @author Jean Pimentel <jeanfap@gmail.com>
|
||||
*/
|
||||
class UppercaseTest extends TestCase
|
||||
final class UppercaseTest extends RuleTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider providerForValidUppercase
|
||||
*/
|
||||
public function testValidUppercaseShouldReturnTrue($input): void
|
||||
/*
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function providerForValidInput(): array
|
||||
{
|
||||
$uppercase = new Uppercase();
|
||||
self::assertTrue($uppercase->validate($input));
|
||||
$uppercase->assert($input);
|
||||
$uppercase->check($input);
|
||||
}
|
||||
$rule = new Uppercase();
|
||||
|
||||
/**
|
||||
* @dataProvider providerForInvalidUppercase
|
||||
* @expectedException \Respect\Validation\Exceptions\UppercaseException
|
||||
*/
|
||||
public function testInvalidUppercaseShouldThrowException($input): void
|
||||
{
|
||||
$lowercase = new Uppercase();
|
||||
self::assertFalse($lowercase->validate($input));
|
||||
$lowercase->assert($input);
|
||||
}
|
||||
|
||||
public function providerForValidUppercase()
|
||||
{
|
||||
return [
|
||||
[''],
|
||||
['UPPERCASE'],
|
||||
['UPPERCASE-WITH-DASHES'],
|
||||
['UPPERCASE WITH SPACES'],
|
||||
['UPPERCASE WITH NUMBERS 123'],
|
||||
['UPPERCASE WITH SPECIALS CHARACTERS LIKE Ã Ç Ê'],
|
||||
['WITH SPECIALS CHARACTERS LIKE # $ % & * +'],
|
||||
['ΤΆΧΙΣΤΗ ΑΛΏΠΗΞ ΒΑΦΉΣ ΨΗΜΈΝΗ ΓΗ, ΔΡΑΣΚΕΛΊΖΕΙ ΥΠΈΡ ΝΩΘΡΟΎ ΚΥΝΌΣ'],
|
||||
[$rule, ''],
|
||||
[$rule, 'UPPERCASE'],
|
||||
[$rule, 'UPPERCASE-WITH-DASHES'],
|
||||
[$rule, 'UPPERCASE WITH SPACES'],
|
||||
[$rule, 'UPPERCASE WITH NUMBERS 123'],
|
||||
[$rule, 'UPPERCASE WITH SPECIALS CHARACTERS LIKE Ã Ç Ê'],
|
||||
[$rule, 'WITH SPECIALS CHARACTERS LIKE # $ % & * +'],
|
||||
[$rule, 'ΤΆΧΙΣΤΗ ΑΛΏΠΗΞ ΒΑΦΉΣ ΨΗΜΈΝΗ ΓΗ, ΔΡΑΣΚΕΛΊΖΕΙ ΥΠΈΡ ΝΩΘΡΟΎ ΚΥΝΌΣ'],
|
||||
// Uppercase should not restrict these
|
||||
[$rule, '42'],
|
||||
[$rule, '!@#$%^'],
|
||||
];
|
||||
}
|
||||
|
||||
public function providerForInvalidUppercase()
|
||||
/*
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function providerForInvalidInput(): array
|
||||
{
|
||||
$rule = new Uppercase();
|
||||
|
||||
return [
|
||||
['lowercase'],
|
||||
['CamelCase'],
|
||||
['First Character Uppercase'],
|
||||
['With Numbers 1 2 3'],
|
||||
[$rule, 42],
|
||||
[$rule, []],
|
||||
[$rule, new stdClass()],
|
||||
[$rule, 'lowercase'],
|
||||
[$rule, 'CamelCase'],
|
||||
[$rule, 'First Character Uppercase'],
|
||||
[$rule, 'With Numbers 1 2 3'],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue