diff --git a/.gitattributes b/.gitattributes index bb6265e..a9b4214 100644 --- a/.gitattributes +++ b/.gitattributes @@ -7,5 +7,6 @@ /.travis.yml export-ignore /phpunit.xml.dist export-ignore /.scrutinizer.yml export-ignore +/.styleci.yml export-ignore /tests export-ignore /.editorconfig export-ignore diff --git a/.travis.yml b/.travis.yml index 6873b3d..46e0697 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,6 +4,7 @@ php: - 7.1 - 7.2 - 7.3 + - 7.4 install: - composer self-update @@ -14,8 +15,4 @@ before_script: - travis_retry composer install --no-interaction --prefer-source --dev script: - - phpunit --coverage-text --coverage-clover=coverage.clover - -after_script: - - wget https://scrutinizer-ci.com/ocular.phar - - php ocular.phar code-coverage:upload --format=php-clover coverage.clover + - vendor/bin/phpunit --testdox --coverage-text --coverage-clover=coverage.clover diff --git a/CHANGELOG.md b/CHANGELOG.md index 4457516..cd05476 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,23 @@ # Changelog -Tất cả lịch sử tiến trình phát triển thư viện \ No newline at end of file +Tất cả lịch sử tiến trình phát triển thư viện + +# 1.0.5 + +- Hổ trợ Laravel 6 & 7. + +# 1.0.4 + +- Sửa lỗi DI khi khởi tạo ip rules. + +# 1.0.3 + +- Sửa lỗi lazy load service provider đè lên thư viện. + +# 1.0.2 + +- Cải thiện performance thông qua lazy load service provider. + +# 1.0.1 + +- Hổ trợ đa ngôn ngữ mặc định. diff --git a/README.md b/README.md index f043bd6..efcf410 100644 --- a/README.md +++ b/README.md @@ -1,18 +1,22 @@

- +

Laravel Validation


+

+ Latest version + Build status + Quantity score + StyleCI + Total download + License +

