Skip to content

Commit d7cf744

Browse files
[9.x] Retrieve the underlying string value in Stringable (#40861)
* Add method to retrieve the underlying string value * rename method Co-authored-by: Taylor Otwell <[email protected]>
1 parent 36c3966 commit d7cf744

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

src/Illuminate/Support/Stringable.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -992,6 +992,26 @@ public function dd()
992992
exit(1);
993993
}
994994

995+
/**
996+
* Get the underlying string value.
997+
*
998+
* @return string
999+
*/
1000+
public function value()
1001+
{
1002+
return $this->toString();
1003+
}
1004+
1005+
/**
1006+
* Get the underlying string value.
1007+
*
1008+
* @return string
1009+
*/
1010+
public function toString()
1011+
{
1012+
return $this->value;
1013+
}
1014+
9951015
/**
9961016
* Convert the object to a string when JSON encoded.
9971017
*

tests/Support/SupportStringableTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -943,4 +943,10 @@ public function testScan()
943943
$this->assertSame(['Otwell', 'Taylor'], $this->stringable('Otwell, Taylor')->scan('%[^,],%s')->toArray());
944944
$this->assertSame(['filename', 'jpg'], $this->stringable('filename.jpg')->scan('%[^.].%s')->toArray());
945945
}
946+
947+
public function testGet()
948+
{
949+
$this->assertSame('foo', $this->stringable('foo')->value());
950+
$this->assertSame('foo', $this->stringable('foo')->toString());
951+
}
946952
}

0 commit comments

Comments
 (0)