Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Resources/config/services/twig.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

<parameters>
<parameter key="braincrafted_bootstrap.twig.icon_extension.class">Braincrafted\Bundle\BootstrapBundle\Twig\BootstrapIconExtension</parameter>
<parameter key="braincrafted_bootstrap.twig.button_extension.class">Braincrafted\Bundle\BootstrapBundle\Twig\BootstrapButtonExtension</parameter>
<parameter key="braincrafted_bootstrap.twig.label_extension.class">Braincrafted\Bundle\BootstrapBundle\Twig\BootstrapLabelExtension</parameter>
<parameter key="braincrafted_bootstrap.twig.badge_extension.class">Braincrafted\Bundle\BootstrapBundle\Twig\BootstrapBadgeExtension</parameter>
<parameter key="braincrafted_bootstrap.twig.form_extension.class">Braincrafted\Bundle\BootstrapBundle\Twig\BootstrapFormExtension</parameter>
Expand All @@ -19,6 +20,11 @@
<argument>%braincrafted_bootstrap.icon_tag%</argument>
</service>

<service id="braincrafted_bootstrap.twig.button_extension" class="%braincrafted_bootstrap.twig.button_extension.class%">
<argument type="service" id="braincrafted_bootstrap.twig.icon_extension" />
<tag name="twig.extension" />
</service>

<service id="braincrafted_bootstrap.twig.label_extension" class="%braincrafted_bootstrap.twig.label_extension.class%">
<tag name="twig.extension" />
</service>
Expand Down
136 changes: 136 additions & 0 deletions Tests/Twig/BootstrapButtonExtensionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
<?php

/**
* This file is part of BraincraftedBootstrapBundle.
*
* (c) 2012-2013 by Florian Eckerstorfer
*/

namespace Braincrafted\Bundle\BootstrapBundle\Tests\Twig;

use Braincrafted\Bundle\BootstrapBundle\Twig\BootstrapButtonExtension;
use Braincrafted\Bundle\BootstrapBundle\Twig\BootstrapIconExtension;

/**
* BootstrapBadgeExtensionTest
*
* This test is only useful if you consider that it will be run by Travis on every supported PHP
* configuration. We live in a world where should not have too manually test every commit with every
* version of PHP. And I know exactly that I will commit short array syntax all the time and break
* compatibility with PHP 5.3
*
* @category Test
* @package BraincraftedBootstrapBundle
* @subpackage Twig
* @author Florian Eckerstorfer <[email protected]>
* @copyright 2012-2013 Florian Eckerstorfer
* @license http://opensource.org/licenses/MIT The MIT License
* @link http://bootstrap.braincrafted.com Bootstrap for Symfony2
* @group unit
*/
class BootstrapButtonExtensionTest extends \PHPUnit_Framework_TestCase
{
/** @var BootstrapButtonExtension */
private $extension;

/**
* Set up
*/
public function setUp()
{
$iconExtension = new BootstrapIconExtension('fa', 'i');
$this->extension = new BootstrapButtonExtension($iconExtension);
}

/**
* @covers Braincrafted\Bundle\BootstrapBundle\Twig\BootstrapButtonExtension::getFunctions()
*/
public function testGetFunctions()
{
$this->assertCount(2, $this->extension->getFunctions());
}

/**
* @covers Braincrafted\Bundle\BootstrapBundle\Twig\BootstrapButtonExtension::buttonFunction
*/
public function testButtonFunctionWithDefaults()
{
$this->assertEquals(
'<button class="btn btn-default btn-md" type="button"></button>',
$this->extension->buttonFunction(),
'->buttonFunction() returns the HTML code for the given button.'
);
}

/**
* @covers Braincrafted\Bundle\BootstrapBundle\Twig\BootstrapButtonExtension::buttonFunction
*/
public function testButtonFunctionWithOptions()
{
$options = array(
'label' => 'Test',
'icon' => 'check',
'type' => 'success',
'size' => 'sm',
'submit' => true,
'attr' => array(
'id' => 'test_button',
'class' => 'my-class',
'title' => 'Test',
'data-confirm' => 'Are you sure?'
),
);

$this->assertEquals(
'<button id="test_button" class="btn btn-success btn-sm my-class" title="Test" data-confirm="Are you sure?" type="submit"><i class="fa fa-check"></i> Test</button>',
$this->extension->buttonFunction($options),
'->buttonFunction() returns the HTML code for the given button.'
);
}

/**
* @covers Braincrafted\Bundle\BootstrapBundle\Twig\BootstrapButtonExtension::buttonLinkFunction
*/
public function testButtonLinkFunctionWithDefaults()
{
$this->assertEquals(
'<a class="btn btn-default btn-md" href="#"></a>',
$this->extension->buttonLinkFunction(),
'->buttonFunction() returns the HTML code for the given button.'
);
}

/**
* @covers Braincrafted\Bundle\BootstrapBundle\Twig\BootstrapButtonExtension::buttonLinkFunction
*/
public function testButtonLinkFunctionWithOptions()
{
$options = array(
'label' => 'Test',
'icon' => 'check',
'type' => 'success',
'size' => 'sm',
'attr' => array(
'id' => 'test_button',
'class' => 'my-class',
'href' => 'example.com',
'title' => 'Test',
'data-confirm' => 'Are you sure?'
),
);

$this->assertEquals(
'<a id="test_button" class="btn btn-success btn-sm my-class" href="example.com" title="Test" data-confirm="Are you sure?"><i class="fa fa-check"></i> Test</a>',
$this->extension->buttonLinkFunction($options),
'->buttonFunction() returns the HTML code for the given button.'
);
}

/**
* @covers Braincrafted\Bundle\BootstrapBundle\Twig\BootstrapButtonExtension::getName()
*/
public function testGetName()
{
$this->assertEquals('braincrafted_bootstrap_button', $this->extension->getName());
}
}
97 changes: 97 additions & 0 deletions Twig/BootstrapButtonExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

