44
55namespace Axlon \PostalCodeValidation \Extensions ;
66
7- use Axlon \PostalCodeValidation \PostalCodeValidator ;
8- use Axlon \PostalCodeValidation \Support \PostalCodeExamples ;
7+ use Axlon \PostalCodeValidation \Contracts \RegexFactory ;
98use Illuminate \Support \Arr ;
109use Illuminate \Validation \Validator ;
1110use InvalidArgumentException ;
1211
1312final 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