Skip to content

Commit

Permalink
fix: simplify the fix
Browse files Browse the repository at this point in the history
  • Loading branch information
tamtamchik committed Aug 6, 2024
1 parent 9c6ae8b commit 529edbd
Showing 1 changed file with 6 additions and 31 deletions.
37 changes: 6 additions & 31 deletions src/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,6 @@ public static function nameCase(?string $name = '', ?array $options = []): strin

self::setOptions($options);

// Temporarily replace HTML encoded entities with placeholders
$placeholders = self::replaceHtmlEntitiesWithPlaceholders($name);

// Do not do anything if string is mixed and lazy option is true.
if ( ! self::canBeProcessed($name)) {
return $name;
Expand All @@ -219,8 +216,7 @@ public static function nameCase(?string $name = '', ?array $options = []): strin

self::processOptions($name);

// After name casing operations, restore HTML encoded entities
self::restoreHtmlEntitiesFromPlaceholders($name, $placeholders);
self::adjustHTMLEntities($name);

return $name;
}
Expand Down Expand Up @@ -421,35 +417,14 @@ private static function fixPostNominal(string &$name): void
}

/**
* Replace HTML entities with placeholders.
* Decode HTML entities.
*
* @param string $name
* @return array
*/
private static function replaceHtmlEntitiesWithPlaceholders(string &$name): array {
$placeholders = [];
$counter = 0;

$name = preg_replace_callback('/&[a-zA-Z0-9#]+;/i', function($matches) use (&$placeholders, &$counter) {
$placeholder = mb_strtolower('HTML_ENTITY_PLACEHOLDER_' . $counter++. ' '); // note space at the end, to avoid merging with the next word
$placeholders[$placeholder] = $matches[0];
return $placeholder;
private static function adjustHTMLEntities(string &$name): void
{
$name = mb_ereg_replace_callback('&[a-zA-Z0-9#]+;', function ($matches) {
return mb_strtolower($matches[0]);
}, $name);

return $placeholders;
}

/**
* Restore HTML entities.
*
* @param string $name
* @param array $placeholders
* @return void
*/
private static function restoreHtmlEntitiesFromPlaceholders(string &$name, array $placeholders): void {
foreach ($placeholders as $placeholder => $entity) {
$name = preg_replace('/' . preg_quote($placeholder, '/') . '/i', $entity, $name);
}
}

}

0 comments on commit 529edbd

Please sign in to comment.