/**
* @author Damian Dlugosz <[email protected]>
*/

namespace Braincrafted\Bundle\BootstrapBundle\Twig;

class BootstrapButtonExtension extends \Twig_Extension
{
/**
* @var BootstrapIconExtension
*/
private $iconExtension;

private $defaults = array(
'label' => '',
'icon' => false,
'type' => 'default',
'size' => 'md',
'attr' => array(),
);

/**
* @param BootstrapIconExtension $iconExtension
*/
public function __construct(BootstrapIconExtension $iconExtension)
{
$this->iconExtension = $iconExtension;
}

/**
* {@inheritDoc}
*/
public function getFunctions()
{
return array(
new \Twig_SimpleFunction('button', array($this, 'buttonFunction'), array('is_safe' => array('html'))),
new \Twig_SimpleFunction('button_link', array($this, 'buttonLinkFunction'), array('is_safe' => array('html'))),
);
}

/**
* @param array $options
* @return string
*/
public function buttonFunction(array $options = array())
{
$options = array_merge($this->defaults, $options);

$options['attr']['class'] = "btn btn-{$options['type']} btn-{$options['size']}" . (isset($options['attr']['class']) ? ' '.$options['attr']['class'] : '');
$options['attr']['type'] = isset($options['submit']) && $options['submit'] ? 'submit' : 'button';

$icon = $options['icon'] ? $this->iconExtension->iconFunction($options['icon']).' ' : '';
$attr = $options['attr'] ? $this->attributes($options['attr']) : '';

$button = "<button{$attr}>{$icon}{$options['label']}</button>";

return $button;
}

/**
* @param array $options
* @return string
*/
public function buttonLinkFunction(array $options = array())
{
$options = array_merge($this->defaults, $options);

$options['attr']['class'] = "btn btn-{$options['type']} btn-{$options['size']}" . (isset($options['attr']['class']) ? ' '.$options['attr']['class'] : '');
$options['attr']['href'] = (isset($options['attr']['href']) ? $options['attr']['href'] : '#');

$icon = $options['icon'] ? $this->iconExtension->iconFunction($options['icon']).' ' : '';
$attr = $options['attr'] ? $this->attributes($options['attr']) : '';

$button = "<a{$attr}>{$icon}{$options['label']}</a>";

return $button;
}

private function attributes(array $attributes)
{
$result = '';
array_walk($attributes, function($value, $attr) use (&$result) {
$result .= " $attr=\"$value\"";
});
return $result;
}

/**
* {@inheritDoc}
*/
public function getName()
{
return 'braincrafted_bootstrap_button';
}
}