Skip to content

Commit

Permalink
Removed string cache from Hyperf\Stringable\Str
Browse files Browse the repository at this point in the history

Co-authored-by: Deeka Wong <[email protected]>
Co-authored-by: 李铭昕 <[email protected]>
  • Loading branch information
3 people authored Jun 20, 2023
1 parent 7773386 commit 4056a0c
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 40 deletions.
43 changes: 3 additions & 40 deletions src/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,6 @@ class Str
{
use Macroable;

/**
* The cache of snake-cased words.
*
* @var array
*/
protected static $snakeCache = [];

/**
* The cache of camel-cased words.
*
* @var array
*/
protected static $camelCache = [];

/**
* The cache of studly-cased words.
*
* @var array
*/
protected static $studlyCache = [];

/**
* Get a new stringable object from the given string.
*
Expand Down Expand Up @@ -178,11 +157,7 @@ public static function between($subject, $from, $to)
*/
public static function camel($value)
{
if (isset(static::$camelCache[$value])) {
return static::$camelCache[$value];
}

return static::$camelCache[$value] = lcfirst(static::studly($value));
return lcfirst(static::studly($value));
}

/**
Expand Down Expand Up @@ -680,19 +655,13 @@ public static function slug(string $title, string $separator = '-', ?string $lan
*/
public static function snake(string $value, string $delimiter = '_'): string
{
$key = $value;

if (isset(static::$snakeCache[$key][$delimiter])) {
return static::$snakeCache[$key][$delimiter];
}

if (! ctype_lower($value)) {
$value = preg_replace('/\s+/u', '', ucwords($value));

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

return static::$snakeCache[$key][$delimiter] = $value;
return $value;
}

/**
Expand All @@ -716,15 +685,9 @@ public static function startsWith(string $haystack, $needles): bool
*/
public static function studly(string $value, string $gap = ''): string
{
$key = $value;

if (isset(static::$studlyCache[$key])) {
return static::$studlyCache[$key];
}

$value = ucwords(str_replace(['-', '_'], ' ', $value));

return static::$studlyCache[$key] = str_replace(' ', $gap, $value);
return str_replace(' ', $gap, $value);
}

/**
Expand Down
57 changes: 57 additions & 0 deletions src/StrCache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact [email protected]
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace Hyperf\Stringable;

class StrCache
{
/**
* The cache of snake-cased words.
*/
protected static array $snakeCache = [];

/**
* The cache of camel-cased words.
*/
protected static array $camelCache = [];

/**
* The cache of studly-cased words.
*/
protected static array $studlyCache = [];

public static function camel($value)
{
if (isset(static::$camelCache[$value])) {
return static::$camelCache[$value];
}

return static::$camelCache[$value] = Str::camel($value);
}

public static function snake(string $value, string $delimiter = '_'): string
{
if (isset(static::$snakeCache[$value][$delimiter])) {
return static::$snakeCache[$value][$delimiter];
}

return static::$snakeCache[$value][$delimiter] = Str::snake($value, $delimiter);
}

public static function studly(string $value, string $gap = ''): string
{
if (isset(static::$studlyCache[$value][$gap])) {
return static::$studlyCache[$value][$gap];
}

return static::$studlyCache[$value][$gap] = Str::studly($value, $gap);
}
}
43 changes: 43 additions & 0 deletions tests/StrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace HyperfTest\Stringable;

use Hyperf\Stringable\Str;
use Hyperf\Stringable\StrCache;
use InvalidArgumentException;
use PHPUnit\Framework\Attributes\CoversNothing;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -241,4 +242,46 @@ public function testIsMatch()
$this->assertTrue(Str::isMatch(['/laravel/i', '/laravel!(.*)/'], 'Hello, Laravel!'));
$this->assertTrue(Str::isMatch(['/^[a-zA-Z,!]+$/', '/^(.*(.*(.*)))/'], 'Hello, Laravel!'));
}

public function testCamel()
{
$this->assertSame('helloWorld', Str::camel('HelloWorld'));
$this->assertSame('helloWorld', Str::camel('hello_world'));
$this->assertSame('helloWorld', Str::camel('hello-world'));
$this->assertSame('helloWorld', Str::camel('hello world'));

$this->assertSame('helloWorld', StrCache::camel('HelloWorld'));
$this->assertSame('helloWorld', StrCache::camel('HelloWorld'));
$this->assertSame('helloWorld', StrCache::camel('hello_world'));
$this->assertSame('helloWorld', StrCache::camel('hello-world'));
$this->assertSame('helloWorld', StrCache::camel('hello world'));
}

public function testSnake()
{
$this->assertSame('hello_world', Str::snake('HelloWorld'));
$this->assertSame('hello_world', Str::snake('hello_world'));
$this->assertSame('hello_world', Str::snake('hello world'));

$this->assertSame('hello_world', StrCache::snake('HelloWorld'));
$this->assertSame('hello_world', StrCache::snake('HelloWorld'));
$this->assertSame('hello_world', StrCache::snake('hello_world'));
$this->assertSame('hello_world', StrCache::snake('hello world'));
}

public function testStudly()
{
$this->assertSame('HelloWorld', Str::studly('helloWorld'));
$this->assertSame('HelloWorld', Str::studly('hello_world'));
$this->assertSame('HelloWorld', Str::studly('hello-world'));
$this->assertSame('HelloWorld', Str::studly('hello world'));
$this->assertSame('Hello-World', Str::studly('hello world', '-'));

$this->assertSame('HelloWorld', StrCache::studly('helloWorld'));
$this->assertSame('HelloWorld', StrCache::studly('helloWorld'));
$this->assertSame('HelloWorld', StrCache::studly('hello_world'));
$this->assertSame('HelloWorld', StrCache::studly('hello-world'));
$this->assertSame('HelloWorld', StrCache::studly('hello world'));
$this->assertSame('Hello-World', StrCache::studly('hello world', '-'));
}
}

0 comments on commit 4056a0c

Please sign in to comment.