Skip to content

Commit

Permalink
🎉 Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Stroud committed Apr 29, 2017
1 parent 5ae00b5 commit 0ed8df4
Show file tree
Hide file tree
Showing 7 changed files with 1,691 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea/

/vendor/
56 changes: 55 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,56 @@
# classnames-php
PHP port of classnames
PHP port of the JavaScript classNames utility. https://github.com/JedWatson/classnames

## Installation

```
composer require cjstroud/classnames-php
```

The classNames can be accessed anywhere.

```
classNames('foo', ['bar' => true]); // 'foo bar'
```

## Usage

The `classNames` function takes any number of arguments which can be a string or array.
When using an array, if the value associated with a given key is falsy, that key won't be included in the output.
If no value is given the true is assumed.

```
classNames('foo'); // 'foo'
classNames(['foo' => true]); // 'foo'
classNames('foo', ['bar' => false, 'baz' => true]); // 'foo baz'
classNames(['foo', 'bar' => true]) // 'foo bar'
// Falsy values get ignored
classNames('foo', null, 0, false, 1); // 'foo 1'
```

Objects and functions will be ignored, unless the object has __toString() function.
If it does that will be called and the string value used.

```
class ExampleObject {
function __toString()
{
return 'bar';
}
}
classNames('foo', function() {}, new stdClass(), new ExampleObject()); // 'foo bar'
```

## Laravel Blade

```
<div class="{{ classNames('foo', ['bar' => true]) }}"></div>
<div class="foo bar"></div>
```

## License

[MIT](LICENSE). Copyright (c) 2017 Chris Stroud.
20 changes: 20 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "cjstroud/classnames-php",
"description": "A simple PHP utility for conditionally joining classNames together",
"require-dev": {
"phpunit/phpunit": "^6.1"
},
"license": "MIT",
"autoload": {
"files": [
"src/ClassNames.php"
]
},
"authors": [
{
"name": "Chris Stroud",
"email": "chris.stroud00@gmail.com"
}
],
"require": {}
}
Loading

0 comments on commit 0ed8df4

Please sign in to comment.