Skip to content

Commit a32025f

Browse files
Merge pull request #166 from radiate-framework/feature/validation-rules
Feature/validation rules
2 parents b865a0f + 2fbac14 commit a32025f

10 files changed

Lines changed: 314 additions & 27 deletions
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace Radiate\Validation\Rules;
4+
5+
use Radiate\Support\Facades\Auth;
6+
7+
class CurrentPassword implements Rule
8+
{
9+
/**
10+
* The auth guard
11+
*
12+
* @var string|null
13+
*/
14+
protected $guard;
15+
16+
/**
17+
* Create the rule
18+
*
19+
* @param string|null $guard
20+
*/
21+
public function __construct(?string $guard = null)
22+
{
23+
$this->guard = $guard;
24+
}
25+
26+
/**
27+
* Determine if the validation rule passes.
28+
*
29+
* @param string $attribute
30+
* @param mixed $value
31+
* @return bool
32+
*/
33+
public function passes(string $attribute, $value): bool
34+
{
35+
if (!$user = Auth::guard($this->guard)->user()) {
36+
return false;
37+
}
38+
39+
return wp_check_password($value, $user->user_pass, $user->ID);
40+
}
41+
42+
/**
43+
* Get the validation error message.
44+
*
45+
* @return string|array
46+
*/
47+
public function message()
48+
{
49+
return 'The password is incorrect';
50+
}
51+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Radiate\Validation\Rules;
4+
5+
class EmailExists implements Rule
6+
{
7+
/**
8+
* Determine if the validation rule passes.
9+
*
10+
* @param string $attribute
11+
* @param mixed $value
12+
* @return bool
13+
*/
14+
public function passes(string $attribute, $value): bool
15+
{
16+
return email_exists($value) !== false;
17+
}
18+
19+
/**
20+
* Get the validation error message.
21+
*
22+
* @return string|array
23+
*/
24+
public function message()
25+
{
26+
return ':Attribute is invalid';
27+
}
28+
}

src/Validation/Rules/IsUuid.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Radiate\Validation\Rules;
4+
5+
use Radiate\Support\Str;
6+
7+
class IsUuid implements Rule
8+
{
9+
/**
10+
* Determine if the validation rule passes.
11+
*
12+
* @param string $attribute
13+
* @param mixed $value
14+
* @return bool
15+
*/
16+
public function passes(string $attribute, $value): bool
17+
{
18+
return Str::isUuid($value);
19+
}
20+
21+
/**
22+
* Get the validation error message.
23+
*
24+
* @return string|array
25+
*/
26+
public function message()
27+
{
28+
return ':Attribute must be a valid UUID';
29+
}
30+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Radiate\Validation\Rules;
4+
5+
class PostTypeExists implements Rule
6+
{
7+
/**
8+
* Determine if the validation rule passes.
9+
*
10+
* @param string $attribute
11+
* @param mixed $value
12+
* @return bool
13+
*/
14+
public function passes(string $attribute, $value): bool
15+
{
16+
return post_type_exists($value);
17+
}
18+
19+
/**
20+
* Get the validation error message.
21+
*
22+
* @return string|array
23+
*/
24+
public function message()
25+
{
26+
return ':Attribute is invalid';
27+
}
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Radiate\Validation\Rules;
4+
5+
class TaxonomyExists implements Rule
6+
{
7+
/**
8+
* Determine if the validation rule passes.
9+
*
10+
* @param string $attribute
11+
* @param mixed $value
12+
* @return bool
13+
*/
14+
public function passes(string $attribute, $value): bool
15+
{
16+
return taxonomy_exists($value);
17+
}
18+
19+
/**
20+
* Get the validation error message.
21+
*
22+
* @return string|array
23+
*/
24+
public function message()
25+
{
26+
return ':Attribute is invalid';
27+
}
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Radiate\Validation\Rules;
4+
5+
class TermExists implements Rule
6+
{
7+
/**
8+
* Determine if the validation rule passes.
9+
*
10+
* @param string $attribute
11+
* @param mixed $value
12+
* @return bool
13+
*/
14+
public function passes(string $attribute, $value): bool
15+
{
16+
return (bool) term_exists($value);
17+
}
18+
19+
/**
20+
* Get the validation error message.
21+
*
22+
* @return string|array
23+
*/
24+
public function message()
25+
{
26+
return ':Attribute is invalid';
27+
}
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Radiate\Validation\Rules;
4+
5+
class UniqueEmail implements Rule
6+
{
7+
/**
8+
* Determine if the validation rule passes.
9+
*
10+
* @param string $attribute
11+
* @param mixed $value
12+
* @return bool
13+
*/
14+
public function passes(string $attribute, $value): bool
15+
{
16+
return email_exists($value) === false;
17+
}
18+
19+
/**
20+
* Get the validation error message.
21+
*
22+
* @return string|array
23+
*/
24+
public function message()
25+
{
26+
return ':Attribute has already been taken';
27+
}
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Radiate\Validation\Rules;
4+
5+
class UniqueUsername implements Rule
6+
{
7+
/**
8+
* Determine if the validation rule passes.
9+
*
10+
* @param string $attribute
11+
* @param mixed $value
12+
* @return bool
13+
*/
14+
public function passes(string $attribute, $value): bool
15+
{
16+
return username_exists($value) === false;
17+
}
18+
19+
/**
20+
* Get the validation error message.
21+
*
22+
* @return string|array
23+
*/
24+
public function message()
25+
{
26+
return ':Attribute has already been taken';
27+
}
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Radiate\Validation\Rules;
4+
5+
class UsernameExists implements Rule
6+
{
7+
/**
8+
* Determine if the validation rule passes.
9+
*
10+
* @param string $attribute
11+
* @param mixed $value
12+
* @return bool
13+
*/
14+
public function passes(string $attribute, $value): bool
15+
{
16+
return username_exists($value) !== false;
17+
}
18+
19+
/**
20+
* Get the validation error message.
21+
*
22+
* @return string|array
23+
*/
24+
public function message()
25+
{
26+
return ':Attribute is invalid';
27+
}
28+
}

src/Validation/Validator.php

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -56,33 +56,43 @@ class Validator
5656
* @var array
5757
*/
5858
protected $rules = [
59-
'accepted' => Rules\IsAccepted::class,
60-
'alpha' => Rules\IsAlpha::class,
61-
'alpha_dash' => Rules\IsAlphaDash::class,
62-
'alpha_num' => Rules\IsAlphaNum::class,
63-
'array' => Rules\IsArray::class,
64-
'between' => Rules\IsBetween::class,
65-
'boolean' => Rules\IsBoolean::class,
66-
'date' => Rules\IsDate::class,
67-
'digits' => Rules\IsDigits::class,
68-
'digits_between' => Rules\IsDigitsBetween::class,
69-
'email' => Rules\IsEmail::class,
70-
'ends_with' => Rules\EndsWith::class,
71-
'integer' => Rules\IsInteger::class,
72-
'in' => Rules\IsIn::class,
73-
'ip' => Rules\IsIp::class,
74-
'ipv4' => Rules\IsIpv4::class,
75-
'ipv6' => Rules\IsIpv6::class,
76-
'max' => Rules\IsMax::class,
77-
'min' => Rules\IsMin::class,
78-
'numeric' => Rules\IsNumeric::class,
79-
'regex' => Rules\MatchesRegex::class,
80-
'required' => Rules\IsRequired::class,
81-
'starts_with' => Rules\StartsWith::class,
82-
'string' => Rules\IsString::class,
83-
'size' => Rules\IsSize::class,
84-
'timezone' => Rules\IsTimezone::class,
85-
'url' => Rules\IsUrl::class,
59+
'accepted' => Rules\IsAccepted::class,
60+
'alpha' => Rules\IsAlpha::class,
61+
'alpha_dash' => Rules\IsAlphaDash::class,
62+
'alpha_num' => Rules\IsAlphaNum::class,
63+
'array' => Rules\IsArray::class,
64+
'between' => Rules\IsBetween::class,
65+
'boolean' => Rules\IsBoolean::class,
66+
'current_password' => Rules\CurrentPassword::class,
67+
'date' => Rules\IsDate::class,
68+
'digits' => Rules\IsDigits::class,
69+
'digits_between' => Rules\IsDigitsBetween::class,
70+
'email' => Rules\IsEmail::class,
71+
'email_exists' => Rules\EmailExists::class,
72+
'ends_with' => Rules\EndsWith::class,
73+
'integer' => Rules\IsInteger::class,
74+
'in' => Rules\IsIn::class,
75+
'ip' => Rules\IsIp::class,
76+
'ipv4' => Rules\IsIpv4::class,
77+
'ipv6' => Rules\IsIpv6::class,
78+
'max' => Rules\IsMax::class,
79+
'min' => Rules\IsMin::class,
80+
'numeric' => Rules\IsNumeric::class,
81+
'posttype_exists' => Rules\PostTypeExists::class,
82+
'regex' => Rules\MatchesRegex::class,
83+
'required' => Rules\IsRequired::class,
84+
'starts_with' => Rules\StartsWith::class,
85+
'string' => Rules\IsString::class,
86+
'size' => Rules\IsSize::class,
87+
'taxonomy_exists' => Rules\TaxonomyExists::class,
88+
'term_exists' => Rules\TermExists::class,
89+
'timezone' => Rules\IsTimezone::class,
90+
'unique_email' => Rules\UniqueEmail::class,
91+
'unique_username' => Rules\UniqueUsername::class,
92+
'url' => Rules\IsUrl::class,
93+
'username_exists' => Rules\UsernameExists::class,
94+
'uuid' => Rules\IsUuid::class,
95+
8696
];
8797

8898
/**

0 commit comments

Comments
 (0)