Skip to content

Commit

Permalink
Enh #22: Added FA:ul() method.
Browse files Browse the repository at this point in the history
Refactoring, updated tests.
  • Loading branch information
rmrevin committed Aug 31, 2016
1 parent 23e282e commit 478907d
Show file tree
Hide file tree
Showing 6 changed files with 194 additions and 13 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
2016-08-31 - 2.16.0
-------------------
* Enh #22: Added FA:ul() method.

2016-08-19 - 2.15.2
-------------------
* Update icon name constants to version 4.6.3.
Expand Down
9 changes: 9 additions & 0 deletions FontAwesome.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@ public static function s($options = [])
return static::stack($options);
}

/**
* @param array $options
* @return component\UnorderedList
*/
public static function ul($options = [])
{
return new component\UnorderedList($options);
}

/**
* Size values
* @see rmrevin\yii\fontawesome\component\Icon::size
Expand Down
15 changes: 11 additions & 4 deletions component/Icon.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,19 @@
class Icon
{

/** @var string */
/**
* @var string
*/
public static $defaultTag = 'i';

/** @var string */
/**
* @var string
*/
private $tag;

/** @var array */
/**
* @var array
*/
private $options = [];

/**
Expand Down Expand Up @@ -84,6 +90,7 @@ public function fixedWidth()
}

/**
* @deprecated
* @return self
*/
public function ul()
Expand Down Expand Up @@ -253,4 +260,4 @@ public function render($tag = null, $content = null, $options = [])

return Html::tag($tag, $content, $options);
}
}
}
29 changes: 20 additions & 9 deletions component/Stack.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace rmrevin\yii\fontawesome\component;

use rmrevin\yii\fontawesome\FA;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;

Expand All @@ -17,27 +18,37 @@
class Stack
{

/** @var string */
/**
* @var string
*/
public static $defaultTag = 'span';

/** @var string */
/**
* @var string
*/
private $tag;

/** @var array */
/**
* @var array
*/
private $options = [];

/** @var Icon */
/**
* @var Icon
*/
private $icon_front;

/** @var Icon */
/**
* @var Icon
*/
private $icon_back;

/**
* @param array $options
*/
public function __construct($options = [])
{
Html::addCssClass($options, 'fa-stack');
Html::addCssClass($options, FA::$cssPrefix . '-stack');

$this->options = $options;
}
Expand Down Expand Up @@ -112,11 +123,11 @@ public function render($tag = null, $options = [])
$template = ArrayHelper::remove($options, 'template', '{back}{front}');

$icon_back = $this->icon_back instanceof Icon
? $this->icon_back->addCssClass('fa-stack-2x')
? $this->icon_back->addCssClass(FA::$cssPrefix . '-stack-2x')
: null;

$icon_front = $this->icon_front instanceof Icon
? $this->icon_front->addCssClass('fa-stack-1x')
? $this->icon_front->addCssClass(FA::$cssPrefix . '-stack-1x')
: null;

return Html::tag(
Expand All @@ -125,4 +136,4 @@ public function render($tag = null, $options = [])
$options
);
}
}
}
108 changes: 108 additions & 0 deletions component/UnorderedList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php
/**
* UnorderedList.php
* @author Revin Roman
* @link https://rmrevin.com
*/

namespace rmrevin\yii\fontawesome\component;

use rmrevin\yii\fontawesome\FA;
use yii\helpers\Html;

/**
* Class UnorderedList
* @package rmrevin\yii\fontawesome\component
*/
class UnorderedList
{

/**
* @var string
*/
public static $defaultTag = 'ul';

/**
* @var string
*/
private $tag;

/**
* @var array
*/
private $options = [];

/**
* @var array
*/
private $items = [];

/**
* @param array $options
*/
public function __construct($options = [])
{
Html::addCssClass($options, FA::$cssPrefix . '-ul');

$this->options = $options;
}

/**
* @return string
*/
public function __toString()
{
return $this->render();
}

/**
* @param string|Icon $icon
* @param string|null $label
* @param array $options
* @return static
*/
public function item($icon, $label = null, $options = [])
{
if (is_string($icon)) {
$icon = new Icon($icon, $options);
}

$content = trim((string)$icon->li() . $label);

$this->items[] = Html::tag('li', $content);

return $this;
}

/**
* Change html tag.
* @param string $tag
* @return static
* @throws \yii\base\InvalidParamException
*/
public function tag($tag)
{
$this->tag = $tag;

return $this;
}

/**
* @param string|null $tag
* @param array $options
* @return string
* @throws \yii\base\InvalidConfigException
*/
public function render($tag = null, $options = [])
{
$tag = empty($tag)
? (empty($this->tag) ? static::$defaultTag : $this->tag)
: $tag;

$options = array_merge($this->options, $options);

$items = $this->items;

return Html::tag($tag, implode($items), $options);
}
}
42 changes: 42 additions & 0 deletions tests/unit/fontawesome/MainTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,35 @@ public function testStackOutput()
);
}

public function testUlOutput()
{
$this->assertEquals(
(string)FA::ul(),
'<ul class="fa-ul"></ul>'
);

$this->assertEquals(
(string)FA::ul()
->item('cog', 'Gear'),
'<ul class="fa-ul"><li><i class="fa fa-cog fa-li"></i>Gear</li></ul>'
);

$this->assertEquals(
(string)FA::ul()
->item('check', 'Check')
->item('cog', 'Gear'),
'<ul class="fa-ul"><li><i class="fa fa-check fa-li"></i>Check</li><li><i class="fa fa-cog fa-li"></i>Gear</li></ul>'
);

$this->assertEquals(
(string)FA::ul()
->tag('ol')
->item('check', 'Check')
->item('cog', 'Gear'),
'<ol class="fa-ul"><li><i class="fa fa-check fa-li"></i>Check</li><li><i class="fa fa-cog fa-li"></i>Gear</li></ol>'
);
}

public function testAnotherPrefix()
{
$old_prefix = FA::$cssPrefix;
Expand All @@ -107,6 +136,19 @@ public function testAnotherPrefix()
$this->assertEquals(FA::icon('cog')->tag('span'), '<span class="fontawesome fontawesome-cog"></span>');
$this->assertEquals(FA::icon('cog')->addCssClass('highlight'), '<i class="fontawesome fontawesome-cog highlight"></i>');

$this->assertEquals(
(string)FA::stack()
->icon(FA::Icon('cog')->spin())
->on(FA::Icon('square-o')->size(FA::SIZE_3X)),
'<span class="fontawesome-stack"><i class="fontawesome fontawesome-square-o fontawesome-3x fontawesome-stack-2x"></i><i class="fontawesome fontawesome-cog fontawesome-spin fontawesome-stack-1x"></i></span>'
);

$this->assertEquals(
(string)FA::ul()
->item('cog', 'Gear'),
'<ul class="fontawesome-ul"><li><i class="fontawesome fontawesome-cog fontawesome-li"></i>Gear</li></ul>'
);

FA::$cssPrefix = $old_prefix;
}

Expand Down

0 comments on commit 478907d

Please sign in to comment.