Skip to content

Commit

Permalink
make getElementName() method variadic & add phpunit test for it (#1)
Browse files Browse the repository at this point in the history
* make getElementName() method variadic & add phpunit test for it

*  - implemented the same variadic method for adding modifiers
 - added phpunit test

*  - Added a short description to each docbloc function
 - fix the spacing on the variadic functions
  • Loading branch information
aktorou authored and bajb committed Jul 11, 2019
1 parent 0c40004 commit c1e3d49
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
6 changes: 6 additions & 0 deletions src/BemComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,11 @@

interface BemComponent
{
/**
* This is the root BEM class that all the other elements
* and modifiers will use as a base
*
* @return string
*/
public function getBlockName(): string;
}
41 changes: 36 additions & 5 deletions src/BemComponentTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,50 @@

trait BemComponentTrait
{
/**
* This is the root BEM class that all the other elements
* and modifiers will use as a base
*
* @return string
*/
abstract public function getBlockName(): string;

public function getModifier(string $modifier, string $element = null): string
/**
* Modifiers are used to dentote a modififaction of the component
* or specific elements inside.
*
* @param string $modifier
* @param string ...$elements
*
* @return string
*/
public function getModifier(string $modifier, string ...$elements): string
{
if($element !== null)
if($elements)
{
return $this->getElementName($element) . '--' . $modifier;
return $this->getElementName(...$elements) . '--' . $modifier;
}

return $this->getBlockName() . '--' . $modifier;
}

public function getElementName(string $element): string
/**
* Element classes are used to style specfic sections within
* a component while still being scoped
*
* @param string ...$elements
*
* @return string
*/
public function getElementName(string ...$elements): string
{
return $this->getBlockName() . '__' . $element;
$class = $this->getBlockName();

foreach($elements as $element)
{
$class .= '__' . $element;
}

return $class;
}
}
2 changes: 2 additions & 0 deletions tests/BemComponentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ public function testElementName()
{
$c = new TestBemComponent();
$this->assertEquals('p-block__input', $c->getElementName('input'));
$this->assertEquals('p-block__input__inner', $c->getElementName('input', 'inner'));
}

public function testModifier()
{
$c = new TestBemComponent();
$this->assertEquals('p-block--light', $c->getModifier('light'));
$this->assertEquals('p-block__input--light', $c->getModifier('light', 'input'));
$this->assertEquals('p-block__input__inner--light', $c->getModifier('light', 'input', 'inner'));
}
}

0 comments on commit c1e3d49

Please sign in to comment.