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

Commit 5eb44b2

Browse files
committed
Add Enum rule
1 parent 84ecbe6 commit 5eb44b2

File tree

6 files changed

+67
-2
lines changed

6 files changed

+67
-2
lines changed

CHANGES.md

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

3+
1.0.10
4+
------
5+
Enum rule. See
6+
37
1.0.9
48
-----
59
Enable Laravel's package discovery https://laravel.com/docs/5.5/packages#package-discovery

src/GenericRuleSet.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,18 @@
1313
* @method GenericRuleSet unique(string $table, string $column, \Closure $closure = null) Get a unique constraint builder instance.
1414
*
1515
* @method StringRuleSet activeUrl() The field under validation must have a valid A or AAAA record according to the dns_get_record PHP function.
16+
* @method StringRuleSet alpha() The field under validation must be entirely alphabetic characters.
17+
* @method StringRuleSet alphaDash() The field under validation may have alpha-numeric characters, as well as dashes and underscores.
18+
* @method StringRuleSet alphaNum() The field under validation must be entirely alpha-numeric characters.
1619
* @method StringRuleSet email() The field under validation must be formatted as an e-mail address.
1720
* @method StringRuleSet ip() The field under validation must be an IP address.
1821
* @method StringRuleSet ipv4() The field under validation must be an IPv4 address.
1922
* @method StringRuleSet ipv6() The field under validation must be an IPv6 address.
2023
* @method StringRuleSet json() The field under validation must be a valid JSON string.
2124
* @method StringRuleSet timezone() The field under validation must be a valid timezone identifier according to the timezone_identifiers_list PHP function
2225
* @method StringRuleSet url() The field under validation must be a valid URL.
26+
* @method StringRuleSet enum(string $enumClass) Field under validation must match one of values of specified Enum
27+
2328
* @method FileRuleSet mimetypes(string ...$types) The file under validation must match one of the given MIME types. To determine the MIME type of the uploaded file, the file's contents will be read and the framework will attempt to guess the MIME type, which may be different from the client provided MIME type.
2429
* @method FileRuleSet mimes(string ...$extensions) The file under validation must have a MIME type corresponding to one of the listed extensions.
2530
*

src/Rule.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
* @method static StringRuleSet url() The field under validation must be a valid URL.
4949
* @method static StringRuleSet regex(string $pattern, bool $ignoreCase = false) The field under validation must match the given regular expression.
5050
* @method static StringRuleSet phoneRegex() Shortcut method for validating phone with use regex.
51+
* @method static StringRuleSet enum(string $enumClass) Field under validation must match one of values of specified Enum
5152
5253
* @method static FileRuleSet mimetypes(string ...$types) The file under validation must match one of the given MIME types. To determine the MIME type of the uploaded file, the file's contents will be read and the framework will attempt to guess the MIME type, which may be different from the client provided MIME type.
5354
* @method static FileRuleSet mimes(string ...$extensions) The file under validation must have a MIME type corresponding to one of the listed extensions.
@@ -144,7 +145,7 @@ public static function image($constraints = []): ImageRuleSet
144145
*
145146
* @return PhoneRuleSet
146147
*/
147-
public static function phone()
148+
public static function phone(): PhoneRuleSet
148149
{
149150
return new PhoneRuleSet();
150151
}

src/StringRuleSet.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Saritasa\Laravel\Validation;
44

55
use Illuminate\Support\Str;
6+
use Saritasa\Enum;
67

78
/**
89
* Rules, that are reasonable for strings only
@@ -21,7 +22,7 @@
2122
*/
2223
class StringRuleSet extends RuleSet
2324
{
24-
const EXPOSED_RULES = ['email', 'regex', 'timezone', 'phoneRegex'];
25+
const EXPOSED_RULES = ['email', 'regex', 'timezone', 'phoneRegex', 'enum'];
2526

2627
const TRIVIAL_STRING_RULES = [
2728
'activeUrl',
@@ -44,6 +45,7 @@ public function __construct(array $rules = [])
4445

4546
/**
4647
* The field under validation must match the given regular expression.
48+
*
4749
* @param string $pattern
4850
* @param bool $ignoreCase
4951
* @return StringRuleSet
@@ -59,6 +61,21 @@ public function regex(string $pattern, bool $ignoreCase = false): StringRuleSet
5961
return $this->appendIfNotExists("regex:$pattern");
6062
}
6163

64+
/**
65+
* Field under validation must match one of values of specified Enum
66+
* @see https://github.com/Saritasa/php-common#enum
67+
*
68+
* @param string $enumClass Enumeration to validate against
69+
* @return StringRuleSet
70+
*/
71+
public function enum(string $enumClass): StringRuleSet
72+
{
73+
if (!is_a($enumClass, Enum::class, true)) {
74+
throw new \UnexpectedValueException('Class is not enum');
75+
}
76+
return $this->appendIfNotExists(Rule::in($enumClass::getConstants()));
77+
}
78+
6279
/**
6380
* Shortcut method for validating phone with use regex.
6481
* The method uses E.164 format for validation. (ex: +12345678901)

tests/EnumRulesTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
4+
use PHPUnit\Framework\TestCase;
5+
use Saritasa\Laravel\Validation\Rule;
6+
7+
/**
8+
* Check Enum rules
9+
* @see https://github.com/Saritasa/php-common#enum
10+
*/
11+
class EnumRulesTest extends TestCase
12+
{
13+
/**
14+
* Basic usage
15+
*/
16+
public function testEnum()
17+
{
18+
$rules = Rule::enum(\Saritasa\Enums\Gender::class)->toString();
19+
$this->assertEquals('string|in:"Male","Female"', $rules);
20+
}
21+
22+
public function testNonExistingEnum()
23+
{
24+
$this->expectException(\UnexpectedValueException::class);
25+
Rule::enum("bullshit");
26+
}
27+
}

tests/RuleTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Support\Str;
66
use PHPUnit\Framework\TestCase;
77
use Saritasa\Enums\Gender;
8+
use Saritasa\Exceptions\NotImplementedException;
89
use Saritasa\Laravel\Validation\Rule;
910
use Saritasa\Laravel\Validation\RuleSet;
1011

@@ -105,4 +106,14 @@ public function testMixed()
105106
$rules = Rule::requiredWithout('facebook_token')->confirmed();
106107
$this->assertEquals('required_without:facebook_token|confirmed', $rules);
107108
}
109+
110+
111+
/**
112+
* Requesting non-registered rule should throw "Not Implemented" exception
113+
*/
114+
public function testNonExistent()
115+
{
116+
$this->expectException(NotImplementedException::class);
117+
Rule::nonExistent();
118+
}
108119
}

0 commit comments

Comments
 (0)