generated from spatie/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
44 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace Mokhosh\Muddle\Strategies\Link; | ||
|
||
use Mokhosh\Muddle\Contracts\LinkStrategy; | ||
use Mokhosh\Muddle\Support\Str; | ||
|
||
class Hex implements LinkStrategy | ||
{ | ||
public function muddle(string $string, string $title): string | ||
{ | ||
$id = Str::id(); | ||
$key = random_int(0, 255); | ||
$hexed = Str::hex('mailto:'.$string, $key); | ||
$script = <<<HTML | ||
</a><script>(function() { | ||
const codes = $id.getAttribute('href').split(' ') | ||
$id.href = '' | ||
for (const code of codes) $id.href = $id.getAttribute('href') + String.fromCharCode(parseInt(code, 16) ^ $key) | ||
})()</script> | ||
HTML; | ||
|
||
return "<a id='$id' href='$hexed'>".$title.$script; | ||
} | ||
|
||
public function unmuddle(string $string): string | ||
{ | ||
preg_match('/ \^ (\d+)/', $string, $key); | ||
preg_match('/href=\'([^\']+)\'>/', $string, $hexed); | ||
preg_match('/>([^<]+)<\/a>/', $string, $title); | ||
|
||
return '<a href="'.Str::unhex($hexed[1], $key[1])."\">$title[1]</a>"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
use Mokhosh\Muddle\Strategies\Link\Hex; | ||
|
||
it('muddles text', function () { | ||
expect($muddled = (new Hex)->muddle('[email protected]', 'email')) | ||
->not->toBe('<a href="mailto:[email protected]">email</a>') | ||
->and((new Hex)->unmuddle($muddled)) | ||
->toBe('<a href="mailto:[email protected]">email</a>'); | ||
}); |