An API to support CSS class names.

v2.0 2014-01-26 11:54 UTC


An API to support CSS class names.



namespace Brickrouge;

$class_names = [

    'node-id' => 'node-id-13',
    'node-slug' => 'node-slug-example',
    'is-active' => true,
    'is-disabled' => false


// "node-id-13 node-slug-example is-active"
render_css_class($class_names, [ 'node-id', 'is-active', 'is-disabled' ]);
// "node-id-13 is-active"
render_css_class($class_names, 'node-id is-active is-disabled');
// "node-id-13 is-active"
render_css_class($class_names, [ '-node-id', '-node-slug' ]);
// "is-active"
render_css_class($class_names, '-node-id -node-slug');
// "is-active"

CSSClassNames and CSSClassNamesProperty

Classes that implements the CSSClassNames interface might want to use the CSSClassNamesProperty trait that provides support for the css_class and css_class_names magic properties.


namespace Icybee\Modules\Nodes;

use Brickrouge\CSSClassNames;
use Brickrouge\CSSClassNamesProperty;

// …

class Node extends ActiveRecord implements CSSClassNames
    use CSSClassNamesProperty;

    // …

     * Returns the CSS class names of the node.
     * @return array[string]mixed
    protected function get_css_class_names()
        $nid = $this->nid;
        $slug = $this->slug;

        return [

            'type' => 'node',
            'id' => $nid ? "node-{$nid}" : null,
            'slug' => $slug ? "node-slug-{$slug}" : null,
            'constructor' => 'constructor-' . \ICanBoogie\normalize($this->constructor)


An instance of such a Node class could be used as follows:


// …

// node node-123 node-slug-example constructor-nodes
// [ 'type' => node, 'id' => 'node-123', 'slug' => 'node-slug-example', 'constructor' => 'constructor-nodes' ]
$node->css_class('-slug -constructor');
// node node-123
$node->css_class('id slug');
// node-123 node-slug-example


The package requires PHP 5.4 or later.


The recommended way to install this package is through Composer. Create a composer.json file and run php composer.phar install command to install it:

    "minimum-stability": "dev",
        "brickrouge/css-class-names": "*"

Cloning the repository

The package is available on GitHub, its repository can be cloned with the following command line:

$ git clone git://


The documentation for the package and its dependencies can be generated with the make doc command. The documentation is generated in the docs directory using ApiGen. The package directory can later by cleaned with the make clean command.


The test suite is ran with the make test command. Composer is automatically installed as well as all the dependencies required to run the suite. The package directory can later be cleaned with the make clean command.

The package is continuously tested by Travis CI.

Build Status


Brickrouge/CSSClassNames is licensed under the New BSD License - See the LICENSE file for details.