Skip to content

Commit 0ed8df4

Browse files
author
Chris Stroud
committed
🎉 Initial commit
1 parent 5ae00b5 commit 0ed8df4

File tree

7 files changed

+1691
-1
lines changed

7 files changed

+1691
-1
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.idea/
2+
3+
/vendor/

README.md

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,56 @@
11
# classnames-php
2-
PHP port of classnames
2+
PHP port of the JavaScript classNames utility. https://github.com/JedWatson/classnames
3+
4+
## Installation
5+
6+
```
7+
composer require cjstroud/classnames-php
8+
```
9+
10+
The classNames can be accessed anywhere.
11+
12+
```
13+
classNames('foo', ['bar' => true]); // 'foo bar'
14+
```
15+
16+
## Usage
17+
18+
The `classNames` function takes any number of arguments which can be a string or array.
19+
When using an array, if the value associated with a given key is falsy, that key won't be included in the output.
20+
If no value is given the true is assumed.
21+
22+
```
23+
classNames('foo'); // 'foo'
24+
classNames(['foo' => true]); // 'foo'
25+
classNames('foo', ['bar' => false, 'baz' => true]); // 'foo baz'
26+
classNames(['foo', 'bar' => true]) // 'foo bar'
27+
28+
// Falsy values get ignored
29+
classNames('foo', null, 0, false, 1); // 'foo 1'
30+
```
31+
32+
Objects and functions will be ignored, unless the object has __toString() function.
33+
If it does that will be called and the string value used.
34+
35+
```
36+
class ExampleObject {
37+
function __toString()
38+
{
39+
return 'bar';
40+
}
41+
}
42+
43+
classNames('foo', function() {}, new stdClass(), new ExampleObject()); // 'foo bar'
44+
```
45+
46+
## Laravel Blade
47+
48+
```
49+
<div class="{{ classNames('foo', ['bar' => true]) }}"></div>
50+
51+
<div class="foo bar"></div>
52+
```
53+
54+
## License
55+
56+
[MIT](LICENSE). Copyright (c) 2017 Chris Stroud.

composer.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "cjstroud/classnames-php",
3+
"description": "A simple PHP utility for conditionally joining classNames together",
4+
"require-dev": {
5+
"phpunit/phpunit": "^6.1"
6+
},
7+
"license": "MIT",
8+
"autoload": {
9+
"files": [
10+
"src/ClassNames.php"
11+
]
12+
},
13+
"authors": [
14+
{
15+
"name": "Chris Stroud",
16+
"email": "[email protected]"
17+
}
18+
],
19+
"require": {}
20+
}

0 commit comments

Comments
 (0)