-Laravel validation hổ trợ kiểm tra các kiểu dữ liệu đặc thù trong nước ta. +## Thông tin -[![Latest Version on Packagist](https://img.shields.io/packagist/v/phpviet/laravel-validation.svg?style=flat-square)](https://packagist.org/packages/phpviet/laravel-validation) -[![Build Status](https://img.shields.io/travis/phpviet/laravel-validation/master.svg?style=flat-square)](https://travis-ci.org/phpviet/laravel-validation) -[![Quality Score](https://img.shields.io/scrutinizer/g/phpviet/laravel-validation.svg?style=flat-square)](https://scrutinizer-ci.com/g/phpviet/laravel-validation) -[![StyleCI](https://styleci.io/repos/187064051/shield?branch=master)](https://styleci.io/repos/187064051) -[![Total Downloads](https://img.shields.io/packagist/dt/phpviet/laravel-validation.svg?style=flat-square)](https://packagist.org/packages/phpviet/laravel-validation) +Laravel validation hổ trợ kiểm tra các kiểu dữ liệu đặc thù trong nước ta. ## Cài đặt @@ -166,6 +170,16 @@ public function rules() } ``` +## Ngôn ngữ + +Nếu như bạn muốn thay đổi các error message thì hãy publish resource đính kèm thông qua câu lệnh: + +```php +php artisan vendor:publish +``` + +Sau khi publish xong bạn hãy vào `resources/lang/vendor/phpVietValidation` để thao tác chỉnh sửa theo ý bạn. + ## Dành cho nhà phát triển Nếu như bạn cảm thấy các kiểu kiểm tra dữ liệu bên trên vẫn chưa đủ đối với thị trường diff --git a/composer.json b/composer.json index a3ad6c8..9d150a6 100644 --- a/composer.json +++ b/composer.json @@ -16,10 +16,10 @@ "require": { "php": "^7.1", "phpviet/validation": "^1.0", - "illuminate/support": "^5.7" + "illuminate/support": "^5.7 || ^6.0 || ^7.0" }, "require-dev": { - "orchestra/testbench": "^3.7" + "orchestra/testbench": "^3.7 || ^4.0 || ^5.0" }, "autoload": { "psr-4": { diff --git a/resources/lang/en/validation.php b/resources/lang/en/validation.php new file mode 100644 index 0000000..465f0d4 --- /dev/null +++ b/resources/lang/en/validation.php @@ -0,0 +1,18 @@ + ':attribute must be an id number of Vietnam.', + 'mobile' => ':attribute must be a mobile phone number of Vietnam.', + 'land_line' => ':attribute must be a land line phone number of Vietnam.', + 'ip' => [ + 'v4' => ':attribute must be Vietnam ipv4.', + 'v6' => ':attribute must be Vietnam ipv6.', + 'default' => ':attribute must be Vietnam ip.', + ], +]; diff --git a/resources/lang/vi/validation.php b/resources/lang/vi/validation.php new file mode 100644 index 0000000..0d70a3a --- /dev/null +++ b/resources/lang/vi/validation.php @@ -0,0 +1,18 @@ + ':attribute phải là số chứng minh thư hoặc thẻ căn cước tại Việt Nam.', + 'mobile' => ':attribute phải là số di động tại Việt Nam.', + 'land_line' => ':attribute phải là số điện thoại bàn tại Việt Nam.', + 'ip' => [ + 'v4' => ':attribute phải là ipv4 tại Việt Nam.', + 'v6' => ':attribute phải là ipv6 tại Việt Nam.', + 'default' => ':attribute phải là ip Việt Nam.', + ], +]; diff --git a/src/Rules/IdVN.php b/src/Rules/IdVN.php index a8e6a79..d107150 100644 --- a/src/Rules/IdVN.php +++ b/src/Rules/IdVN.php @@ -29,6 +29,6 @@ public function passes($attribute, $value): bool */ public function message(): string { - return __('validation.phpviet.id'); + return __('phpVietValidation::validation.id'); } } diff --git a/src/Rules/IpVN.php b/src/Rules/IpVN.php index 46d5bde..c26db79 100644 --- a/src/Rules/IpVN.php +++ b/src/Rules/IpVN.php @@ -8,8 +8,8 @@ namespace PHPViet\Laravel\Validation\Rules; -use PHPViet\Validation\Validator; use PHPViet\Validation\Rules\IpVN as BaseIpVN; +use PHPViet\Validation\Validator; /** * @author Vuong Minh @@ -52,13 +52,13 @@ public function message(): string switch ($this->version) { case self::IPV4: - return __('validation.phpviet.ipv4'); + return __('phpVietValidation::validation.ip.v4'); case self::IPV6: - return __('validation.phpviet.ipv6'); + return __('phpVietValidation::validation.ip.v6'); default: - return __('validation.phpviet.ip'); + return __('phpVietValidation::validation.ip.default'); } } diff --git a/src/Rules/LandLineVN.php b/src/Rules/LandLineVN.php index 67152cc..604f604 100644 --- a/src/Rules/LandLineVN.php +++ b/src/Rules/LandLineVN.php @@ -29,6 +29,6 @@ public function passes($attribute, $value): bool */ public function message(): string { - return __('validation.phpviet.land_line'); + return __('phpVietValidation::validation.land_line'); } } diff --git a/src/Rules/MobileVN.php b/src/Rules/MobileVN.php index 2a5ad9e..a1e2297 100644 --- a/src/Rules/MobileVN.php +++ b/src/Rules/MobileVN.php @@ -29,6 +29,6 @@ public function passes($attribute, $value): bool */ public function message(): string { - return __('validation.phpviet.mobile'); + return __('phpVietValidation::validation.mobile'); } } diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index 3c947bd..43a2537 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -8,11 +8,11 @@ namespace PHPViet\Laravel\Validation; +use Illuminate\Support\ServiceProvider as BaseServiceProvider; use PHPViet\Laravel\Validation\Rules\IdVN; use PHPViet\Laravel\Validation\Rules\IpVN; -use PHPViet\Laravel\Validation\Rules\MobileVN; use PHPViet\Laravel\Validation\Rules\LandLineVN; -use Illuminate\Support\ServiceProvider as BaseServiceProvider; +use PHPViet\Laravel\Validation\Rules\MobileVN; /** * @author Vuong Minh @@ -21,6 +21,20 @@ class ServiceProvider extends BaseServiceProvider { public function boot(): void + { + $this->loadTrans(); + $this->loadExt(); + } + + protected function loadTrans(): void + { + $this->publishes([ + __DIR__.'/../resources/lang' => resource_path('lang/vendor/phpVietValidation'), + ]); + $this->loadTranslationsFrom(__DIR__.'/../resources/lang/', 'phpVietValidation'); + } + + protected function loadExt(): void { if (isset($this->app['validator'])) { foreach ($this->getCallableRules() as $name => $rule) { @@ -36,8 +50,8 @@ protected function getCallableRules(): array 'mobile_vn' => $this->app->make(MobileVN::class), 'id_vn' => $this->app->make(IdVN::class), 'ip_vn' => $this->app->make(IpVN::class), - 'ipv4_vn' => $this->app->make(IpVN::class, [IpVN::IPV4]), - 'ipv6_vn' => $this->app->make(IpVN::class, [IpVN::IPV6]), + 'ipv4_vn' => $this->app->make(IpVN::class, ['version' => IpVN::IPV4]), + 'ipv6_vn' => $this->app->make(IpVN::class, ['version' => IpVN::IPV6]), ]; } } diff --git a/tests/IdVNTest.php b/tests/Rules/IdVNTest.php similarity index 79% rename from tests/IdVNTest.php rename to tests/Rules/IdVNTest.php index a120b1a..d4a1839 100644 --- a/tests/IdVNTest.php +++ b/tests/Rules/IdVNTest.php @@ -6,10 +6,11 @@ * @license [MIT](https://opensource.org/licenses/MIT) */ -namespace PHPViet\Laravel\Validation\Tests; +namespace PHPViet\Laravel\Validation\Tests\Rules; use Illuminate\Support\Facades\Lang; use PHPViet\Laravel\Validation\Rules\IdVN; +use PHPViet\Laravel\Validation\Tests\TestCase; /** * @author Vuong Minh @@ -17,7 +18,7 @@ */ class IdVNTest extends TestCase { - public function testValidId() + public function testValid() { $rule = new IdVN(); $id = '025479661'; @@ -32,7 +33,7 @@ public function testValidId() $this->assertEquals($id, array_shift($validId)); } - public function testInvalidIp() + public function testInvalid() { $rule = new IdVN(); $id = '025479661123123123123'; @@ -48,11 +49,9 @@ public function testInvalidIp() public function testCanTranslateErrorMessage() { - Lang::addLines([ - 'validation.phpviet.id' => 'id', - ], Lang::getLocale()); + Lang::setLocale('vi'); $rule = new IdVN(); $rule->passes('attribute', '025479661123123123123!!!'); - $this->assertEquals('id', $rule->message()); + $this->assertEquals(':attribute phải là số chứng minh thư hoặc thẻ căn cước tại Việt Nam.', $rule->message()); } } diff --git a/tests/IpVNTest.php b/tests/Rules/IpVNTest.php similarity index 88% rename from tests/IpVNTest.php rename to tests/Rules/IpVNTest.php index 328a2af..6d5f7db 100644 --- a/tests/IpVNTest.php +++ b/tests/Rules/IpVNTest.php @@ -6,10 +6,11 @@ * @license [MIT](https://opensource.org/licenses/MIT) */ -namespace PHPViet\Laravel\Validation\Tests; +namespace PHPViet\Laravel\Validation\Tests\Rules; use Illuminate\Support\Facades\Lang; use PHPViet\Laravel\Validation\Rules\IpVN; +use PHPViet\Laravel\Validation\Tests\TestCase; /** * @author Vuong Minh @@ -17,7 +18,7 @@ */ class IpVNTest extends TestCase { - public function testValidCases() + public function testValid() { $rule = new IpVN(); $ruleV4 = new IpVN(IpVN::IPV4); @@ -45,7 +46,7 @@ public function testValidCases() $this->assertEquals($ipv6, $validIps['ipv6']); } - public function testInvalidCases() + public function testInvalid() { $rule = new IpVN(); $ip = '113.173.134.203@'; @@ -61,11 +62,9 @@ public function testInvalidCases() public function testCanTranslateErrorMessage() { - Lang::addLines([ - 'validation.phpviet.ip' => 'ip', - ], Lang::getLocale()); + Lang::setLocale('vi'); $rule = new IpVN(); $rule->passes('attribute', '113.173.134.203@'); - $this->assertEquals('ip', $rule->message()); + $this->assertEquals(':attribute phải là ip Việt Nam.', $rule->message()); } } diff --git a/tests/LandLineVNTest.php b/tests/Rules/LandLineVNTest.php similarity index 81% rename from tests/LandLineVNTest.php rename to tests/Rules/LandLineVNTest.php index 325fc47..0aa7077 100644 --- a/tests/LandLineVNTest.php +++ b/tests/Rules/LandLineVNTest.php @@ -6,10 +6,11 @@ * @license [MIT](https://opensource.org/licenses/MIT) */ -namespace PHPViet\Laravel\Validation\Tests; +namespace PHPViet\Laravel\Validation\Tests\Rules; use Illuminate\Support\Facades\Lang; use PHPViet\Laravel\Validation\Rules\LandLineVN; +use PHPViet\Laravel\Validation\Tests\TestCase; /** * @author Vuong Minh @@ -17,7 +18,7 @@ */ class LandLineVNTest extends TestCase { - public function testValidId() + public function testValid() { $rule = new LandLineVN(); $landLine = '02838574955'; @@ -32,7 +33,7 @@ public function testValidId() $this->assertEquals($landLine, array_shift($validLandLine)); } - public function testInvalidIp() + public function testInvalid() { $rule = new LandLineVN(); $landLine = '02838574955!@#'; @@ -48,11 +49,9 @@ public function testInvalidIp() public function testCanTranslateErrorMessage() { - Lang::addLines([ - 'validation.phpviet.land_line' => 'land_line', - ], Lang::getLocale()); + Lang::setLocale('vi'); $rule = new LandLineVN(); $rule->passes('attribute', '02838574955!!!'); - $this->assertEquals('land_line', $rule->message()); + $this->assertEquals(':attribute phải là số điện thoại bàn tại Việt Nam.', $rule->message()); } } diff --git a/tests/MobileVNTest.php b/tests/Rules/MobileVNTest.php similarity index 81% rename from tests/MobileVNTest.php rename to tests/Rules/MobileVNTest.php index 771b38f..e1d8733 100644 --- a/tests/MobileVNTest.php +++ b/tests/Rules/MobileVNTest.php @@ -6,10 +6,11 @@ * @license [MIT](https://opensource.org/licenses/MIT) */ -namespace PHPViet\Laravel\Validation\Tests; +namespace PHPViet\Laravel\Validation\Tests\Rules; use Illuminate\Support\Facades\Lang; use PHPViet\Laravel\Validation\Rules\MobileVN; +use PHPViet\Laravel\Validation\Tests\TestCase; /** * @author Vuong Minh @@ -17,7 +18,7 @@ */ class MobileVNTest extends TestCase { - public function testValidId() + public function testValid() { $rule = new MobileVN(); $mobile = '0909113911'; @@ -32,7 +33,7 @@ public function testValidId() $this->assertEquals($mobile, array_shift($validMobile)); } - public function testInvalidIp() + public function testInvalid() { $rule = new MobileVN(); $mobile = '0909113911!@#'; @@ -48,11 +49,9 @@ public function testInvalidIp() public function testCanTranslateErrorMessage() { - Lang::addLines([ - 'validation.phpviet.mobile' => 'mobile', - ], Lang::getLocale()); + Lang::setLocale('vi'); $rule = new MobileVN(); $rule->passes('attribute', '0909113911!!!'); - $this->assertEquals('mobile', $rule->message()); + $this->assertEquals(':attribute phải là số di động tại Việt Nam.', $rule->message()); } }