Skip to content

Commit 5ee5b68

Browse files
committed
Add new validation engine
1 parent d9ea08d commit 5ee5b68

19 files changed

+344
-450
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,6 @@ All notable changes to this project will be documented in this file.
1212
### Removed
1313

1414
- Lumen is no longer supported
15+
- Manual validation outside of Laravel validation has been removed
16+
- Overriding validation patterns has been removed
1517
- PostalCodes facade has been removed

README.md

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ Worldwide postal code validation for Laravel, based on Google's Address Data Ser
2323
- [Available rules](#available-rules)
2424
- [Fluent API](#fluent-api)
2525
- [Adding an error message](#adding-an-error-message)
26-
- [Manually validating](#manually-validating)
27-
- [Overriding rules](#overriding-rules)
2826
- [Changelog](#changelog)
2927
- [Contributing](#contributing)
3028
- [Credits](#credits)
@@ -124,34 +122,6 @@ The following placeholders will be automatically filled for you:
124122

125123
*The `:countries` and `:examples` placeholders may be empty if no valid countries are passed.
126124

127-
### Manually validating
128-
129-
If you want to validate postal codes manually outside of Laravel's validation system, you can call the validator
130-
directly, like so:
131-
132-
```php
133-
PostalCodes::passes($country, $postalCode); // returns a boolean
134-
```
135-
136-
### Overriding rules
137-
138-
Depending on your use case you may want to override the patterns used to validate postal codes for a country. You can do
139-
this by adding the code below in a central place in your application (e.g. a service provider):
140-
141-
```php
142-
PostalCodes::override('country', '/your pattern/');
143-
144-
// You can also pass overrides as an array
145-
146-
PostalCodes::override([
147-
'country 1' => '/pattern 1/',
148-
'country 2' => '/pattern 2/',
149-
]);
150-
```
151-
152-
**Important**: If you believe there is a bug in one of the patterns that this package ships with, please create an
153-
[issue](https://github.com/axlon/laravel-postal-code-validation/issues/new) in the issue tracker.
154-
155125
## Changelog
156126

157127
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

phpstan-baseline.neon

Lines changed: 0 additions & 43 deletions
This file was deleted.

phpstan.neon.dist

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
includes:
22
- phar://phpstan.phar/conf/bleedingEdge.neon
3-
- phpstan-baseline.neon
43

54
parameters:
65
level: 10
6+
featureToggles:
7+
internalTag: false
78
paths:
89
- src
910
- tests

src/Contracts/Regex.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Axlon\PostalCodeValidation\Contracts;
6+
7+
interface Regex
8+
{
9+
/**
10+
* Get an example value that matches the regex.
11+
*
12+
* @return non-empty-string
13+
*/
14+
public function example(): string;
15+
16+
/**
17+
* Determine if the given value matches the regex.
18+
*
19+
* @param string $value
20+
* @return bool
21+
*/
22+
public function test(string $value): bool;
23+
}

src/Contracts/RegexFactory.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Axlon\PostalCodeValidation\Contracts;
6+
7+
interface RegexFactory
8+
{
9+
/**
10+
* Get the regular expression for the specified territory.
11+
*
12+
* @param string $key
13+
* @return \Axlon\PostalCodeValidation\Contracts\Regex|null
14+
*/
15+
public function get(string $key): ?Regex;
16+
}

src/Extensions/PostalCode.php

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,20 @@
44

55
namespace Axlon\PostalCodeValidation\Extensions;
66

7-
use Axlon\PostalCodeValidation\PostalCodeValidator;
8-
use Axlon\PostalCodeValidation\Support\PostalCodeExamples;
7+
use Axlon\PostalCodeValidation\Contracts\RegexFactory;
98
use InvalidArgumentException;
109

1110
final class PostalCode
1211
{
13-
/**
14-
* The postal code examples.
15-
*
16-
* @var \Axlon\PostalCodeValidation\Support\PostalCodeExamples
17-
*/
18-
protected $examples;
19-
20-
/**
21-
* The pattern matcher.
22-
*
23-
* @var \Axlon\PostalCodeValidation\PostalCodeValidator
24-
*/
25-
protected $validator;
26-
2712
/**
2813
* Create a new PostalCode validator extension.
2914
*
30-
* @param \Axlon\PostalCodeValidation\PostalCodeValidator $validator
31-
* @param \Axlon\PostalCodeValidation\Support\PostalCodeExamples $examples
15+
* @param \Axlon\PostalCodeValidation\Contracts\RegexFactory $regexes
3216
* @return void
3317
*/
34-
public function __construct(PostalCodeValidator $validator, PostalCodeExamples $examples)
35-
{
36-
$this->examples = $examples;
37-
$this->validator = $validator;
18+
public function __construct(
19+
private readonly RegexFactory $regexes,
20+
) {
3821
}
3922

4023
/**
@@ -52,7 +35,7 @@ public function replace(string $message, string $attribute, string $rule, array
5235
$examples = [];
5336

5437
foreach ($parameters as $parameter) {
55-
if (($example = $this->examples->get($parameter)) === null) {
38+
if (($example = $this->regexes->get($parameter)?->example()) === null) {
5639
continue;
5740
}
5841

@@ -83,8 +66,12 @@ public function validate(string $attribute, ?string $value, array $parameters):
8366
throw new InvalidArgumentException('Validation rule postal_code requires at least 1 parameter.');
8467
}
8568

69+
if (!is_string($value)) {
70+
return false;
71+
}
72+
8673
foreach ($parameters as $parameter) {
87-
if ($this->validator->passes($parameter, $value)) {
74+
if ($this->regexes->get($parameter)?->test($value) === true) {
8875
return true;
8976
}
9077
}

src/Extensions/PostalCodeFor.php

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,22 @@
44

55
namespace Axlon\PostalCodeValidation\Extensions;
66

7-
use Axlon\PostalCodeValidation\PostalCodeValidator;
8-
use Axlon\PostalCodeValidation\Support\PostalCodeExamples;
7+
use Axlon\PostalCodeValidation\Contracts\RegexFactory;
98
use Illuminate\Support\Arr;
109
use Illuminate\Validation\Validator;
1110
use InvalidArgumentException;
1211

1312
final class PostalCodeFor
1413
{
15-
/**
16-
* The postal code examples.
17-
*
18-
* @var \Axlon\PostalCodeValidation\Support\PostalCodeExamples
19-
*/
20-
protected $examples;
21-
22-
/**
23-
* The pattern matcher.
24-
*
25-
* @var \Axlon\PostalCodeValidation\PostalCodeValidator
26-
*/
27-
protected $validator;
28-
2914
/**
3015
* Create a new PostalCodeFor validator extension.
3116
*
32-
* @param \Axlon\PostalCodeValidation\PostalCodeValidator $validator
33-
* @param \Axlon\PostalCodeValidation\Support\PostalCodeExamples $examples
17+
* @param \Axlon\PostalCodeValidation\Contracts\RegexFactory $regexes
3418
* @return void
3519
*/
36-
public function __construct(PostalCodeValidator $validator, PostalCodeExamples $examples)
37-
{
38-
$this->examples = $examples;
39-
$this->validator = $validator;
20+
public function __construct(
21+
private readonly RegexFactory $regexes,
22+
) {
4023
}
4124

4225
/**
@@ -60,11 +43,13 @@ public function replace(
6043
$examples = [];
6144

6245
foreach ($parameters as $parameter) {
63-
if (($input = Arr::get($validator->getData(), $parameter)) === null) {
46+
$input = Arr::get($validator->getData(), $parameter);
47+
48+
if (!is_string($input)) {
6449
continue;
6550
}
6651

67-
if (($example = $this->examples->get($input)) === null) {
52+
if (($example = $this->regexes->get($input)?->example()) === null) {
6853
continue;
6954
}
7055

@@ -102,12 +87,16 @@ public function validate(string $attribute, ?string $value, array $parameters, V
10287
return true;
10388
}
10489

90+
if (!is_string($value)) {
91+
return false;
92+
}
93+
10594
foreach ($parameters as $parameter) {
10695
if ($parameter === null) {
10796
continue;
10897
}
10998

110-
if ($this->validator->passes($parameter, $value)) {
99+
if (is_string($parameter) && $this->regexes->get($parameter)?->test($value) === true) {
111100
return true;
112101
}
113102
}

0 commit comments

Comments
 (0)