Skip to content
This repository was archived by the owner on Sep 13, 2024. It is now read-only.

Commit 838d440

Browse files
committed
Add string validation rules containsUppercase, containsLowercase, containsNumeral
1 parent d33986a commit 838d440

File tree

10 files changed

+204
-9
lines changed

10 files changed

+204
-9
lines changed

.github/workflows/phpunit.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ jobs:
77
runs-on: ubuntu-latest
88
strategy:
99
matrix:
10-
php-versions: [ '7.1', '7.2', '7.3', '7.4' ]
10+
php-versions: [ '7.1', '7.2', '7.3', '7.4', '8.0', '8.1' ]
1111
name: PHP ${{ matrix.php-versions }} - PHPUnit
1212
steps:
1313
- name: Checkout
@@ -18,7 +18,7 @@ jobs:
1818
with:
1919
php-version: ${{ matrix.php-versions }}
2020
coverage: xdebug
21-
21+
2222
- name: Check PHP Version
2323
run: php -v
2424

@@ -30,11 +30,11 @@ jobs:
3030

3131
- name: Run test suite
3232
run: vendor/bin/phpunit --coverage-clover=coverage.xml
33-
33+
3434
- name: Upload Codecov
3535
uses: codecov/codecov-action@v1
3636
with:
3737
file: ./coverage.xml
3838
flags: unittests
3939
fail_ci_if_error: false
40-
verbose: true
40+
verbose: true

CHANGES.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changes History
22

3+
1.2.0
4+
_____
5+
+ Add String validation rules 'containsUppercase', 'containsLowercase', 'containsNumeral' - useful for password validation
6+
* Declared compatibility with Laravel 7,8,9
7+
38
1.1.0
49
-----
510
Compatibility with Laravel 6

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
],
1313
"require": {
1414
"php": ">=7.0",
15-
"illuminate/database": "^5.4 || ^6.0",
16-
"illuminate/support": "^5.4 || ^6.0",
17-
"illuminate/validation": "^5.4 || ^6.0",
15+
"illuminate/database": "^5.4 || ^6.0 || ^7.0 || ^8.0 || ^9.0",
16+
"illuminate/support": "^5.4 || ^6.0 || ^7.0 || ^8.0 || ^9.0",
17+
"illuminate/validation": "^5.4 || ^6.0 || ^7.0 || ^8.0 || ^9.0",
1818
"saritasa/php-common": "^1.0",
1919
"propaganistas/laravel-phone": "^3.0 || ^4.0 || ^5.0"
2020
},

src/Rules/ContainsLowercase.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Saritasa\Laravel\Validation\Rules;
4+
5+
use Illuminate\Contracts\Validation\Rule;
6+
7+
/**
8+
* The rule to check for a lowercase letter in a string.
9+
*/
10+
class ContainsLowercase implements Rule
11+
{
12+
const REGEX = '/([\p{Ll}]+)/u';
13+
14+
/**
15+
* Determine if the validation rule passes.
16+
*
17+
* @param mixed $attribute The name of the attribute
18+
* @param mixed $value The value of the attribute
19+
*
20+
* @return boolean
21+
*/
22+
public function passes($attribute, $value): bool
23+
{
24+
return $attribute && preg_match(self::REGEX, $value);
25+
}
26+
27+
/**
28+
* Get the validation error message.
29+
*
30+
* @return string
31+
*/
32+
public function message(): string
33+
{
34+
return 'At least one lowercase letter';
35+
}
36+
}

src/Rules/ContainsNumeral.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Saritasa\Laravel\Validation\Rules;
4+
5+
use Illuminate\Contracts\Validation\Rule;
6+
7+
/**
8+
* The rule to check for a digit in a string.
9+
*/
10+
class ContainsNumeral implements Rule
11+
{
12+
const REGEX = '/\d/';
13+
14+
/**
15+
* Determine if the validation rule passes.
16+
*
17+
* @param mixed $attribute The name of the attribute
18+
* @param mixed $value The value of the attribute
19+
*
20+
* @return boolean
21+
*/
22+
public function passes($attribute, $value): bool
23+
{
24+
return $attribute && preg_match(self::REGEX, $value);
25+
}
26+
27+
/**
28+
* Get the validation error message.
29+
*
30+
* @return string
31+
*/
32+
public function message(): string
33+
{
34+
return 'At least one numeral';
35+
}
36+
}

