Skip to content

Commit 4056a0c

Browse files
LTaooohuangdijialimingxinleo
authored
Removed string cache from Hyperf\Stringable\Str
Co-authored-by: Deeka Wong <[email protected]> Co-authored-by: 李铭昕 <[email protected]>
1 parent 7773386 commit 4056a0c

File tree

3 files changed

+103
-40
lines changed

3 files changed

+103
-40
lines changed

src/Str.php

+3-40
Original file line numberDiff line numberDiff line change
@@ -31,27 +31,6 @@ class Str
3131
{
3232
use Macroable;
3333

34-
/**
35-
* The cache of snake-cased words.
36-
*
37-
* @var array
38-
*/
39-
protected static $snakeCache = [];
40-
41-
/**
42-
* The cache of camel-cased words.
43-
*
44-
* @var array
45-
*/
46-
protected static $camelCache = [];
47-
48-
/**
49-
* The cache of studly-cased words.
50-
*
51-
* @var array
52-
*/
53-
protected static $studlyCache = [];
54-
5534
/**
5635
* Get a new stringable object from the given string.
5736
*
@@ -178,11 +157,7 @@ public static function between($subject, $from, $to)
178157
*/
179158
public static function camel($value)
180159
{
181-
if (isset(static::$camelCache[$value])) {
182-
return static::$camelCache[$value];
183-
}
184-
185-
return static::$camelCache[$value] = lcfirst(static::studly($value));
160+
return lcfirst(static::studly($value));
186161
}
187162

188163
/**
@@ -680,19 +655,13 @@ public static function slug(string $title, string $separator = '-', ?string $lan
680655
*/
681656
public static function snake(string $value, string $delimiter = '_'): string
682657
{
683-
$key = $value;
684-
685-
if (isset(static::$snakeCache[$key][$delimiter])) {
686-
return static::$snakeCache[$key][$delimiter];
687-
}
688-
689658
if (! ctype_lower($value)) {
690659
$value = preg_replace('/\s+/u', '', ucwords($value));
691660

692661
$value = static::lower(preg_replace('/(.)(?=[A-Z])/u', '$1' . $delimiter, $value));
693662
}
694663

695-
return static::$snakeCache[$key][$delimiter] = $value;
664+
return $value;
696665
}
697666

698667
/**
@@ -716,15 +685,9 @@ public static function startsWith(string $haystack, $needles): bool
716685
*/
717686
public static function studly(string $value, string $gap = ''): string
718687
{
719-
$key = $value;
720-
721-
if (isset(static::$studlyCache[$key])) {
722-
return static::$studlyCache[$key];
723-
}
724-
725688
$value = ucwords(str_replace(['-', '_'], ' ', $value));
726689

727-
return static::$studlyCache[$key] = str_replace(' ', $gap, $value);
690+
return str_replace(' ', $gap, $value);
728691
}
729692

730693
/**

src/StrCache.php

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of Hyperf.
6+
*
7+
* @link https://www.hyperf.io
8+
* @document https://hyperf.wiki
9+
* @contact [email protected]
10+
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
11+
*/
12+
namespace Hyperf\Stringable;
13+
14+
class StrCache
15+
{
16+
/**
17+
* The cache of snake-cased words.
18+
*/
19+
protected static array $snakeCache = [];
20+
21+
/**
22+
* The cache of camel-cased words.
23+
*/
24+
protected static array $camelCache = [];
25+
26+
/**
27+
* The cache of studly-cased words.
28+
*/
29+
protected static array $studlyCache = [];
30+
31+
public static function camel($value)
32+
{
33+
if (isset(static::$camelCache[$value])) {
34+
return static::$camelCache[$value];
35+
}
36+
37+
return static::$camelCache[$value] = Str::camel($value);
38+
}
39+
40+
public static function snake(string $value, string $delimiter = '_'): string
41+
{
42+
if (isset(static::$snakeCache[$value][$delimiter])) {
43+
return static::$snakeCache[$value][$delimiter];
44+
}
45+
46+
return static::$snakeCache[$value][$delimiter] = Str::snake($value, $delimiter);
47+
}
48+
49+
public static function studly(string $value, string $gap = ''): string
50+
{
51+
if (isset(static::$studlyCache[$value][$gap])) {
52+
return static::$studlyCache[$value][$gap];
53+
}
54+
55+
return static::$studlyCache[$value][$gap] = Str::studly($value, $gap);
56+
}
57+
}

tests/StrTest.php

+43
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace HyperfTest\Stringable;
1313

1414
use Hyperf\Stringable\Str;
15+
use Hyperf\Stringable\StrCache;
1516
use InvalidArgumentException;
1617
use PHPUnit\Framework\Attributes\CoversNothing;
1718
use PHPUnit\Framework\TestCase;
@@ -241,4 +242,46 @@ public function testIsMatch()
241242
$this->assertTrue(Str::isMatch(['/laravel/i', '/laravel!(.*)/'], 'Hello, Laravel!'));
242243
$this->assertTrue(Str::isMatch(['/^[a-zA-Z,!]+$/', '/^(.*(.*(.*)))/'], 'Hello, Laravel!'));
243244
}
245+
246+
public function testCamel()
247+
{
248+
$this->assertSame('helloWorld', Str::camel('HelloWorld'));
249+
$this->assertSame('helloWorld', Str::camel('hello_world'));
250+
$this->assertSame('helloWorld', Str::camel('hello-world'));
251+
$this->assertSame('helloWorld', Str::camel('hello world'));
252+
253+
$this->assertSame('helloWorld', StrCache::camel('HelloWorld'));
254+
$this->assertSame('helloWorld', StrCache::camel('HelloWorld'));
255+
$this->assertSame('helloWorld', StrCache::camel('hello_world'));
256+
$this->assertSame('helloWorld', StrCache::camel('hello-world'));
257+
$this->assertSame('helloWorld', StrCache::camel('hello world'));
258+
}
259+
260+
public function testSnake()
261+
{
262+
$this->assertSame('hello_world', Str::snake('HelloWorld'));
263+
$this->assertSame('hello_world', Str::snake('hello_world'));
264+
$this->assertSame('hello_world', Str::snake('hello world'));
265+
266+
$this->assertSame('hello_world', StrCache::snake('HelloWorld'));
267+
$this->assertSame('hello_world', StrCache::snake('HelloWorld'));
268+
$this->assertSame('hello_world', StrCache::snake('hello_world'));
269+
$this->assertSame('hello_world', StrCache::snake('hello world'));
270+
}
271+
272+
public function testStudly()
273+
{
274+
$this->assertSame('HelloWorld', Str::studly('helloWorld'));
275+
$this->assertSame('HelloWorld', Str::studly('hello_world'));
276+
$this->assertSame('HelloWorld', Str::studly('hello-world'));
277+
$this->assertSame('HelloWorld', Str::studly('hello world'));
278+
$this->assertSame('Hello-World', Str::studly('hello world', '-'));
279+
280+
$this->assertSame('HelloWorld', StrCache::studly('helloWorld'));
281+
$this->assertSame('HelloWorld', StrCache::studly('helloWorld'));
282+
$this->assertSame('HelloWorld', StrCache::studly('hello_world'));
283+
$this->assertSame('HelloWorld', StrCache::studly('hello-world'));
284+
$this->assertSame('HelloWorld', StrCache::studly('hello world'));
285+
$this->assertSame('Hello-World', StrCache::studly('hello world', '-'));
286+
}
244287
}

0 commit comments

Comments
 (0)