2017 © Pedro Peláez
 

library classnames-php

A simple PHP utility for conditionally joining classNames together

image

xterr/classnames-php

A simple PHP utility for conditionally joining classNames together

  • Monday, August 28, 2017
  • by razvanceana
  • Repository
  • 1 Watchers
  • 0 Stars
  • 7 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

classnames-php

A simple PHP utility for conditionally joining classNames together, (*1)

Build Status, (*2)

PHP port of the JavaScript classNames utility. https://github.com/JedWatson/classnames, (*3)

Inspired by CJStroud/classnames-php, (*4)

Installation

composer require xterr/classnames-php

The classNames can be accessed by using the function defined in xterr\ClassNames\classNames, (*5)

use function xterr\ClassNames\classNames;

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

or by instantiating the class \xterr\ClassNames\ClassNames and using it as a function, (*6)

use \xterr\ClassNames\ClassNames;

$oClassNames = new \xterr\ClassNames\ClassNames;
$oClassNames('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., (*7)

use function xterr\ClassNames\classNames;

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'

Arrays will be recursively flattened as per the rules above:, (*8)

use function xterr\ClassNames\classNames;

$arr = ['b', ['c' => TRUE, 'd' => FALSE]];
classNames('a', arr); // => 'a b c'

Objects will be processed if the __toString() method exists, (*9)

use function xterr\ClassNames\classNames;

class ExampleObject 
{
    function __toString()
    {
        return 'bar';
    }
}

classNames(new ExampleObject()); // => 'bar'

Functions and callables will be processed and should return the same types (string, array, etc) as the arguments accepted by the classNames function. The functions and callables will receive the entire result set as argument., (*10)

use function xterr\ClassNames\classNames;

class ExampleObject
{
    public static function getClasses($aResultSet)
    {
        return ['bar'];
    }

    public function getClassesDynamic($aResultSet)
    {
        return ['baz'];
    }
}

$oObj = new ExampleObject();

classNames(function($aResultSet) { 
    return 'foo'
}, ['ExampleObject', 'getClasses'], [$oObj, 'getClassesDynamic']); // 'foo bar baz'

Dedupe

Dedupes classes and ensures that falsy classes specified in later arguments are excluded from the result set., (*11)

use function xterr\ClassNames\classNames;

classNames('foo', 'foo', 'bar'); // => 'foo bar'
classNames('foo', ['foo' => FALSE, 'bar' => TRUE ]); // => 'bar'

License

MIT. Copyright (c) 2017 Razvan Ceana., (*12)

The Versions

28/08 2017

dev-master

9999999-dev

A simple PHP utility for conditionally joining classNames together

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Ceana Razvan

28/08 2017

v1.0.1

1.0.1.0

A simple PHP utility for conditionally joining classNames together

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Ceana Razvan

24/08 2017

v1.0.0

1.0.0.0

A simple PHP utility for conditionally joining classNames together

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Ceana Razvan