Skip to content

Commit

Permalink
新增classnames函数 css样式名生成器
Browse files Browse the repository at this point in the history
  • Loading branch information
yunwuxin committed Apr 5, 2017
1 parent ed64408 commit 0c99dc6
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,30 @@ function trait_uses_recursive($trait)

return $traits;
}
}
if (!function_exists('classnames')) {
/**
* css样式名生成器
* classnames("foo", "bar"); // => "foo bar"
* classnames("foo", [ "bar"=> true ]); // => "foo bar"
* classnames([ "foo-bar"=> true ]); // => "foo-bar"
* classnames([ "foo-bar"=> false ]); // => "
* classnames([ "foo" => true ], [ "bar"=> true ]); // => "foo bar"
* classnames([ "foo" => true, "bar"=> true ]); // => "foo bar"
* classnames("foo", [ "bar"=> true, "duck"=> false ], "baz", [ "quux"=> true ]); // => "foo bar baz quux"
* classnames(null, false, "bar", 0, 1, [ "baz"=> null ]); // => "bar 1"
*/
function classnames()
{
$args = func_get_args();
$classes = array_map(function ($arg) {
if (is_array($arg)) {
return implode(" ", array_filter(array_map(function ($expression, $class) {
return $expression ? $class : false;
}, $arg, array_keys($arg))));
}
return $arg;
}, $args);
return implode(" ", array_filter($classes));
}
}

0 comments on commit 0c99dc6

Please sign in to comment.