From 57050310d5fc8b02ac16ade13f1e9a866f85b431 Mon Sep 17 00:00:00 2001 From: fabiocarneiro Date: Mon, 3 Nov 2014 19:21:20 -0200 Subject: [PATCH] update module coding style and remove hardcoded default form view helpers ignore list --- config/module.config.php | 27 +- .../Factory/TwbBundleFormElementFactory.php | 22 + .../Form/View/Helper/TwbBundleForm.php | 73 +- .../Form/View/Helper/TwbBundleFormButton.php | 155 +++-- .../View/Helper/TwbBundleFormCheckbox.php | 75 ++- .../View/Helper/TwbBundleFormCollection.php | 15 +- .../Form/View/Helper/TwbBundleFormElement.php | 131 ++-- .../Helper/TwbBundleFormElementErrors.php | 6 +- .../Form/View/Helper/TwbBundleFormErrors.php | 27 +- .../Helper/TwbBundleFormMultiCheckbox.php | 10 +- .../Form/View/Helper/TwbBundleFormRadio.php | 18 +- .../Form/View/Helper/TwbBundleFormRow.php | 91 ++- .../Form/View/Helper/TwbBundleFormStatic.php | 31 +- .../Options/Factory/ModuleOptionsFactory.php | 17 + src/TwbBundle/Options/ModuleOptions.php | 23 + src/TwbBundle/View/Helper/TwbBundleAlert.php | 136 ++-- src/TwbBundle/View/Helper/TwbBundleBadge.php | 81 ++- .../View/Helper/TwbBundleButtonGroup.php | 70 +- .../View/Helper/TwbBundleDropdown.php | 621 +++++++++++------- .../View/Helper/TwbBundleFontAwesome.php | 7 +- .../View/Helper/TwbBundleGlyphicon.php | 60 +- src/TwbBundle/View/Helper/TwbBundleLabel.php | 93 ++- 22 files changed, 1144 insertions(+), 645 deletions(-) create mode 100644 src/TwbBundle/Form/View/Helper/Factory/TwbBundleFormElementFactory.php create mode 100644 src/TwbBundle/Options/Factory/ModuleOptionsFactory.php create mode 100644 src/TwbBundle/Options/ModuleOptions.php diff --git a/config/module.config.php b/config/module.config.php index 9dcfab0..71f7881 100644 --- a/config/module.config.php +++ b/config/module.config.php @@ -1,8 +1,25 @@ array( - 'invokables' => array( + 'twbbundle' => array ( + 'ignoredViewHelpers' => array ( + 'file', + 'checkbox', + 'radio', + 'submit', + 'multi_checkbox', + 'static', + 'button', + 'reset' + ) + ), + 'service_manager' => array ( + 'factories' => array ( + 'TwbBundle\Options\ModuleOptions' => 'TwbBundle\Options\Factory\ModuleOptionsFactory' + ) + ), + 'view_helpers' => array ( + 'invokables' => array ( //Alert 'alert' => 'TwbBundle\View\Helper\TwbBundleAlert', //Badge @@ -17,7 +34,6 @@ 'formSubmit' => 'TwbBundle\Form\View\Helper\TwbBundleFormButton', 'formCheckbox' => 'TwbBundle\Form\View\Helper\TwbBundleFormCheckbox', 'formCollection' => 'TwbBundle\Form\View\Helper\TwbBundleFormCollection', - 'formElement' => 'TwbBundle\Form\View\Helper\TwbBundleFormElement', 'formElementErrors' => 'TwbBundle\Form\View\Helper\TwbBundleFormElementErrors', 'formMultiCheckbox' => 'TwbBundle\Form\View\Helper\TwbBundleFormMultiCheckbox', 'formRadio' => 'TwbBundle\Form\View\Helper\TwbBundleFormRadio', @@ -31,6 +47,9 @@ 'fontAwesome' => 'TwbBundle\View\Helper\TwbBundleFontAwesome', //Label 'label' => 'TwbBundle\View\Helper\TwbBundleLabel' + ), + 'factories' => array ( + 'formElement' => 'TwbBundle\Form\View\Helper\Factory\TwbBundleFormElementFactory', ) - ) + ), ); diff --git a/src/TwbBundle/Form/View/Helper/Factory/TwbBundleFormElementFactory.php b/src/TwbBundle/Form/View/Helper/Factory/TwbBundleFormElementFactory.php new file mode 100644 index 0000000..f8f4687 --- /dev/null +++ b/src/TwbBundle/Form/View/Helper/Factory/TwbBundleFormElementFactory.php @@ -0,0 +1,22 @@ + + * @license MIT + */ +class TwbBundleFormElementFactory implements FactoryInterface +{ + public function createService(ServiceLocatorInterface $serviceLocator) + { + $options = $serviceLocator->getServiceLocator()->get('TwbBundle\Options\ModuleOptions'); + return new TwbBundleFormElement($options); + } +} diff --git a/src/TwbBundle/Form/View/Helper/TwbBundleForm.php b/src/TwbBundle/Form/View/Helper/TwbBundleForm.php index 39ee5a6..17c27a0 100644 --- a/src/TwbBundle/Form/View/Helper/TwbBundleForm.php +++ b/src/TwbBundle/Form/View/Helper/TwbBundleForm.php @@ -1,7 +1,11 @@ render($oForm, $sFormLayout); } $this->formLayout = $sFormLayout; return $this; - } + } /** * Render a form from the provided $oForm, - * @see \Zend\Form\View\Helper\Form::render() - * @param \Zend\Form\FormInterface $oForm + * @see Form::render() + * @param FormInterface $oForm * @param string $sFormLayout * @return string */ - public function render(\Zend\Form\FormInterface $oForm, $sFormLayout = self::LAYOUT_HORIZONTAL) + public function render(FormInterface $oForm, $sFormLayout = self::LAYOUT_HORIZONTAL) { - //Prepare form if needed - if (method_exists($oForm, 'prepare')) { - $oForm->prepare(); - } + //Prepare form if needed + if (method_exists($oForm, 'prepare')) { + $oForm->prepare(); + } - $this->setFormClass($oForm, $sFormLayout); + $this->setFormClass($oForm, $sFormLayout); - //Set form role - if (!$oForm->getAttribute('role')) { + //Set form role + if (!$oForm->getAttribute('role')) { $oForm->setAttribute('role', 'form'); } $bHasColumnSizes = false; - $sFormContent = ''; - $oRenderer = $this->getView(); - foreach($oForm as $oElement){ - $aOptions = $oElement->getOptions(); - if (!$bHasColumnSizes && !empty($aOptions['column-size'])) { + $sFormContent = ''; + $oRenderer = $this->getView(); + foreach ($oForm as $oElement) { + $aOptions = $oElement->getOptions(); + if (!$bHasColumnSizes && !empty($aOptions['column-size'])) { $bHasColumnSizes = true; } //Define layout option to form elements if not already defined - if($sFormLayout && empty($aOptions['twb-layout'])){ + if ($sFormLayout && empty($aOptions['twb-layout'])) { $aOptions['twb-layout'] = $sFormLayout; - $oElement->setOptions($aOptions); - } - $sFormContent .= $oElement instanceof \Zend\Form\FieldsetInterface?$oRenderer->formCollection($oElement):$oRenderer->formRow($oElement); - } - if ($bHasColumnSizes && $sFormLayout !== self::LAYOUT_HORIZONTAL) { + $oElement->setOptions($aOptions); + } + $sFormContent .= $oElement instanceof FieldsetInterface ? $oRenderer->formCollection($oElement) : $oRenderer->formRow($oElement); + } + if ($bHasColumnSizes && $sFormLayout !== self::LAYOUT_HORIZONTAL) { $sFormContent = sprintf(self::$formRowFormat, $sFormContent); } - return $this->openTag($oForm).$sFormContent.$this->closeTag(); + return $this->openTag($oForm) . $sFormContent . $this->closeTag(); } /** * Sets form layout class * - * @param \Zend\Form\FormInterface $oForm + * @param FormInterface $oForm * @param string $sFormLayout * @return void */ - protected function setFormClass(\Zend\Form\FormInterface $oForm, $sFormLayout = self::LAYOUT_HORIZONTAL) + protected function setFormClass(FormInterface $oForm, $sFormLayout = self::LAYOUT_HORIZONTAL) { - if(is_string($sFormLayout)){ + if (is_string($sFormLayout)) { $sLayoutClass = 'form-'.$sFormLayout; if ($sFormClass = $oForm->getAttribute('class')) { if (!preg_match('/(\s|^)' . preg_quote($sLayoutClass, '/') . '(\s|$)/', $sFormClass)) { $oForm->setAttribute('class', trim($sFormClass . ' ' . $sLayoutClass)); } - } - else { + } else { $oForm->setAttribute('class', $sLayoutClass); } } @@ -100,10 +103,10 @@ protected function setFormClass(\Zend\Form\FormInterface $oForm, $sFormLayout = /** * Generate an opening form tag * - * @param null|\Zend\Form\FormInterface $form + * @param null|FormInterface $form * @return string */ - public function openTag(\Zend\Form\FormInterface $form = null) + public function openTag(FormInterface $form = null) { $this->setFormClass($form, $this->formLayout); return parent::openTag($form); diff --git a/src/TwbBundle/Form/View/Helper/TwbBundleFormButton.php b/src/TwbBundle/Form/View/Helper/TwbBundleFormButton.php index f2dccf0..c9cc492 100644 --- a/src/TwbBundle/Form/View/Helper/TwbBundleFormButton.php +++ b/src/TwbBundle/Form/View/Helper/TwbBundleFormButton.php @@ -2,8 +2,15 @@ namespace TwbBundle\Form\View\Helper; -class TwbBundleFormButton extends \Zend\Form\View\Helper\FormButton { - +use DomainException; +use LogicException; +use Exception; +use Zend\Form\LabelAwareInterface; +use Zend\Form\View\Helper\FormButton; +use Zend\Form\ElementInterface; + +class TwbBundleFormButton extends FormButton +{ /** * @var string */ @@ -36,14 +43,15 @@ class TwbBundleFormButton extends \Zend\Form\View\Helper\FormButton { protected static $buttonOptions = array('default', 'primary', 'success', 'info', 'warning', 'danger'); /** - * @see \Zend\Form\View\Helper\FormButton::render() - * @param \Zend\Form\ElementInterface $oElement + * @see FormButton::render() + * @param ElementInterface $oElement * @param string $sButtonContent - * @throws \LogicException - * @throws \Exception + * @throws LogicException + * @throws Exception * @return string */ - public function render(\Zend\Form\ElementInterface $oElement, $sButtonContent = null) { + public function render(ElementInterface $oElement, $sButtonContent = null) + { if ($sClass = $oElement->getAttribute('class')) { if (!preg_match('/(\s|^)btn(\s|$)/', $sClass)) { $sClass .= ' btn'; @@ -75,93 +83,152 @@ public function render(\Zend\Form\ElementInterface $oElement, $sButtonContent = $sIconHelperMethod = 'fontAwesome'; } - // Define button content + /* + * Define button content + */ if (null === $sButtonContent) { $sButtonContent = $oElement->getLabel(); if (null === $sButtonContent && !$aIconOptions) { - throw new \DomainException(sprintf( - '%s expects either button content as the second argument, ' . - 'or that the element provided has a label value or a icon option; neither found', __METHOD__ + throw new DomainException(sprintf( + '%s expects either button content as the second argument, ' . + 'or that the element provided has a label value or a glyphicon option; neither found', + __METHOD__ )); } if (null !== ($oTranslator = $this->getTranslator())) { $sButtonContent = $oTranslator->translate( - $sButtonContent, $this->getTranslatorTextDomain() + $sButtonContent, + $this->getTranslatorTextDomain() ); } } - if (!$oElement instanceof \Zend\Form\LabelAwareInterface || !$oElement->getLabelOption('disable_html_escape')) { + if (!$oElement instanceof LabelAwareInterface || !$oElement->getLabelOption('disable_html_escape')) { $oEscapeHtmlHelper = $this->getEscapeHtmlHelper(); $sButtonContent = $oEscapeHtmlHelper($sButtonContent); } - // Manage icon + /* + * Manage icon + */ if ($aIconOptions) { if (is_scalar($aIconOptions)) { - $aIconOptions = array( + $aIconOptions = array ( 'icon' => $aIconOptions, 'position' => self::ICON_PREPEND ); - } elseif (!is_array($aIconOptions)) { - throw new \LogicException('Button "icon" option expects a scalar value or an array, "' . gettype($aIconOptions) . '" given'); - } elseif (!is_scalar($aIconOptions['icon'])) { - throw new \LogicException('Button "icon" option expects a scalar value, "' . gettype($aIconOptions['icon']) . '" given'); - } elseif (empty($aIconOptions['position'])) { - $aIconOptions['position'] = 'prepend'; - } elseif (!is_string($aIconOptions['position'])) { - throw new \LogicException('Icon "position" option expects a string, "' . gettype($aIconOptions['position']) . '" given'); - } elseif ($aIconOptions['position'] !== self::ICON_PREPEND && $aIconOptions['position'] !== self::ICON_APPEND) { - throw new \LogicException('Icon "position" option allows "' . self::ICON_PREPEND . '" or "' . self::ICON_APPEND . '", "' . $aIconOptions['position'] . '" given'); + } + + if (!is_array($aIconOptions)) { + throw new LogicException(sprintf( + '"glyphicon" button option expects a scalar value or an array, "%s" given', + is_object($aIconOptions) ? get_class($aIconOptions) : gettype($aIconOptions) + )); + } + + $position = 'prepend'; + + if (!empty($aIconOptions['position'])) { + $position = $aIconOptions['position']; + } + + if (!empty($aIconOptions['icon'])) { + $icon = $aIconOptions['icon']; + } + + if (!is_scalar($icon)) { + throw new LogicException(sprintf( + 'Glyphicon "icon" option expects a scalar value, "%s" given', + is_object($icon) ? get_class($icon) : gettype($icon) + )); + } elseif (!is_string($position)) { + throw new LogicException(sprintf( + 'Glyphicon "position" option expects a string, "%s" given', + is_object($position) ? get_class($position) : gettype($position) + )); + } elseif ($position !== self::ICON_PREPEND && $position !== self::ICON_APPEND) { + throw new LogicException(sprintf( + 'Glyphicon "position" option allows "'.self::ICON_PREPEND.'" or "'.self::ICON_APPEND.'", "%s" given', + is_object($position) ? get_class($position) : gettype($position) + )); } if ($sButtonContent) { - if ($aIconOptions['position'] === self::ICON_PREPEND) { - $sButtonContent = $this->getView()->{$sIconHelperMethod}($aIconOptions['icon'], isset($aIconOptions['attributes']) ? $aIconOptions['attributes'] : null) . ' ' . $sButtonContent; + if ($position === self::ICON_PREPEND) { + $sButtonContent = $this->getView()->{$sIconHelperMethod}( + $icon, + isset($aIconOptions['attributes'])?$aIconOptions['attributes']:null + ).' '.$sButtonContent; } else { - $sButtonContent .= ' ' . $this->getView()->{$sIconHelperMethod}($aIconOptions['icon'], isset($aIconOptions['attributes']) ? $aIconOptions['attributes'] : null); + $sButtonContent .= ' ' . $this->getView()->{$sIconHelperMethod}( + $icon, + isset($aIconOptions['attributes'])?$aIconOptions['attributes']:null + ); } } else { - $sButtonContent = $this->getView()->{$sIconHelperMethod}($aIconOptions['icon'], isset($aIconOptions['attributes']) ? $aIconOptions['attributes'] : null); + $sButtonContent = $this->getView()->{$sIconHelperMethod}( + $icon, + isset($aIconOptions['attributes']) ? $aIconOptions['attributes'] : null + ); } } - //Dropdown button + /* + * Dropdown button + */ if ($aDropdownOptions = $oElement->getOption('dropdown')) { if (!is_array($aDropdownOptions)) { - throw new \LogicException('"dropdown" option expects an array, "' . gettype($aDropdownOptions) . '" given'); + throw new LogicException(sprintf( + '"dropdown" option expects an array, "%s" given', + is_object($aDropdownOptions) ? get_class($aDropdownOptions) : gettype($aDropdownOptions) + )); } if (empty($aDropdownOptions['split'])) { - //Class + /* + * Class + */ if (!preg_match('/(\s|^)dropdown-toggle(\s|$)/', $sClass = $oElement->getAttribute('class'))) { $oElement->setAttribute('class', trim($sClass . ' dropdown-toggle')); } - //data-toggle + /* + * data-toggle + */ $oElement->setAttribute('data-toggle', 'dropdown'); - $sMarkup = $this->openTag($oElement) . sprintf(self::$dropdownToggleFormat, $sButtonContent) . $this->closeTag(); - } //Ad caret element - else { - $sMarkup = $this->openTag($oElement) . $sButtonContent . $this->closeTag() . sprintf(self::$dropdownCaretFormat, $oElement->getAttribute('class')); + $sMarkup = $this->openTag($oElement) . + sprintf(self::$dropdownToggleFormat, $sButtonContent) . + $this->closeTag(); + } else { + /* + * Add caret element + */ + $sMarkup = $this->openTag($oElement) . + $sButtonContent . + $this->closeTag() . + sprintf(self::$dropdownCaretFormat, $oElement->getAttribute('class')); } - //No container + /* + * No container + */ if ($oElement->getOption('disable-twb')) { return $sMarkup . $this->getView()->dropdown()->renderListItems($aDropdownOptions); } - //Render button + dropdown + /* + * Render button + dropdown + */ return sprintf( - self::$dropdownContainerFormat, - //Drop way - empty($aDropdownOptions['dropup']) ? '' : 'dropup', $sMarkup - . $this->getView()->dropdown()->renderListItems($aDropdownOptions) + self::$dropdownContainerFormat, + //Drop way + empty($aDropdownOptions['dropup']) ? '' : 'dropup', + $sMarkup . + $this->getView()->dropdown()->renderListItems($aDropdownOptions) ); } return $this->openTag($oElement) . $sButtonContent . $this->closeTag(); } - } diff --git a/src/TwbBundle/Form/View/Helper/TwbBundleFormCheckbox.php b/src/TwbBundle/Form/View/Helper/TwbBundleFormCheckbox.php index 94c5ba5..8e8e091 100644 --- a/src/TwbBundle/Form/View/Helper/TwbBundleFormCheckbox.php +++ b/src/TwbBundle/Form/View/Helper/TwbBundleFormCheckbox.php @@ -2,34 +2,46 @@ namespace TwbBundle\Form\View\Helper; -class TwbBundleFormCheckbox extends \Zend\Form\View\Helper\FormCheckbox { +use Zend\Form\View\Helper\FormRow; +use Zend\Form\View\Helper\FormCheckbox; +use Zend\Form\ElementInterface; +use InvalidArgumentException; +use LogicException; +use Zend\Form\Element\Checkbox; +use Zend\Form\View\Helper\FormLabel; + +class TwbBundleFormCheckbox extends FormCheckbox +{ /** * Form label helper instance - * @var \Zend\Form\View\Helper\FormLabel + * @var FormLabel */ protected $labelHelper; /** - * @see \Zend\Form\View\Helper\FormCheckbox::render() - * @param \Zend\Form\ElementInterface $oElement - * @throws \LogicException - * @throws \InvalidArgumentException + * @see FormCheckbox::render() + * @param ElementInterface $oElement + * @throws LogicException + * @throws InvalidArgumentException * @return string */ - public function render(\Zend\Form\ElementInterface $oElement) { + public function render(ElementInterface $oElement) + { if ($oElement->getOption('disable-twb')) { return parent::render($oElement); } - if (!$oElement instanceof \Zend\Form\Element\Checkbox) { - throw new \InvalidArgumentException(sprintf( - '%s requires that the element is of type Zend\Form\Element\Checkbox', __METHOD__ + if (!$oElement instanceof Checkbox) { + throw new InvalidArgumentException(sprintf( + '%s requires that the element is of type Zend\Form\Element\Checkbox', + __METHOD__ )); } if (($sName = $oElement->getName()) !== 0 && empty($sName)) { - throw new \LogicException(sprintf( - '%s requires that the element has an assigned name; none discovered', __METHOD__ + throw new LogicException(sprintf( + '%s requires that the element has an assigned name; none discovered', + __METHOD__ )); } @@ -61,20 +73,28 @@ public function render(\Zend\Form\ElementInterface $oElement) { $sElementContent = sprintf('createAttributesString($aAttributes), $sClosingBracket); //Add label markup - if ($this->getLabelPosition($oElement) === \Zend\Form\View\Helper\FormRow::LABEL_PREPEND) { - $sElementContent = $sLabelOpen . ($sLabelContent ? rtrim($sLabelContent) . ' ' : '') . $sElementContent . $sLabelClose; + if ($this->getLabelPosition($oElement) === FormRow::LABEL_PREPEND) { + $sElementContent = $sLabelOpen . + ($sLabelContent ? rtrim($sLabelContent) . ' ' : '') . + $sElementContent . + $sLabelClose; } else { - $sElementContent = $sLabelOpen . $sElementContent . ($sLabelContent ? ' ' . ltrim($sLabelContent) : '') . $sLabelClose; + $sElementContent = $sLabelOpen . + $sElementContent . + ($sLabelContent ? ' ' . ltrim($sLabelContent) : '') . + $sLabelClose; } //Render hidden input if ($oElement->useHiddenElement()) { $sElementContent = sprintf( - 'createAttributesString(array( - 'name' => $aAttributes['name'], - 'value' => $oElement->getUncheckedValue(), - )), $sClosingBracket - ) . $sElementContent; + 'createAttributesString(array( + 'name' => $aAttributes['name'], + 'value' => $oElement->getUncheckedValue(), + )), + $sClosingBracket + ) . $sElementContent; } return $sElementContent; } @@ -83,28 +103,29 @@ public function render(\Zend\Form\ElementInterface $oElement) { * Get the label position * @return string */ - public function getLabelPosition(\Zend\Form\Element\Checkbox $oElement) { - return $oElement->getLabelOption('position')? : \Zend\Form\View\Helper\FormRow::LABEL_APPEND; + public function getLabelPosition(Checkbox $oElement) + { + return $oElement->getLabelOption('position')? : FormRow::LABEL_APPEND; } /** * Retrieve the FormLabel helper - * @return \Zend\Form\View\Helper\FormLabel + * @return FormLabel */ - protected function getLabelHelper() { + protected function getLabelHelper() + { if ($this->labelHelper) { return $this->labelHelper; } if (method_exists($this->view, 'plugin')) { $this->labelHelper = $this->view->plugin('form_label'); } - if (!($this->labelHelper instanceof \Zend\Form\View\Helper\FormLabel)) { - $this->labelHelper = new \Zend\Form\View\Helper\FormLabel(); + if (!($this->labelHelper instanceof FormLabel)) { + $this->labelHelper = new FormLabel(); } if ($this->hasTranslator()) { $this->labelHelper->setTranslator($this->getTranslator(), $this->getTranslatorTextDomain()); } return $this->labelHelper; } - } diff --git a/src/TwbBundle/Form/View/Helper/TwbBundleFormCollection.php b/src/TwbBundle/Form/View/Helper/TwbBundleFormCollection.php index 4960e11..ccc1932 100644 --- a/src/TwbBundle/Form/View/Helper/TwbBundleFormCollection.php +++ b/src/TwbBundle/Form/View/Helper/TwbBundleFormCollection.php @@ -3,9 +3,11 @@ namespace TwbBundle\Form\View\Helper; use Zend\Form\Element\Collection as CollectionElement; +use Zend\Form\View\Helper\FormCollection; +use Zend\Form\ElementInterface; -class TwbBundleFormCollection extends \Zend\Form\View\Helper\FormCollection { - +class TwbBundleFormCollection extends FormCollection +{ /** * @var string */ @@ -29,7 +31,8 @@ class TwbBundleFormCollection extends \Zend\Form\View\Helper\FormCollection { * @param \Zend\Form\ElementInterface $oElement * @return string */ - public function render(\Zend\Form\ElementInterface $oElement) { + public function render(ElementInterface $oElement) + { $oRenderer = $this->getView(); if (!method_exists($oRenderer, 'plugin')) { return ''; @@ -68,8 +71,8 @@ public function render(\Zend\Form\ElementInterface $oElement) { } $sMarkup = sprintf( - self::$legendFormat, ($sAttributes = $this->createAttributesString($oElement->getLabelAttributes()? : array())) ? ' ' . $sAttributes : '', $this->getEscapeHtmlHelper()->__invoke($sLabel) - ) . $sMarkup; + self::$legendFormat, ($sAttributes = $this->createAttributesString($oElement->getLabelAttributes()? : array())) ? ' ' . $sAttributes : '', $this->getEscapeHtmlHelper()->__invoke($sLabel) + ) . $sMarkup; } //Set form layout class @@ -99,7 +102,7 @@ public function render(\Zend\Form\ElementInterface $oElement) { */ public function renderTemplate(CollectionElement $collection) { - if($sElementLayout = $collection->getOption('twb-layout')) { + if ($sElementLayout = $collection->getOption('twb-layout')) { $elementOrFieldset = $collection->getTemplateElement(); $elementOrFieldset->setOption('twb-layout', $sElementLayout); } diff --git a/src/TwbBundle/Form/View/Helper/TwbBundleFormElement.php b/src/TwbBundle/Form/View/Helper/TwbBundleFormElement.php index 7a92711..4a3b30d 100644 --- a/src/TwbBundle/Form/View/Helper/TwbBundleFormElement.php +++ b/src/TwbBundle/Form/View/Helper/TwbBundleFormElement.php @@ -2,8 +2,21 @@ namespace TwbBundle\Form\View\Helper; -class TwbBundleFormElement extends \Zend\Form\View\Helper\FormElement implements \Zend\I18n\Translator\TranslatorAwareInterface { +use Traversable; +use InvalidArgumentException; +use LogicException; +use Zend\Form\ElementInterface; +use Zend\Form\View\Helper\FormElement; +use Zend\Form\Element\Collection; +use Zend\Form\Factory; +use Zend\I18n\Translator\TranslatorAwareInterface; +use Zend\I18n\Translator\TranslatorInterface; +use Zend\I18n\Translator\Translator; +use TwbBundle\Options\ModuleOptions; +use Zend\Form\Element\Button; +class TwbBundleFormElement extends FormElement implements TranslatorAwareInterface +{ /** * @var string */ @@ -16,7 +29,7 @@ class TwbBundleFormElement extends \Zend\Form\View\Helper\FormElement implements /** * Translator (optional) - * @var \Zend\I18n\Translator\Translator + * @var Translator */ protected $translator; @@ -31,6 +44,12 @@ class TwbBundleFormElement extends \Zend\Form\View\Helper\FormElement implements * @var boolean */ protected $translatorEnabled = true; + + /** + * Hold configurable options + * @var ModuleOptions + */ + protected $options; /** * Instance map to view helper @@ -47,17 +66,23 @@ class TwbBundleFormElement extends \Zend\Form\View\Helper\FormElement implements 'Zend\Form\Element\MonthSelect' => 'formmonthselect', 'TwbBundle\Form\Element\StaticElement' => 'formStatic', ); + + public function __construct(ModuleOptions $options) + { + $this->options = $options; + } /** * Render an element - * @param \Zend\Form\ElementInterface $oElement + * @param ElementInterface $oElement * @return string */ - public function render(\Zend\Form\ElementInterface $oElement) { + public function render(ElementInterface $oElement) + { // Add form-controll class $sElementType = $oElement->getAttribute('type'); - if ( - !in_array($sElementType, array('file', 'checkbox', 'radio', 'submit', 'multi_checkbox', 'static', 'button', 'reset')) && !($oElement instanceof \Zend\Form\Element\Collection) + if (!in_array($sElementType, $this->options->getIgnoredViewHelpers()) && + !($oElement instanceof Collection) ) { if ($sElementClass = $oElement->getAttribute('class')) { if (!preg_match('/(\s|^)form-control(\s|$)/', $sElementClass)) { @@ -91,7 +116,9 @@ public function render(\Zend\Form\ElementInterface $oElement) { } } return sprintf( - self::$inputGroupFormat, trim($sSpecialClass), $sMarkup + self::$inputGroupFormat, + trim($sSpecialClass), + $sMarkup ); } return $sMarkup; @@ -100,20 +127,24 @@ public function render(\Zend\Form\ElementInterface $oElement) { /** * Render addo-on markup * @param string $aAddOnOptions - * @throws \InvalidArgumentException - * @throws \LogicException + * @throws InvalidArgumentException + * @throws LogicException * @return string */ - protected function renderAddOn($aAddOnOptions) { + protected function renderAddOn($aAddOnOptions) + { if (empty($aAddOnOptions)) { - throw new \InvalidArgumentException('Addon options are empty'); + throw new InvalidArgumentException('Addon options are empty'); } - if ($aAddOnOptions instanceof \Zend\Form\ElementInterface) { + if ($aAddOnOptions instanceof ElementInterface) { $aAddOnOptions = array('element' => $aAddOnOptions); } elseif (is_scalar($aAddOnOptions)) { $aAddOnOptions = array('text' => $aAddOnOptions); } elseif (!is_array($aAddOnOptions)) { - throw new \InvalidArgumentException('Addon options expects an array or a scalar value, "' . gettype($aAddOnOptions) . '" given'); + throw new InvalidArgumentException(sprintf( + 'Addon options expects an array or a scalar value, "%s" given', + is_object($aAddOnOptions) ? get_class($aAddOnOptions) : gettype($aAddOnOptions) + )); } $sMarkup = ''; @@ -121,7 +152,10 @@ protected function renderAddOn($aAddOnOptions) { $sAddonClass = ''; if (!empty($aAddOnOptions['text'])) { if (!is_scalar($aAddOnOptions['text'])) { - throw new \LogicException('"text" option expects a scalar value, "' . gettype($aAddOnOptions['text']) . '" given'); + throw new InvalidArgumentException(sprintf( + '"text" option expects a scalar value, "%s" given', + is_object($aAddOnOptions['text']) ? get_class($aAddOnOptions['text']) : gettype($aAddOnOptions['text']) + )); } elseif (($oTranslator = $this->getTranslator())) { $sMarkup .= $oTranslator->translate($aAddOnOptions['text'], $this->getTranslatorTextDomain()); } else { @@ -129,20 +163,27 @@ protected function renderAddOn($aAddOnOptions) { } $sAddonClass .= ' input-group-addon'; } elseif (!empty($aAddOnOptions['element'])) { - if ( - is_array($aAddOnOptions['element']) || ($aAddOnOptions['element'] instanceof \Traversable && !($aAddOnOptions['element'] instanceof \Zend\Form\ElementInterface)) + if (is_array($aAddOnOptions['element']) || + ($aAddOnOptions['element'] instanceof Traversable && + !($aAddOnOptions['element'] instanceof ElementInterface)) ) { - $oFactory = new \Zend\Form\Factory(); + $oFactory = new Factory(); $aAddOnOptions['element'] = $oFactory->create($aAddOnOptions['element']); - } elseif (!($aAddOnOptions['element'] instanceof \Zend\Form\ElementInterface)) { - throw new \LogicException(sprintf( - '"element" option expects an instanceof \Zend\Form\ElementInterface, "%s" given', is_object($aAddOnOptions['element']) ? get_class($aAddOnOptions['element']) : gettype($aAddOnOptions['element']) + } elseif (!($aAddOnOptions['element'] instanceof ElementInterface)) { + throw new LogicException(sprintf( + '"element" option expects an instanceof Zend\Form\ElementInterface, "%s" given', + is_object($aAddOnOptions['element']) ? get_class($aAddOnOptions['element']) : gettype($aAddOnOptions['element']) )); } - $aAddOnOptions['element']->setOptions(array_merge($aAddOnOptions['element']->getOptions(), array('disable-twb' => true))); + + $aAddOnOptions['element']->setOptions(array_merge( + $aAddOnOptions['element']->getOptions(), + array('disable-twb' => true) + )); + $sMarkup .= $this->render($aAddOnOptions['element']); - if ($aAddOnOptions['element'] instanceof \Zend\Form\Element\Button) { + if ($aAddOnOptions['element'] instanceof Button) { $sAddonClass .= ' input-group-btn'; //Element contains dropdown, so add-on container must be a "div" if ($aAddOnOptions['element']->getOption('dropdown')) { @@ -158,12 +199,13 @@ protected function renderAddOn($aAddOnOptions) { /** * Sets translator to use in helper - * @see \Zend\I18n\Translator\TranslatorAwareInterface::setTranslator() - * @param \Zend\I18n\Translator\TranslatorInterface $oTranslator : [optional] translator. Default is null, which sets no translator. + * @see TranslatorAwareInterface::setTranslator() + * @param TranslatorInterface $oTranslator : [optional] translator. Default is null, which sets no translator. * @param string $sTextDomain : [optional] text domain Default is null, which skips setTranslatorTextDomain - * @return \TwbBundle\Form\View\Helper\TwbBundleFormElement + * @return TwbBundleFormElement */ - public function setTranslator(\Zend\I18n\Translator\TranslatorInterface $oTranslator = null, $sTextDomain = null) { + public function setTranslator(TranslatorInterface $oTranslator = null, $sTextDomain = null) + { $this->translator = $oTranslator; if (null !== $sTextDomain) { $this->setTranslatorTextDomain($sTextDomain); @@ -173,60 +215,65 @@ public function setTranslator(\Zend\I18n\Translator\TranslatorInterface $oTransl /** * Returns translator used in helper - * @see \Zend\I18n\Translator\TranslatorAwareInterface::getTranslator() - * @return null|\Zend\I18n\Translator\TranslatorInterface + * @see TranslatorAwareInterface::getTranslator() + * @return null|TranslatorInterface */ - public function getTranslator() { + public function getTranslator() + { return $this->isTranslatorEnabled() ? $this->translator : null; } /** * Checks if the helper has a translator - * @see \Zend\I18n\Translator\TranslatorAwareInterface::hasTranslator() + * @see TranslatorAwareInterface::hasTranslator() * @return boolean */ - public function hasTranslator() { + public function hasTranslator() + { return !!$this->getTranslator(); } /** * Sets whether translator is enabled and should be used - * @see \Zend\I18n\Translator\TranslatorAwareInterface::setTranslatorEnabled() + * @see TranslatorAwareInterface::setTranslatorEnabled() * @param boolean $bEnabled - * @return \TwbBundle\Form\View\Helper\TwbBundleFormElement + * @return TwbBundleFormElement */ - public function setTranslatorEnabled($bEnabled = true) { + public function setTranslatorEnabled($bEnabled = true) + { $this->translatorEnabled = !!$bEnabled; return $this; } /** * Returns whether translator is enabled and should be used - * @see \Zend\I18n\Translator\TranslatorAwareInterface::isTranslatorEnabled() + * @see TranslatorAwareInterface::isTranslatorEnabled() * @return boolean */ - public function isTranslatorEnabled() { + public function isTranslatorEnabled() + { return $this->translatorEnabled; } /** * Set translation text domain - * @see \Zend\I18n\Translator\TranslatorAwareInterface::setTranslatorTextDomain() + * @see TranslatorAwareInterface::setTranslatorTextDomain() * @param string $sTextDomain - * @return \TwbBundle\Form\View\Helper\TwbBundleFormElement + * @return TwbBundleFormElement */ - public function setTranslatorTextDomain($sTextDomain = 'default') { + public function setTranslatorTextDomain($sTextDomain = 'default') + { $this->translatorTextDomain = $sTextDomain; return $this; } /** * Return the translation text domain - * @see \Zend\I18n\Translator\TranslatorAwareInterface::getTranslatorTextDomain() + * @see TranslatorAwareInterface::getTranslatorTextDomain() * @return string */ - public function getTranslatorTextDomain() { + public function getTranslatorTextDomain() + { return $this->translatorTextDomain; } - } diff --git a/src/TwbBundle/Form/View/Helper/TwbBundleFormElementErrors.php b/src/TwbBundle/Form/View/Helper/TwbBundleFormElementErrors.php index beda3cd..735b804 100644 --- a/src/TwbBundle/Form/View/Helper/TwbBundleFormElementErrors.php +++ b/src/TwbBundle/Form/View/Helper/TwbBundleFormElementErrors.php @@ -1,6 +1,10 @@ 'help-block' ); diff --git a/src/TwbBundle/Form/View/Helper/TwbBundleFormErrors.php b/src/TwbBundle/Form/View/Helper/TwbBundleFormErrors.php index c25b896..439c08d 100644 --- a/src/TwbBundle/Form/View/Helper/TwbBundleFormErrors.php +++ b/src/TwbBundle/Form/View/Helper/TwbBundleFormErrors.php @@ -2,7 +2,11 @@ namespace TwbBundle\Form\View\Helper; -class TwbBundleFormErrors extends \Zend\Form\View\Helper\AbstractHelper { +use Zend\Form\View\Helper\AbstractHelper; +use Zend\Form\FormInterface; + +class TwbBundleFormErrors extends AbstractHelper +{ protected $defaultErrorText = 'There were errors in the form submission'; protected $messageOpenFormat = '

%s