Skip to content

Commit

Permalink
Allow wider types in translate|highlight and subtext methods
Browse files Browse the repository at this point in the history
  • Loading branch information
juniwalk committed May 13, 2024
1 parent acdd413 commit a6394ac
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions src/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,11 @@ public function hasAnyClassOf(string ...$class): bool
}


/**
* @param Stringable|scalar|null $content
*/
public static function highlight(
Stringable|string|null $content,
mixed $content,
Color $color = Color::Primary,
bool $translate = true,
): self {
Expand All @@ -73,8 +76,11 @@ public static function highlight(
}


/**
* @param Stringable|scalar|null $content
*/
public static function subtext(
Stringable|string|null $content,
mixed $content,
Color $color = Color::Secondary,
bool $translate = true,
): self {
Expand Down Expand Up @@ -180,7 +186,7 @@ public static function option(
): self {
$content = static::translate($content, $translate);
$labelHtml = static::translate($label, $translate);
$label = Strings::stripHtml($labelHtml);
$label = Strings::stripHtml($labelHtml ?? '');

if (!$content && $label <> $labelHtml) {
$content = $labelHtml;
Expand Down Expand Up @@ -283,18 +289,21 @@ public static function progressBar(float $percent, Color $color): self
}


private static function translate(Stringable|string|null $text, bool $translate = true): Stringable|string
/**
* @param Stringable|scalar|null $value
*/
private static function translate(mixed $value, bool $translate = true): null|string|Stringable
{
$text ??= '';
$value = strval($value) ?: null;

if (!$translate || static::$disableTranslation || !static::$translator) {
return $text;
return $value;
}

if (!$text || !Strings::match((string) $text, static::TranslationRegEx)) {
return $text;
if (!$value || !Strings::match($value, static::TranslationRegEx)) {
return $value;
}

return static::$translator->translate($text);
return static::$translator->translate($value);
}
}

0 comments on commit a6394ac

Please sign in to comment.