@@ -22,11 +22,7 @@ protected function replaceAcceptedIf($message, $attribute, $rule, $parameters)
2222
2323 $ parameters [0 ] = $ this ->getDisplayableAttribute ($ parameters [0 ]);
2424
25- return str_replace (
26- [':other ' , ':OTHER ' , ':Other ' , ':value ' , ':VALUE ' , ':Value ' ],
27- [$ parameters [0 ], Str::upper ($ parameters [0 ]), Str::ucfirst ($ parameters [0 ]), $ parameters [1 ], Str::upper ($ parameters [1 ]), Str::ucfirst ($ parameters [1 ])],
28- $ message
29- );
25+ return $ this ->replaceWhileKeepingCase ($ message , ['other ' => $ parameters [0 ], 'value ' => $ parameters [1 ]]);
3026 }
3127
3228 /**
@@ -337,11 +333,7 @@ protected function replaceInArray($message, $attribute, $rule, $parameters)
337333 {
338334 $ value = $ this ->getDisplayableAttribute ($ parameters [0 ]);
339335
340- return str_replace (
341- [':other ' , ':OTHER ' , ':Other ' ],
342- [$ value , Str::upper ($ value ), Str::ucfirst ($ value )],
343- $ message
344- );
336+ return $ this ->replaceWhileKeepingCase ($ message , ['other ' => $ value ]);
345337 }
346338
347339 /**
@@ -629,11 +621,7 @@ protected function replaceRequiredIfAccepted($message, $attribute, $rule, $param
629621 {
630622 $ value = $ this ->getDisplayableAttribute ($ parameters [0 ]);
631623
632- return str_replace (
633- [':other ' , ':OTHER ' , ':Other ' ],
634- [$ value , Str::upper ($ value ), Str::ucfirst ($ value )],
635- $ message
636- );
624+ return $ this ->replaceWhileKeepingCase ($ message , ['other ' => $ value ]);
637625 }
638626
639627 /**
@@ -774,11 +762,7 @@ protected function replaceSame($message, $attribute, $rule, $parameters)
774762 {
775763 $ value = $ this ->getDisplayableAttribute ($ parameters [0 ]);
776764
777- return str_replace (
778- [':other ' , ':OTHER ' , ':Other ' ],
779- [$ value , Str::upper ($ value ), Str::ucfirst ($ value )],
780- $ message
781- );
765+ return $ this ->replaceWhileKeepingCase ($ message , ['other ' => $ value ]);
782766 }
783767
784768 /**
@@ -946,4 +930,28 @@ protected function replaceDoesntContain($message, $attribute, $rule, $parameters
946930 {
947931 return $ this ->replaceIn ($ message , $ attribute , $ rule , $ parameters );
948932 }
933+
934+ /**
935+ * Replace the given string while maintaining different casing variants.
936+ *
937+ * @param array<string, string> $mapping
938+ */
939+ private function replaceWhileKeepingCase (string $ message , array $ mapping ): string
940+ {
941+ $ fn = [Str::lower (...), Str::upper (...), Str::ucfirst (...)];
942+
943+ $ cases = array_reduce (
944+ array_keys ($ mapping ),
945+ fn (array $ carry , string $ placeholder ) => [...$ carry , ...array_map (fn (callable $ fn ) => ': ' .$ fn ($ placeholder ), $ fn )],
946+ [],
947+ );
948+
949+ $ replacements = array_reduce (
950+ array_values ($ mapping ),
951+ fn (array $ carry , string $ parameter ) => [...$ carry , ...array_map (fn (callable $ fn ) => $ fn ($ parameter ), $ fn )],
952+ [],
953+ );
954+
955+ return str_replace ($ cases , $ replacements , $ message );
956+ }
949957}
0 commit comments