src/Rules/ContainsUppercase.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Saritasa\Laravel\Validation\Rules;
4+
5+
use Illuminate\Contracts\Validation\Rule;
6+
7+
/**
8+
* The rule to check for a uppercase letter in a string.
9+
*/
10+
class ContainsUppercase implements Rule
11+
{
12+
const REGEX = '/([\p{Lu}]+)/u';
13+
14+
/**
15+
* Determine if the validation rule passes.
16+
*
17+
* @param mixed $attribute The name of the attribute
18+
* @param mixed $value The value of the attribute
19+
*
20+
* @return boolean
21+
*/
22+
public function passes($attribute, $value): bool
23+
{
24+
return $attribute && preg_match(self::REGEX, $value);
25+
}
26+
27+
/**
28+
* Get the validation error message.
29+
*
30+
* @return string
31+
*/
32+
public function message(): string
33+
{
34+
return 'At least one uppercase letter';
35+
}
36+
}

src/Rules/EnumName.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Rules;
4+
5+
use Illuminate\Contracts\Validation\Rule;
6+
use Saritasa\Laravel\Validation\IRule;
7+
8+
class EnumName implements Rule, IRule
9+
{
10+
public function __toString(): string
11+
{
12+
// TODO: Implement __toString() method.
13+
}
14+
15+
public function passes($attribute, $value)
16+
{
17+
// TODO: Implement passes() method.
18+
}
19+
20+
public function message()
21+
{
22+
// TODO: Implement message() method.
23+
}
24+
}

src/StringRuleSet.php

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
use Illuminate\Support\Str;
66
use Saritasa\Enum;
7+
use Saritasa\Laravel\Validation\Rules\ContainsLowercase;
8+
use Saritasa\Laravel\Validation\Rules\ContainsNumeral;
9+
use Saritasa\Laravel\Validation\Rules\ContainsUppercase;
710

811
/**
912
* Rules, that are reasonable for strings only
@@ -22,7 +25,7 @@
2225
*/
2326
class StringRuleSet extends RuleSet
2427
{
25-
const EXPOSED_RULES = ['email', 'regex', 'timezone', 'phoneRegex', 'enum'];
28+
const EXPOSED_RULES = ['email', 'regex', 'timezone', 'phoneRegex', 'enum', ];
2629

2730
const TRIVIAL_STRING_RULES = [
2831
'activeUrl',
@@ -79,6 +82,44 @@ public function enum(string $enumClass): StringRuleSet
7982
return $this->appendIfNotExists(Rule::in($enumClass::getConstants()));
8083
}
8184

85+
public function enumName(string $enumClass): StringRuleSet
86+
{
87+
if (!is_a($enumClass, Enum::class, true)) {
88+
throw new \UnexpectedValueException('Class is not enum');
89+
}
90+
return $this->appendIfNotExists(Rule::enumName(array_keys($enumClass::getConstants())));
91+
}
92+
93+
/**
94+
* The field under validation must contain lowercase letters
95+
*
96+
* @return StringRuleSet
97+
*/
98+
public function containsLowercase(): StringRuleSet
99+
{
100+
return $this->regex(ContainsLowercase::REGEX);
101+
}
102+
103+
/**
104+
* The field under validation must contain uppercase letters
105+
*
106+
* @return StringRuleSet
107+
*/
108+
public function containsUppercase(): StringRuleSet
109+
{
110+
return $this->regex(ContainsUppercase::REGEX);
111+
}
112+
113+
/**
114+
* The field under validation must contain numbers
115+
*
116+
* @return StringRuleSet
117+
*/
118+
public function containsNumeral(): StringRuleSet
119+
{
120+
return $this->regex(ContainsNumeral::REGEX);
121+
}
122+
82123
/**
83124
* Shortcut method for validating phone with use regex.
84125
* The method uses E.164 format for validation. (ex: +12345678901)

tests/EnumRulesTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php
22

3-
43
use PHPUnit\Framework\TestCase;
54
use Saritasa\Laravel\Validation\Rule;
65

tests/StringRulesTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,24 @@ public function testRegex()
2424
$this->assertEquals('string|regex:/^\w+[\w-\.]*\w+$/', $rules);
2525
}
2626

27+
public function testContainsLowercase()
28+
{
29+
$rules = Rule::string()->containsUppercase();
30+
$this->assertEquals('string|regex:/([\p{Lu}]+)/u', $rules);
31+
}
32+
33+
public function testContainsUppercase()
34+
{
35+
$rules = Rule::string()->containsUppercase();
36+
$this->assertEquals('string|regex:/([\p{Lu}]+)/u', $rules);
37+
}
38+
39+
public function testContainsNumeral()
40+
{
41+
$rules = Rule::string()->containsNumeral();
42+
$this->assertEquals('string|regex:/\d/', $rules);
43+
}
44+
2745
public function testPhoneRegex()
2846
{
2947
$rules = Rule::string()->phoneRegex();

0 commit comments

Comments
 (0)