Skip to content

Commit

Permalink
refactor to fluent stringable
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonvarga committed Oct 11, 2023
1 parent 09ad1ca commit 48cbe53
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions src/Facades/Endpoint/Pattern.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Statamic\Facades\Endpoint;

use Statamic\Support\Str;

/**
* Regular expressions, et al.
*/
Expand Down Expand Up @@ -110,9 +112,11 @@ public function isUUID($value)
*
* Similar to the preg_quote method for regular expressions.
*/
public function sqlLikeQuote(string $string): string
public function sqlLikeQuote(string $like): string
{
return str_replace(['%', '_'], ['\%', '\_'], $string);
return Str::of($like)
->replace('%', '\%')
->replace('_', '\_');
}

/**
Expand All @@ -122,13 +126,15 @@ public function sqlLikeQuote(string $string): string
*/
public function sqlLikeToRegex(string $like): string
{
$pattern = str_replace('\_', $underscore = str_random(), $like);
$pattern = str_replace('\%', $percent = str_random(), $pattern);
$pattern = preg_quote($pattern, '/');
$pattern = str_replace(['%', '_'], ['.*', '.'], $pattern);
$pattern = str_replace($underscore, '_', $pattern);
$pattern = str_replace($percent, '%', $pattern);

return '^'.$pattern.'$';
return Str::of($like)
->replace('\_', $underscore = Str::random())
->replace('\%', $percent = Str::random())
->pipe(fn ($str) => preg_quote($str, '/'))
->replace('%', '.*')
->replace('_', '.')
->replace($underscore, '_')
->replace($percent, '%')
->prepend('^')
->append('$');
}
}

0 comments on commit 48cbe53

Please sign in to comment.