Skip to content

Commit f7e6e8c

Browse files
authored
Use StrCache instead of Str in special cases. (#5866)
1 parent 4ef52f1 commit f7e6e8c

File tree

4 files changed

+24
-21
lines changed

4 files changed

+24
-21
lines changed

src/Concerns/FormatsMessages.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Hyperf\Collection\Arr;
1616
use Hyperf\HttpMessage\Upload\UploadedFile;
1717
use Hyperf\Stringable\Str;
18+
use Hyperf\Stringable\StrCache;
1819
use Hyperf\Validation\Validator;
1920

2021
use function Hyperf\Support\make;
@@ -35,8 +36,8 @@ public function makeReplacements(string $message, string $attribute, string $rul
3536

3637
$message = $this->replaceInputPlaceholder($message, $attribute);
3738

38-
if (isset($this->replacers[Str::snake($rule)])) {
39-
return $this->callReplacer($message, $attribute, Str::snake($rule), $parameters, $this);
39+
if (isset($this->replacers[StrCache::snake($rule)])) {
40+
return $this->callReplacer($message, $attribute, StrCache::snake($rule), $parameters, $this);
4041
}
4142
if (method_exists($this, $replacer = "replace{$rule}")) {
4243
return $this->{$replacer}($message, $attribute, $rule, $parameters);
@@ -78,7 +79,7 @@ public function getDisplayableAttribute(string $attribute): string
7879
return $attribute;
7980
}
8081

81-
return str_replace('_', ' ', Str::snake($attribute));
82+
return str_replace('_', ' ', StrCache::snake($attribute));
8283
}
8384

8485
/**
@@ -115,7 +116,7 @@ protected function getMessage(string $attribute, string $rule): string
115116
return $inlineMessage;
116117
}
117118

118-
$lowerRule = Str::snake($rule);
119+
$lowerRule = StrCache::snake($rule);
119120

120121
$customMessage = $this->getCustomMessageFromTranslator(
121122
$customKey = "validation.custom.{$attribute}.{$lowerRule}"
@@ -158,7 +159,7 @@ protected function getMessage(string $attribute, string $rule): string
158159
*/
159160
protected function getInlineMessage(string $attribute, string $rule)
160161
{
161-
$inlineEntry = $this->getFromLocalArray($attribute, Str::snake($rule));
162+
$inlineEntry = $this->getFromLocalArray($attribute, StrCache::snake($rule));
162163

163164
return is_array($inlineEntry) && in_array($rule, $this->sizeRules)
164165
? $inlineEntry[$this->getAttributeType($attribute)]
@@ -231,7 +232,7 @@ protected function getWildcardCustomMessages(array $messages, string $search, st
231232
*/
232233
protected function getSizeMessage(string $attribute, string $rule): string
233234
{
234-
$lowerRule = Str::snake($rule);
235+
$lowerRule = StrCache::snake($rule);
235236

236237
// There are three different types of size validations. The attribute may be
237238
// either a number, file, or string so we will check a few things to know

src/ValidationRuleParser.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Closure;
1515
use Hyperf\Collection\Arr;
1616
use Hyperf\Stringable\Str;
17+
use Hyperf\Stringable\StrCache;
1718
use Hyperf\Validation\Contract\Rule as RuleContract;
1819
use Hyperf\Validation\Rules\Exists;
1920
use Hyperf\Validation\Rules\Unique;
@@ -202,7 +203,7 @@ protected function mergeRulesForAttribute(array $results, string $attribute, mix
202203
*/
203204
protected static function parseArrayRule(array $rules): array
204205
{
205-
return [Str::studly(trim((string) Arr::get($rules, 0))), array_slice($rules, 1)];
206+
return [StrCache::studly(trim((string) Arr::get($rules, 0))), array_slice($rules, 1)];
206207
}
207208

208209
/**
@@ -221,7 +222,7 @@ protected static function parseStringRule(string $rules): array
221222
$parameters = static::parseParameters($rules, $parameter);
222223
}
223224

224-
return [Str::studly(trim($rules)), $parameters];
225+
return [StrCache::studly(trim($rules)), $parameters];
225226
}
226227

227228
/**

src/Validator.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Hyperf\Contract\ValidatorInterface as ValidatorContract;
2020
use Hyperf\HttpMessage\Upload\UploadedFile;
2121
use Hyperf\Stringable\Str;
22+
use Hyperf\Stringable\StrCache;
2223
use Hyperf\Support\Fluent;
2324
use Hyperf\Support\MessageBag;
2425
use Hyperf\Validation\Contract\ImplicitRule;
@@ -172,7 +173,7 @@ public function __construct(
172173
*/
173174
public function __call($method, $parameters)
174175
{
175-
$rule = Str::snake(substr($method, 8));
176+
$rule = StrCache::snake(substr($method, 8));
176177

177178
if (isset($this->extensions[$rule])) {
178179
return $this->callExtension($rule, $parameters);
@@ -498,7 +499,7 @@ public function sometimes($attribute, $rules, callable $callback): self
498499
public function addExtensions(array $extensions)
499500
{
500501
if ($extensions) {
501-
$keys = array_map('\Hyperf\Stringable\Str::snake', array_keys($extensions));
502+
$keys = array_map('\Hyperf\Stringable\StrCache::snake', array_keys($extensions));
502503

503504
$extensions = array_combine($keys, array_values($extensions));
504505
}
@@ -514,7 +515,7 @@ public function addImplicitExtensions(array $extensions)
514515
$this->addExtensions($extensions);
515516

516517
foreach ($extensions as $rule => $extension) {
517-
$this->implicitRules[] = Str::studly($rule);
518+
$this->implicitRules[] = StrCache::studly($rule);
518519
}
519520
}
520521

@@ -526,7 +527,7 @@ public function addDependentExtensions(array $extensions)
526527
$this->addExtensions($extensions);
527528

528529
foreach ($extensions as $rule => $extension) {
529-
$this->dependentRules[] = Str::studly($rule);
530+
$this->dependentRules[] = StrCache::studly($rule);
530531
}
531532
}
532533

@@ -535,7 +536,7 @@ public function addDependentExtensions(array $extensions)
535536
*/
536537
public function addExtension(string $rule, Closure|string $extension)
537538
{
538-
$this->extensions[Str::snake($rule)] = $extension;
539+
$this->extensions[StrCache::snake($rule)] = $extension;
539540
}
540541

541542
/**
@@ -545,7 +546,7 @@ public function addImplicitExtension(string $rule, Closure|string $extension)
545546
{
546547
$this->addExtension($rule, $extension);
547548

548-
$this->implicitRules[] = Str::studly($rule);
549+
$this->implicitRules[] = StrCache::studly($rule);
549550
}
550551

551552
/**
@@ -555,7 +556,7 @@ public function addDependentExtension(string $rule, Closure|string $extension)
555556
{
556557
$this->addExtension($rule, $extension);
557558

558-
$this->dependentRules[] = Str::studly($rule);
559+
$this->dependentRules[] = StrCache::studly($rule);
559560
}
560561

561562
/**
@@ -564,7 +565,7 @@ public function addDependentExtension(string $rule, Closure|string $extension)
564565
public function addReplacers(array $replacers)
565566
{
566567
if ($replacers) {
567-
$keys = array_map('\Hyperf\Stringable\Str::snake', array_keys($replacers));
568+
$keys = array_map('\Hyperf\Stringable\StrCache::snake', array_keys($replacers));
568569

569570
$replacers = array_combine($keys, array_values($replacers));
570571
}
@@ -577,7 +578,7 @@ public function addReplacers(array $replacers)
577578
*/
578579
public function addReplacer(string $rule, Closure|string $replacer)
579580
{
580-
$this->replacers[Str::snake($rule)] = $replacer;
581+
$this->replacers[StrCache::snake($rule)] = $replacer;
581582
}
582583

583584
/**

src/ValidatorFactory.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use Closure;
1515
use Hyperf\Contract\TranslatorInterface;
1616
use Hyperf\Contract\ValidatorInterface;
17-
use Hyperf\Stringable\Str;
17+
use Hyperf\Stringable\StrCache;
1818
use Hyperf\Validation\Contract\PresenceVerifierInterface;
1919
use Hyperf\Validation\Contract\ValidatorFactoryInterface;
2020
use Psr\Container\ContainerInterface;
@@ -128,7 +128,7 @@ public function extend(string $rule, Closure|string $extension, ?string $message
128128
$this->extensions[$rule] = $extension;
129129

130130
if ($message) {
131-
$this->fallbackMessages[Str::snake($rule)] = $message;
131+
$this->fallbackMessages[StrCache::snake($rule)] = $message;
132132
}
133133
}
134134

@@ -140,7 +140,7 @@ public function extendImplicit(string $rule, Closure|string $extension, ?string
140140
$this->implicitExtensions[$rule] = $extension;
141141

142142
if ($message) {
143-
$this->fallbackMessages[Str::snake($rule)] = $message;
143+
$this->fallbackMessages[StrCache::snake($rule)] = $message;
144144
}
145145
}
146146

@@ -152,7 +152,7 @@ public function extendDependent(string $rule, Closure|string $extension, ?string
152152
$this->dependentExtensions[$rule] = $extension;
153153

154154
if ($message) {
155-
$this->fallbackMessages[Str::snake($rule)] = $message;
155+
$this->fallbackMessages[StrCache::snake($rule)] = $message;
156156
}
157157
}
158158

0 commit comments

Comments
 (0)