@@ -22,6 +22,13 @@ final class PostalCode implements ValidationRule, DataAwareRule
2222 */
2323 private array $ data = [];
2424
25+ /**
26+ * The resolved parameters.
27+ *
28+ * @var list<non-empty-string|false>|null
29+ */
30+ private ?array $ resolvedParameters = null ;
31+
2532 /**
2633 * Create a new validation rule.
2734 *
@@ -63,24 +70,54 @@ public static function of(array|string $countries): self
6370 }
6471
6572 /**
66- * Resolve the rule parameters.
73+ * Get the replacements for the validation message.
74+ *
75+ * @return array<string, string>
76+ */
77+ public function replacements (): array
78+ {
79+ $ countries = $ examples = [];
80+
81+ foreach ($ this ->resolvedParameters () as $ parameter ) {
82+ if ($ parameter === false ) {
83+ continue ;
84+ }
85+
86+ $ countries [] = $ parameter ;
87+ $ regex = $ this ->regexes ->get ($ parameter );
88+
89+ if ($ regex !== null ) {
90+ $ examples [] = $ regex ->example ();
91+ }
92+ }
93+
94+ return [
95+ 'countries ' => implode (', ' , $ countries ),
96+ 'examples ' => implode (', ' , $ examples ),
97+ ];
98+ }
99+
100+ /**
101+ * Get the resolved parameters.
67102 *
68103 * @return list<non-empty-string|false>
69104 */
70- private function resolveParameters (): array
105+ private function resolvedParameters (): array
71106 {
72- $ parameters = [];
73-
74- foreach ($ this ->parameters as $ parameter ) {
75- if (self ::isCountryCode ($ parameter )) {
76- $ parameters [] = $ parameter ;
77- } elseif (Arr::has ($ this ->data , $ parameter )) {
78- $ other = Arr::get ($ this ->data , $ parameter );
79- $ parameters [] = self ::isCountryCode ($ other ) ? $ other : false ;
107+ if ($ this ->resolvedParameters === null ) {
108+ $ this ->resolvedParameters = [];
109+
110+ foreach ($ this ->parameters as $ parameter ) {
111+ if (self ::isCountryCode ($ parameter )) {
112+ $ this ->resolvedParameters [] = $ parameter ;
113+ } elseif (Arr::has ($ this ->data , $ parameter )) {
114+ $ other = Arr::get ($ this ->data , $ parameter );
115+ $ this ->resolvedParameters [] = self ::isCountryCode ($ other ) ? $ other : false ;
116+ }
80117 }
81118 }
82119
83- return $ parameters ;
120+ return $ this -> resolvedParameters ;
84121 }
85122
86123 /**
@@ -92,6 +129,7 @@ private function resolveParameters(): array
92129 public function setData (array $ data ): self
93130 {
94131 $ this ->data = $ data ;
132+ $ this ->resolvedParameters = null ;
95133
96134 return $ this ;
97135 }
@@ -110,35 +148,22 @@ public function validate(string $attribute, mixed $value, Closure $fail): void
110148 return ;
111149 }
112150
113- $ examples = $ countryCodes = [];
114- $ parameters = $ this ->resolveParameters ();
115-
116- if ($ parameters === [] && is_string ($ value )) {
117- return ;
118- }
119-
120151 if (is_string ($ value )) {
152+ if (($ parameters = $ this ->resolvedParameters ()) === []) {
153+ return ;
154+ }
155+
121156 foreach ($ parameters as $ parameter ) {
122157 if ($ parameter === false ) {
123158 continue ;
124159 }
125160
126- $ regex = $ this ->regexes ->get ($ parameter );
127-
128- if ($ regex ?->test($ value ) === true ) {
161+ if ($ this ->regexes ->get ($ parameter )?->test($ value ) === true ) {
129162 return ;
130163 }
131-
132- if ($ regex !== null ) {
133- $ countryCodes [] = $ parameter ;
134- $ examples [] = $ regex ->example ();
135- }
136164 }
137165 }
138166
139- $ fail ('validation.postal_code ' )->translate ([
140- 'countries ' => implode (', ' , $ countryCodes ),
141- 'examples ' => implode (', ' , $ examples ),
142- ]);
167+ $ fail ('validation.postal_code ' )->translate ($ this ->replacements ());
143168 }
144169}
0 commit comments