Skip to content

Commit 509c468

Browse files
authored
Use Hyperf\Stringable\Str instead of Hyperf\Utils\Str (#5634)
1 parent b227c86 commit 509c468

File tree

3 files changed

+86
-1
lines changed

3 files changed

+86
-1
lines changed

composer.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@
3030
"autoload": {
3131
"psr-4": {
3232
"Hyperf\\Stringable\\": "src/"
33-
}
33+
},
34+
"files": [
35+
"src/Functions.php"
36+
]
3437
},
3538
"autoload-dev": {
3639
"psr-4": {

src/Functions.php

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
/**
15+
* Get a new stringable object from the given string.
16+
*
17+
* @param null|string $string
18+
* @return mixed|Stringable
19+
*/
20+
function str($string = null)
21+
{
22+
if (func_num_args() === 0) {
23+
return new class() {
24+
public function __call($method, $parameters)
25+
{
26+
return Str::$method(...$parameters);
27+
}
28+
29+
public function __toString()
30+
{
31+
return '';
32+
}
33+
};
34+
}
35+
36+
return Str::of($string);
37+
}

tests/FunctionsTest.php

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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 HyperfTest\Stringable;
13+
14+
use Hyperf\Stringable\Stringable;
15+
use PHPUnit\Framework\TestCase;
16+
use ReflectionClass;
17+
18+
use function Hyperf\Stringable\str;
19+
20+
/**
21+
* @internal
22+
* @coversNothing
23+
*/
24+
class FunctionsTest extends TestCase
25+
{
26+
public function testStr()
27+
{
28+
$stringable = str('string-value');
29+
30+
$this->assertInstanceOf(Stringable::class, $stringable);
31+
$this->assertSame('string-value', (string) $stringable);
32+
33+
$stringable = str($name = null);
34+
$this->assertInstanceOf(Stringable::class, $stringable);
35+
$this->assertTrue($stringable->isEmpty());
36+
37+
$strAccessor = str();
38+
$this->assertTrue((new ReflectionClass($strAccessor))->isAnonymous());
39+
$this->assertSame($strAccessor->limit('string-value', 3), 'str...');
40+
41+
$strAccessor = str();
42+
$this->assertTrue((new ReflectionClass($strAccessor))->isAnonymous());
43+
$this->assertSame((string) $strAccessor, '');
44+
}
45+
}

0 commit comments

Comments
 (0)