ecailles/dom-class-name

A DOM class name generation library (BEM syntax class name builder etc.)

v1.0.0 2016-01-30 03:15 UTC

This package is auto-updated.

Last update: 2024-11-29 04:46:58 UTC


README

Latest Stable Version Latest Unstable Version License Build Status Coverage Status

Install via Composer

composer require ecailles/dom-class-name

Basic Usage

Block

use Ecailles\DomClassName\Bem\Bem;

// 'block--block-modifier'
$classname = (new Bem())
    ->block('block')->modifier('block-modifier')
    ->value();

Element

use Ecailles\DomClassName\Bem\Bem;

// 'block--block-modifier__element--element-modifier'
$classname = (new Bem())
    ->block('block')->modifier('block-modifier')
    ->element('element')->modifier('element-modifier')
    ->value();

Classes

use Ecailles\DomClassName\Bem\Bem;

// 'block--block-modifier__element--element-modifier class1 class2'
$classname = (new Bem())
    ->block('block')->modifier('block-modifier')
    ->element('element')->modifier('element-modifier')
    ->class(['class1', 'class2'])
    ->value();

Return Value

Class names as array

use Ecailles\DomClassName\Bem\Bem;

// ['block--block-modifier__element--element-modifier', 'class1', 'class2']
$classnames = (new Bem())
    ->block('block')->modifier('block-modifier')
    ->element('element')->modifier('element-modifier')
    ->class(['class1', 'class2'])
    ->get();

Class names as string

use Ecailles\DomClassName\Bem\Bem;

// 'block--block-modifier__element--element-modifier class1 class2'
$classname = (new Bem())
    ->block('block')->modifier('block-modifier')
    ->element('element')->modifier('element-modifier')
    ->class(['class1', 'class2'])
    ->value();

Class names as string (implicit type conversion)

use Ecailles\DomClassName\Bem\Bem;

$bem = (new Bem())->block('block')->class('class');

// 'block class'
echo htmlspecialchars($bem, ENT_QUOTES, 'UTF-8');

Explicit Modifiers

use Ecailles\DomClassName\Bem\Bem;

// 'block--block-modifier__element--element-modifier'
$classname = (new Bem())
    ->blockModifier('block-modifier')
    ->elementModifier('element-modifier')
    ->block('block')->element('element')
    ->value();

Cloning (PHP 5.6 or earlier are also supported)

use Ecailles\DomClassName\Bem\Bem;

$block = (new Bem())->block('block');

// 'block__element1'
$element1 = $block->clone()->element('element1')->value();

// 'block__element2'
$element2 = $block->clone()->element('element2')->value();

Prefixing

use Ecailles\DomClassName\Bem\Bem;

// 'prefix-block'
$classname = (new Bem())->prefix('prefix')->block('block')->value();

or

use Ecailles\DomClassName\Bem\Bem;

// 'prefix-block'
$classname = (new Bem('prefix'))->block('block')->value();

Custom Separators

Prefix Separator

use Ecailles\DomClassName\Bem\Bem;

// 'prefix__block'
$classname = (new Bem())->prefix('prefix')->prefixSeparator('__')
    ->block('block')->value();

or

use Ecailles\DomClassName\Bem\Bem;

// 'prefix__block'
$classname = (new Bem('prefix', '__'))->block('block')->value();

Element Separator

use Ecailles\DomClassName\Bem\Bem;

// 'block-element'
$classname = (new Bem())->elementSeparator('-')
    ->block('block')->element('element')->value();

or

use Ecailles\DomClassName\Bem\Bem;

// 'block-element'
$classname = (new Bem(null, null, '-'))
    ->block('block')->element('element')->value();

Modifier Separator

use Ecailles\DomClassName\Bem\Bem;

// 'block-modifier'
$classname = (new Bem())->modifierSeparator('-')
    ->block('block')->element('modifier')->value();

or

use Ecailles\DomClassName\Bem\Bem;

// 'block-modifier'
$classname = (new Bem(null, null, null, '-'))
    ->block('block')->modifier('modifier')->value();