Skip to content

Instantly share code, notes, and snippets.

@mcsee
Last active August 26, 2023 00:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcsee/4eb63e5acfcdda39b24e3f6a73eb0bdb to your computer and use it in GitHub Desktop.
Save mcsee/4eb63e5acfcdda39b24e3f6a73eb0bdb to your computer and use it in GitHub Desktop.
<?
final class TextReplacer {
function replace(
string $patternToFind,
string $textToReplace,
string $subject,
string $replaceFunctionName,
$postProcessClosure) {
return $postProcessClosure(
$replaceFunctionName($patternToFind, $textToReplace, $subject));
}
}
// Lots of tests on text replacer so you can gain confidence.
final class WordProcessor {
function replaceText(string $patternToFind, string $textToReplace) {
$this->text = (new TextReplacer())->replace(
$patternToFind,
$textToReplace,
$this->text,
'str_replace', fn($text) => '<<<' . $text . '>>>');
}
}
final class Obfuscator {
function obfuscate(string $patternToFind, string $textToReplace) {
$this->text = (new TextReplacer())->replace(
$patternToFind,
$textToReplace,
$this->text,
'str_ireplace', fn($text) => strlower($text));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment