Skip to content

Commit

Permalink
Simplify class modifier function
Browse files Browse the repository at this point in the history
  • Loading branch information
nanaya committed Nov 2, 2023
1 parent 848f059 commit 1f1e829
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions app/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,33 +203,37 @@ function captcha_login_triggered()
return $triggered;
}

function class_modifiers_each(array $modifiersArray, callable $callback)
function class_modifiers_flat(array $modifiersArray): array
{
$ret = [];

foreach ($modifiersArray as $modifiers) {
if (is_array($modifiers)) {
// either "$modifier => boolean" or "$i => $modifier|null"
foreach ($modifiers as $k => $v) {
if (is_bool($v)) {
if ($v) {
$callback($k);
$ret[] = $k;
}
} elseif ($v !== null) {
$callback($v);
$ret[] = $v;
}
}
} elseif (is_string($modifiers)) {
$callback($modifiers);
$ret[] = $modifiers;
}
}

return $ret;
}

function class_with_modifiers(string $className, ...$modifiersArray)
function class_with_modifiers(string $className, ...$modifiersArray): string
{
$class = $className;

class_modifiers_each($modifiersArray, function ($m) use (&$class, $className) {
foreach (class_modifiers_flat($modifiersArray) as $m) {
$class .= " {$className}--{$m}";
});
}

return $class;
}
Expand Down

0 comments on commit 1f1e829

Please sign in to comment.