Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit adb888b

Browse files
Added rules
1 parent faf805e commit adb888b

File tree

7 files changed

+261
-0
lines changed

7 files changed

+261
-0
lines changed

‎composer.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
"phpviet/validation": "^1.0",
1919
"illuminate/support": "^5.7"
2020
},
21+
"require-dev": {
22+
"orchestra/testbench": "^3.7"
23+
},
2124
"autoload": {
2225
"psr-4": {
2326
"PHPViet\\Laravel\\Validation\\": "src"
@@ -34,6 +37,11 @@
3437
"extra": {
3538
"branch-alias": {
3639
"dev-master": "1.0-dev"
40+
},
41+
"laravel": {
42+
"providers": [
43+
"PHPViet\\Laravel\\Validation\\ServiceProvider"
44+
]
3745
}
3846
}
3947
}

‎src/Rules/CallableRule.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
/**
3+
* @link https://github.com/phpviet/laravel-validation
4+
*
5+
* @copyright (c) PHP Viet
6+
* @license [MIT](https://opensource.org/licenses/MIT)
7+
*/
8+
9+
namespace PHPViet\Laravel\Validation\Rules;
10+
11+
use Illuminate\Contracts\Validation\Rule;
12+
13+
/**
14+
* @author Vuong Minh <vuongxuongminh@gmail.com>
15+
* @since 1.0.0
16+
*/
17+
abstract class CallableRule implements Rule
18+
{
19+
20+
/**
21+
* Hổ trợ sử dụng rule dưới dạng extension.
22+
*
23+
* @param $attribute
24+
* @param $value
25+
* @param $parameters
26+
* @param $validator
27+
* @return bool
28+
*/
29+
public function __invoke($attribute, $value, $parameters, $validator)
30+
{
31+
return $this->passes($attribute, $value);
32+
}
33+
34+
}

‎src/Rules/IdVN.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
/**
3+
* @link https://github.com/phpviet/laravel-validation
4+
*
5+
* @copyright (c) PHP Viet
6+
* @license [MIT](https://opensource.org/licenses/MIT)
7+
*/
8+
9+
namespace PHPViet\Laravel\Validation\Rules;
10+
11+
use PHPViet\Validation\Validator;
12+
13+
/**
14+
* @author Vuong Minh <vuongxuongminh@gmail.com>
15+
* @since 1.0.0
16+
*/
17+
class IdVN extends CallableRule
18+
{
19+
20+
/**
21+
* @inheritDoc
22+
*/
23+
public function passes($attribute, $value): bool
24+
{
25+
return Validator::idVN()->validate($value);
26+
}
27+
28+
/**
29+
* @inheritDoc
30+
*/
31+
public function message(): string
32+
{
33+
return __('validation.phpviet.id');
34+
}
35+
36+
}

‎src/Rules/IpVN.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
/**
3+
* @link https://github.com/phpviet/laravel-validation
4+
*
5+
* @copyright (c) PHP Viet
6+
* @license [MIT](https://opensource.org/licenses/MIT)
7+
*/
8+
9+
namespace PHPViet\Laravel\Validation\Rules;
10+
11+
use PHPViet\Validation\Validator;
12+
use PHPViet\Validation\Rules\IpVN as BaseIpVN;
13+
14+
/**
15+
* @author Vuong Minh <vuongxuongminh@gmail.com>
16+
* @since 1.0.0
17+
*/
18+
class IpVN extends CallableRule
19+
{
20+
21+
const IPV4 = BaseIpVN::IPV4;
22+
23+
const IPV6 = BaseIpVN::IPV6;
24+
25+
/**
26+
* @var int|null
27+
*/
28+
protected $version;
29+
30+
/**
31+
* IpVN constructor.
32+
*
33+
* @param int|null $version
34+
*/
35+
public function __construct(?int $version = null)
36+
{
37+
$this->version = $version;
38+
}
39+
40+
/**
41+
* @inheritDoc
42+
*/
43+
public function passes($attribute, $value): bool
44+
{
45+
return Validator::ipVN($this->version)->validate($value);
46+
}
47+
48+
/**
49+
* @inheritDoc
50+
*/
51+
public function message(): string
52+
{
53+
switch ($this->version) {
54+
case self::IPV4:
55+
56+
return __('validation.phpviet.ipv4');
57+
case self::IPV6:
58+
59+
return __('validation.phpviet.ipv6');
60+
default:
61+
62+
return __('validation.phpviet.ip');
63+
64+
}
65+
}
66+
67+
}

‎src/Rules/LandLineVN.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
/**
3+
* @link https://github.com/phpviet/laravel-validation
4+
*
5+
* @copyright (c) PHP Viet
6+
* @license [MIT](https://opensource.org/licenses/MIT)
7+
*/
8+
9+
namespace PHPViet\Laravel\Validation\Rules;
10+
11+
use PHPViet\Validation\Validator;
12+
13+
/**
14+
* @author Vuong Minh <vuongxuongminh@gmail.com>
15+
* @since 1.0.0
16+
*/
17+
class LandLineVN extends CallableRule
18+
{
19+
20+
/**
21+
* @inheritDoc
22+
*/
23+
public function passes($attribute, $value): bool
24+
{
25+
return Validator::landLineVN()->validate($value);
26+
}
27+
28+
/**
29+
* @inheritDoc
30+
*/
31+
public function message(): string
32+
{
33+
return __('validation.phpviet.land_line');
34+
}
35+
36+
}

‎src/Rules/MobileVN.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
/**
3+
* @link https://github.com/phpviet/laravel-validation
4+
*
5+
* @copyright (c) PHP Viet
6+
* @license [MIT](https://opensource.org/licenses/MIT)
7+
*/
8+
9+
namespace PHPViet\Laravel\Validation\Rules;
10+
11+
use PHPVIet\Validation\Validator;
12+
13+
/**
14+
* @author Vuong Minh <vuongxuongminh@gmail.com>
15+
* @since 1.0.0
16+
*/
17+
class MobileVN extends CallableRule
18+
{
19+
/**
20+
* @inheritDoc
21+
*/
22+
public function passes($attribute, $value): bool
23+
{
24+
return Validator::mobileVN()->validate($value);
25+
}
26+
27+
/**
28+
* @inheritDoc
29+
*/
30+
public function message(): string
31+
{
32+
return __('validation.phpviet.mobile');
33+
}
34+
35+
}

‎src/ServiceProvider.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
/**
3+
* @link https://github.com/phpviet/laravel-validation
4+
*
5+
* @copyright (c) PHP Viet
6+
* @license [MIT](https://opensource.org/licenses/MIT)
7+
*/
8+
9+
namespace PHPViet\Laravel\Validation;
10+
11+
use PHPViet\Validation\Rules\IdVN;
12+
use PHPViet\Laravel\Validation\Rules\IpVN;
13+
use PHPViet\Laravel\Validation\Rules\MobileVN;
14+
use PHPViet\Laravel\Validation\Rules\LandLineVN;
15+
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
16+
17+
/**
18+
* @author Vuong Minh <vuongxuongminh@gmail.com>
19+
* @since 1.0.0
20+
*/
21+
class ServiceProvider extends BaseServiceProvider
22+
{
23+
24+
public function boot(): void
25+
{
26+
if (isset($this->app['validator'])) {
27+
28+
foreach ($this->getCallableRules() as $name => $rule) {
29+
$this->app['validator']->extend($name, $rule, $rule->message());
30+
}
31+
}
32+
}
33+
34+
protected function getCallableRules(): array
35+
{
36+
return [
37+
'land_line_vn' => new LandLineVN(),
38+
'mobile_vn' => new MobileVN(),
39+
'id_vn' => new IdVN(),
40+
'ip_vn' => new IpVN(),
41+
'ipv4_vn' => new IpVN(IpVN::IPV4),
42+
'ipv6_vn' => new IpVN(IpVN::IPV6),
43+
];
44+
}
45+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /