Skip to content

Commit 02662c8

Browse files
pixelpeterDDEV User
andauthored
Refactor for less boilerplate and better structure
* chore: add PHPStan * refactor: complete refactoring of the integration. No API changes * fix: larastan version vor php 8.1 * fix: phpstan version for larastan 2.x * fix: replace fluent() with data_get() --------- Co-authored-by: DDEV User <[email protected]>
1 parent 345312f commit 02662c8

22 files changed

+366
-964
lines changed

README.md

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,17 @@
99
[![PHPStan](https://github.com/pixelpeter/laravel-isocodes-validation/actions/workflows/phpstan.yml/badge.svg)](https://github.com/pixelpeter/laravel-isocodes-validation/actions/workflows/phpstan.yml)
1010
[![dependabot-auto-merge](https://github.com/pixelpeter/laravel-isocodes-validation/actions/workflows/dependabot-auto-merge.yml/badge.svg)](https://github.com/pixelpeter/laravel-isocodes-validation/actions/workflows/dependabot-auto-merge.yml)
1111

12-
A simple Laravel 8+ wrapper for the [IsoCodes Validation library](https://github.com/ronanguilloux/IsoCodes) from
12+
A simple Laravel 10+ wrapper for the [IsoCodes Validation library](https://github.com/ronanguilloux/IsoCodes) from
1313
ronanguilloux.
1414

1515
## Installation
1616

17-
### Step 1: Install Through Composer
17+
### Install With Composer
1818

1919
``` bash
2020
composer require pixelpeter/laravel-isocodes-validation
2121
```
2222

23-
### Step 2: Add the Service Provider
24-
25-
*(not needed starting with v2.x because of auto discovery)*
26-
27-
Add the service provider in `app/config/app.php`
28-
29-
```php
30-
'provider' => [
31-
...
32-
Pixelpeter\IsoCodesValidation\IsoCodesValidationServiceProvider::class,
33-
...
34-
];
35-
```
36-
3723
## Usage
3824

3925
### Simple examples

composer.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,11 @@
3333
}
3434
},
3535
"require-dev": {
36+
"larastan/larastan": "^2.11|^3.7",
3637
"mockery/mockery": "^1.2",
3738
"orchestra/testbench": "^8.0|^9.5|^10.0",
3839
"phpstan/extension-installer": "^1.4",
40+
"phpstan/phpstan": "^1.12|^2.1",
3941
"phpunit/phpunit": "^10.5|^11.0|^12.0"
4042
},
4143
"extra": {
@@ -49,6 +51,8 @@
4951
"allow-plugins": {
5052
"phpstan/extension-installer": true
5153
},
52-
"sort-packages": true
54+
"sort-packages": true,
55+
"platform": {
56+
}
5357
}
5458
}

lang/de/validation.php

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

3+
declare(strict_types=1);
4+
35
return [
46
'bban' => 'Der Wert ":value" von :attribute ist kein gültiger BBAN Code.',
57
'bsn' => 'Der Wert ":value" von :attribute ist kein gültiger BSN.',

lang/en/validation.php

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

3+
declare(strict_types=1);
4+
35
return [
46
'bban' => 'The value ":value" of :attribute is not a valid BBAN code.',
57
'bsn' => 'The value ":value" of :attribute is not a valid BSN.',

lang/es/validation.php

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

3+
declare(strict_types=1);
4+
35
return [
46
'bban' => 'El valor ":value" del campo :attribute no es un código BBAN válido.',
57
'bsn' => 'El valor ":value" del campo :attribute no es un BSN válido.',

phpstan.neon.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ includes:
22
- phpstan-baseline.neon
33

44
parameters:
5-
level: 5
5+
level: 8
66
paths:
77
- src
88
tmpDir: build/phpstan

phpunit.xml

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" colors="true"
3-
processIsolation="false" stopOnFailure="true"
4-
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" cacheDirectory=".phpunit.cache"
5-
backupStaticProperties="false">
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
backupGlobals="false"
4+
colors="true"
5+
processIsolation="false"
6+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
7+
cacheDirectory=".phpunit.cache"
8+
backupStaticProperties="false"
9+
stopOnFailure="true"
10+
stopOnError="true"
11+
12+
>
613
<testsuites>
714
<testsuite name="Unit">
815
<directory suffix=".php">./tests/Unit</directory>

pint.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"preset": "laravel",
3+
"rules": {
4+
"declare_strict_types": true,
5+
"final_class": true
6+
}
7+
}
Lines changed: 26 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,50 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Pixelpeter\IsoCodesValidation;
46

57
use Illuminate\Support\ServiceProvider;
6-
use Illuminate\Support\Str;
8+
use Pixelpeter\IsoCodesValidation\Support\ReferenceResolver;
9+
use Pixelpeter\IsoCodesValidation\Support\Registry;
10+
use Pixelpeter\IsoCodesValidation\Support\Replacer;
711

8-
class IsoCodesValidationServiceProvider extends ServiceProvider
12+
final class IsoCodesValidationServiceProvider extends ServiceProvider
913
{
10-
/**
11-
* Bootstrap the application services.
12-
*
13-
* @return void
14-
*/
15-
public function boot()
14+
public function boot(): void
1615
{
17-
// load translation files
1816
$this->loadTranslationsFrom(
1917
__DIR__.'/../lang',
2018
'validation'
2119
);
2220

23-
// registering intervention validator extension
24-
$this->app['validator']->resolver(function ($translator, $data, $rules, $messages, $customAttributes) {
25-
// set the validation error messages
26-
foreach (get_class_methods('Pixelpeter\IsoCodesValidation\IsoCodesValidator') as $method) {
27-
$key = $this->getTranslationKeyFromMethodName($method);
21+
$validatorService = $this->app->make('validator');
2822

29-
$messages[$key] = $this->getErrorMessage($translator, $messages, $key);
30-
}
23+
foreach (Registry::ruleNames() as $rule) {
24+
$validatorService->extend(
25+
$rule,
26+
function ($attribute, $value, $parameters, $validator) use ($rule): bool {
27+
$validatorClass = Registry::validatorClassForRule($rule);
28+
$referenceResolver = new ReferenceResolver;
29+
$isoCodesValdator = new IsoCodesValidator($attribute, $value, $parameters, $validator, $rule, $validatorClass, $referenceResolver);
3130

32-
return new IsoCodesValidator($translator, $data, $rules, $messages, $customAttributes);
33-
});
34-
}
31+
return $isoCodesValdator->validate();
32+
},
33+
$this->errorMessage($rule)
34+
);
3535

36-
/**
37-
* Return translation key for correspondent method name
38-
*
39-
* @param string $name
40-
* @return string
41-
*/
42-
private function getTranslationKeyFromMethodName($name)
43-
{
44-
if (stripos($name, 'validate') !== false) {
45-
return Str::snake(substr($name, 8));
46-
}
36+
$validatorService->replacer($rule, function ($message, $attribute, $_, $parameters, $validator) {
37+
$referenceResolver = new ReferenceResolver;
38+
$replacer = new Replacer($attribute, $parameters, $validator, $referenceResolver);
4739

48-
return '';
49-
}
40+
return $replacer->replace($message);
41+
});
5042

51-
/**
52-
* Return the matching error message for the key
53-
*
54-
* @param string $key
55-
* @return string
56-
*/
57-
private function getErrorMessage($translator, $messages, $key)
58-
{
59-
// return error messages passed directly to the validator
60-
if (isset($messages[$key])) {
61-
return $messages[$key];
62-
}
63-
64-
// return error message from validation translation file
65-
if ($translator->has("validation.{$key}")) {
66-
return $translator->get("validation.{$key}");
6743
}
68-
69-
// return packages default message
70-
return $translator->get("validation::validation.{$key}");
7144
}
7245

73-
/**
74-
* Register the application services.
75-
*
76-
* @return void
77-
*/
78-
public function register()
46+
protected function errorMessage(string $rulename): string
7947
{
80-
//
48+
return $this->app->make('translator')->get('validation::validation.'.$rulename);
8149
}
8250
}

0 commit comments

Comments
 (0)