Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
25.00% covered (danger)
25.00%
1 / 4
CRAP
77.14% covered (warning)
77.14%
27 / 35
TwbBundleButtonGroup
0.00% covered (danger)
0.00%
0 / 1
25.00% covered (danger)
25.00%
1 / 4
21.87
77.14% covered (warning)
77.14%
27 / 35
 __invoke(array $aButtons = null, array $aButtonGroupOptions = null)
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
1 / 1
 render(array $aButtons, array $aButtonGroupOptions = null)
0.00% covered (danger)
0.00%
0 / 1
5.06
86.67% covered (warning)
86.67%
13 / 15
 renderButtons(array $aButtons, $bJustified = false)
0.00% covered (danger)
0.00%
0 / 1
10.92
64.29% covered (warning)
64.29%
9 / 14
 getFormElementHelper()
0.00% covered (danger)
0.00%
0 / 1
3.07
80.00% covered (warning)
80.00%
4 / 5
<?php
namespace TwbBundle\View\Helper;
class TwbBundleButtonGroup extends \Zend\Form\View\Helper\AbstractHelper {
    /**
     * @var string
     */
    protected static $buttonGroupContainerFormat = '<div %s>%s</div>';
    /**
     * @var string
     */
    protected static $buttonGroupJustifiedFormat = '<div class="btn-group">%s</div>';
    /**
     * @var \TwbBundle\Form\View\Helper\TwbBundleFormElement
     */
    protected $formElementHelper;
    /**
     * @param array $aButtons
     * @param array $aButtonGroupOptions
     * @return \TwbBundle\View\Helper\TwbBundleButtonGroup|string
     */
    public function __invoke(array $aButtons = null, array $aButtonGroupOptions = null) {
        return $aButtons ? $this->render($aButtons, $aButtonGroupOptions) : $this;
    }
    /**
     * Render button groups markup
     * @param array $aButtons
     * @param array $aButtonGroupOptions
     * @throws \LogicException
     * @return string
     */
    public function render(array $aButtons, array $aButtonGroupOptions = null) {
        // ### Button group container attributes ###
        if (empty($aButtonGroupOptions['attributes'])) {
            $aButtonGroupOptions['attributes'] = array('class' => 'btn-group');
        } else {
            if (!is_array($aButtonGroupOptions['attributes'])) {
                throw new \LogicException('"attributes" option expects an array, "' . gettype($aButtonGroupOptions['attributes']) . '" given');
            }
            if (empty($aButtonGroupOptions['attributes']['class'])) {
                $aButtonGroupOptions['attributes']['class'] = 'btn-group';
            } elseif (!preg_match('/(\s|^)(?:btn-group|btn-group-vertical)(\s|$)/', $aButtonGroupOptions['attributes']['class'])) {
                $aButtonGroupOptions['attributes']['class'] .= ' btn-group';
            }
        }
        // ### Render button group ###
        return sprintf(
                self::$buttonGroupContainerFormat,
                //Container attributes
                $this->createAttributesString($aButtonGroupOptions['attributes']),
                //Buttons
                $this->renderButtons($aButtons, strpos($aButtonGroupOptions['attributes']['class'], 'btn-group-justified') !== false)
        );
    }
    /**
     * Render buttons markup
     * @param array $aButtons
     * @return string
     */
    protected function renderButtons(array $aButtons, $bJustified = false) {
        $sMarkup = '';
        foreach ($aButtons as $oButton) {
            if (
                    is_array($oButton) || ($oButton instanceof \Traversable && !($oButton instanceof \Zend\Form\ElementInterface))
            ) {
                $oFactory = new \Zend\Form\Factory();
                $oButton = $oFactory->create($oButton);
            } elseif (!($oButton instanceof \Zend\Form\ElementInterface)) {
                throw new \LogicException(sprintf(
                        'Button expects an instanceof \Zend\Form\ElementInterface or an array / \Traversable, "%s" given', is_object($oButton) ? get_class($oButton) : gettype($oButton)
                ));
            }
            $sButtonMarkup = $this->getFormElementHelper()->__invoke($oButton);
            $sMarkup .= $bJustified ? sprintf(self::$buttonGroupJustifiedFormat, $sButtonMarkup) : $sButtonMarkup;
        }
        return $sMarkup;
    }
    /**
     * @return \TwbBundle\Form\View\Helper\TwbBundleFormElement
     */
    public function getFormElementHelper() {
        if ($this->formElementHelper instanceof \TwbBundle\Form\View\Helper\TwbBundleFormElement) {
            return $this->formElementHelper;
        }
        if (method_exists($this->view, 'plugin')) {
            return $this->formElementHelper = $this->view->plugin('form_element');
        }
        return $this->formElementHelper = new \TwbBundle\Form\View\Helper\TwbBundleFormElement();
    }
}