newridetech/php-classnames

1.2.0 2018-07-01 23:28 UTC

This package is not auto-updated.

Last update: 2024-09-15 05:02:54 UTC


README

此包是https://github.com/JedWatson/classnames的PHP版本。

唯一的不同是,Classnames默认会去重类名。这意味着Classnames::make('foo', 'foo', 'bar')默认返回'foo bar'而不是foo foo bar。这是因为PHP中的重绘不像JavaScript(原始实现)中那样频繁,所以这不是一个问题,而且输出的代码更干净、更小(这在后端是一个更大的问题)。

如果您想在不去重的情况下使用Classnames,可以调用Classnames::flatten

生成CSS类名

use Newride\Classnames\Classnames;

Classnames::make('foo', 'bar'); // => 'foo bar'
Classnames::make('foo', [ 'bar' => true ]); // => 'foo bar'
Classnames::make([ 'foo-bar' => true ]); // => 'foo-bar'
Classnames::make([ 'foo-bar' => false ]); // => ''
Classnames::make([ 'foo' => true ], [ 'bar' => true ]); // => 'foo bar'
Classnames::make([ 'foo' => true, 'bar' => true ]); // => 'foo bar'
Classnames::make(1, 2, 3); // => '1 2 3'

Classnames::make('foo', 'foo', 'bar'); // => 'foo bar'
Classnames::flatten('foo', 'foo', 'bar'); // => 'foo foo bar'

无效类名

当使用无法转换为字符串的类型作为参数时,会抛出异常。

Classnames::flatten(new stdClass()); // => throws \Newride\Classnames\NotConvertibleTypeException