Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit a720cbe

Browse filesBrowse files
[12.x] Add hash string helper (#55767)
* feat: add `hash` method to `Stringable` * fix: cs * formatting --------- Co-authored-by: Taylor Otwell <taylor@laravel.com>
1 parent 40edd68 commit a720cbe
Copy full SHA for a720cbe

File tree

Expand file treeCollapse file tree

2 files changed

+18
-0
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+18
-0
lines changed

‎src/Illuminate/Support/Stringable.php

Copy file name to clipboardExpand all lines: src/Illuminate/Support/Stringable.php
+11Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1335,6 +1335,17 @@ public function fromBase64($strict = false)
13351335
return new static(base64_decode($this->value, $strict));
13361336
}
13371337

1338+
/**
1339+
* Hash the string using the given algorithm.
1340+
*
1341+
* @param string $algorithm
1342+
* @return static
1343+
*/
1344+
public function hash(string $algorithm)
1345+
{
1346+
return new static(hash($algorithm, $this->value));
1347+
}
1348+
13381349
/**
13391350
* Dump the string.
13401351
*

‎tests/Support/SupportStringableTest.php

Copy file name to clipboardExpand all lines: tests/Support/SupportStringableTest.php
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1419,4 +1419,11 @@ public function testFromBase64()
14191419
$this->assertSame('foobar', (string) $this->stringable(base64_encode('foobar'))->fromBase64(true));
14201420
$this->assertSame('foobarbaz', (string) $this->stringable(base64_encode('foobarbaz'))->fromBase64());
14211421
}
1422+
1423+
public function testHash()
1424+
{
1425+
$this->assertSame(hash('xxh3', 'foo'), (string) $this->stringable('foo')->hash('xxh3'));
1426+
$this->assertSame(hash('xxh3', 'foobar'), (string) $this->stringable('foobar')->hash('xxh3'));
1427+
$this->assertSame(hash('sha256', 'foobarbaz'), (string) $this->stringable('foobarbaz')->hash('sha256'));
1428+
}
14221429
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.