File tree Expand file tree Collapse file tree 3 files changed +86
-1
lines changed Expand file tree Collapse file tree 3 files changed +86
-1
lines changed Original file line number Diff line number Diff line change 30
30
"autoload" : {
31
31
"psr-4" : {
32
32
"Hyperf\\ Stringable\\ " : " src/"
33
- }
33
+ },
34
+ "files" : [
35
+ " src/Functions.php"
36
+ ]
34
37
},
35
38
"autoload-dev" : {
36
39
"psr-4" : {
Original file line number Diff line number Diff line change
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 group@hyperf.io
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
+ }
Original file line number Diff line number Diff line change
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 group@hyperf.io
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
+ }
You can’t perform that action at this time.
0 commit comments