Skip to content
This repository has been archived by the owner on Jul 10, 2020. It is now read-only.

Commit

Permalink
Fix ZF 2.3.* compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
neilime committed Mar 16, 2014
1 parent 88dce5b commit 19460e2
Show file tree
Hide file tree
Showing 59 changed files with 2,483 additions and 2,021 deletions.
683 changes: 373 additions & 310 deletions build/logs/clover.xml

Large diffs are not rendered by default.

10 changes: 0 additions & 10 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,6 @@
"support": {
"issues": "https://github.com/neilime/zf2-twb-bundle/issues"
},
"repositories":[
{
"type": "package",
"package": {
"version": "dev-master",
"name": "twitter/bootstrap",
"source": {"url": "https://github.com/twitter/bootstrap.git","type": "git","reference": "master"}
}
}
],
"suggest": {
"neilime/zf2-assets-bundle": "Bundling & caching Twitter Bootstrap assets",
"twitter/bootstrap": "Twitter bootstrap assets"
Expand Down
89 changes: 44 additions & 45 deletions src/TwbBundle/Form/View/Helper/TwbBundleFormCollection.php
Original file line number Diff line number Diff line change
@@ -1,93 +1,92 @@
<?php

namespace TwbBundle\Form\View\Helper;
class TwbBundleFormCollection extends \Zend\Form\View\Helper\FormCollection{

/**
* @var string
*/
private static $legendFormat = '<legend %s>%s</legend>';
class TwbBundleFormCollection extends \Zend\Form\View\Helper\FormCollection {

/**
* @var string
*/
private static $fieldsetFormat = '<fieldset %s>%s</fieldset>';
/**
* @var string
*/
private static $legendFormat = '<legend%s>%s</legend>';

/**
* Attributes valid for the tag represented by this helper
* @var array
*/
protected $validTagAttributes = array(
'disabled' => true
);
/**
* @var string
*/
private static $fieldsetFormat = '<fieldset%s>%s</fieldset>';

/**
* Render a collection by iterating through all fieldsets and elements
* @param \Zend\Form\ElementInterface $oElement
* @return string
*/
public function render(\Zend\Form\ElementInterface $oElement){
/**
* Attributes valid for the tag represented by this helper
* @var array
*/
protected $validTagAttributes = array(
'disabled' => true
);

$oRenderer = $this->getView();
if(!method_exists($oRenderer, 'plugin'))return '';
/**
* Render a collection by iterating through all fieldsets and elements
* @param \Zend\Form\ElementInterface $oElement
* @return string
*/
public function render(\Zend\Form\ElementInterface $oElement) {
$oRenderer = $this->getView();
if (!method_exists($oRenderer, 'plugin')) {
return '';
}

$bShouldWrap = $this->shouldWrap;

$sMarkup = '';
$sMarkup = '';
$sElementLayout = $oElement->getOption('twb-layout');
if($oElement instanceof \IteratorAggregate){
$oElementHelper = $this->getElementHelper();
$oFieldsetHelper = $this->getFieldsetHelper();
if ($oElement instanceof \IteratorAggregate) {
$oElementHelper = $this->getElementHelper();
$oFieldsetHelper = $this->getFieldsetHelper();

foreach($oElement->getIterator() as $oElementOrFieldset){
foreach ($oElement->getIterator() as $oElementOrFieldset) {
$aOptions = $oElementOrFieldset->getOptions();
if ($sElementLayout && empty($aOptions['twb-layout'])) {
$aOptions['twb-layout'] = $sElementLayout;
$oElementOrFieldset->setOptions($aOptions);
}

if ($oElementOrFieldset instanceof \Zend\Form\FieldsetInterface) {
if ($oElementOrFieldset instanceof \Zend\Form\FieldsetInterface) {
$sMarkup .= $oFieldsetHelper($oElementOrFieldset);
} elseif ($oElementOrFieldset instanceof \Zend\Form\ElementInterface) {
$sMarkup .= $oElementHelper($oElementOrFieldset);
}
}
if ($oElement instanceof \Zend\Form\Element\Collection && $oElement->shouldCreateTemplate()) {
if ($oElement instanceof \Zend\Form\Element\Collection && $oElement->shouldCreateTemplate()) {
$sMarkup .= $this->renderTemplate($oElement);
}
}

if($bShouldWrap) {
if(($sLabel = $oElement->getLabel())){
if ($bShouldWrap) {
if (($sLabel = $oElement->getLabel())) {
if (null !== ($oTranslator = $this->getTranslator())) {
$sLabel = $oTranslator->translate($sLabel, $this->getTranslatorTextDomain());
}

$sMarkup = sprintf(
self::$legendFormat,
$this->createAttributesString($oElement->getLabelAttributes()?:array()),
$this->getEscapeHtmlHelper()->__invoke($sLabel)
).$sMarkup;
self::$legendFormat, ($sAttributes = $this->createAttributesString($oElement->getLabelAttributes()? : array())) ? ' ' . $sAttributes : '', $this->getEscapeHtmlHelper()->__invoke($sLabel)
) . $sMarkup;
}

//Set form layout class
if($sElementLayout){
$sLayoutClass = 'form-'.$sElementLayout;
if ($sElementLayout) {
$sLayoutClass = 'form-' . $sElementLayout;
if ($sElementClass = $oElement->getAttribute('class')) {
if (!preg_match('/(\s|^)' . preg_quote($sLayoutClass, '/') . '(\s|$)/', $sElementClass)) {
$oElement->setAttribute('class', trim($sElementClass . ' ' . $sLayoutClass));
}
}
else {
} else {
$oElement->setAttribute('class', $sLayoutClass);
}
}

$sMarkup = sprintf(
self::$fieldsetFormat,
$this->createAttributesString($oElement->getAttributes()),
$sMarkup
self::$fieldsetFormat, ($sAttributes = $this->createAttributesString($oElement->getAttributes())) ? ' ' . $sAttributes : '', $sMarkup
);
}
return $sMarkup;
}
}

}
90 changes: 49 additions & 41 deletions src/TwbBundle/Form/View/Helper/TwbBundleFormElement.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<?php

namespace TwbBundle\Form\View\Helper;
class TwbBundleFormElement extends \Zend\Form\View\Helper\FormElement implements \Zend\I18n\Translator\TranslatorAwareInterface
{

class TwbBundleFormElement extends \Zend\Form\View\Helper\FormElement implements \Zend\I18n\Translator\TranslatorAwareInterface {

/**
* @var string
*/
protected static $addonFormat = '<%s class="%s">%s</%s>';


/**
* @var string
*/
Expand Down Expand Up @@ -37,36 +37,44 @@ class TwbBundleFormElement extends \Zend\Form\View\Helper\FormElement implements
* @param \Zend\Form\ElementInterface $oElement
* @return string
*/
public function render(\Zend\Form\ElementInterface $oElement)
{
public function render(\Zend\Form\ElementInterface $oElement) {
//Add form-controll class
$sElementType = $oElement->getAttribute('type');

if (!in_array($sElementType, array('file', 'checkbox', 'radio', 'submit', 'multi_checkbox', 'static', 'button', 'reset'))) {
if ($sElementClass = $oElement->getAttribute('class')) {
if (!preg_match('/(\s|^)form-control(\s|$)/', $sElementClass)) $oElement->setAttribute('class', trim($sElementClass . ' form-control'));
} else $oElement->setAttribute('class', 'form-control');
if (!preg_match('/(\s|^)form-control(\s|$)/', $sElementClass)) {
$oElement->setAttribute('class', trim($sElementClass . ' form-control'));
}
} else {
$oElement->setAttribute('class', 'form-control');
}
}

$sMarkup = $oElement instanceof \TwbBundle\Form\Element\StaticElement ? $this->getView()->formStatic()->render($oElement) : parent::render($oElement);

//Addon prepend
if ($aAddOnPrepend = $oElement->getOption('add-on-prepend')) $sMarkup = $this->renderAddOn($aAddOnPrepend) . $sMarkup;
if ($aAddOnPrepend = $oElement->getOption('add-on-prepend')) {
$sMarkup = $this->renderAddOn($aAddOnPrepend) . $sMarkup;
}

//Addon append
if ($aAddOnAppend = $oElement->getOption('add-on-append')) $sMarkup .= $this->renderAddOn($aAddOnAppend);
if ($aAddOnAppend = $oElement->getOption('add-on-append')) {
$sMarkup .= $this->renderAddOn($aAddOnAppend);
}

if ($aAddOnAppend || $aAddOnPrepend) {
$sSpecialClass = '';
//Input size
if ($sElementClass = $oElement->getAttribute('class')) {
if (preg_match('/(\s|^)input-lg(\s|$)/', $sElementClass)) $sSpecialClass .= ' input-group-lg';
elseif (preg_match('/(\s|^)input-sm(\s|$)/', $sElementClass)) $sSpecialClass .= ' input-group-sm';
if (preg_match('/(\s|^)input-lg(\s|$)/', $sElementClass)) {
$sSpecialClass .= ' input-group-lg';
} elseif (preg_match('/(\s|^)input-sm(\s|$)/', $sElementClass)) {
$sSpecialClass .= ' input-group-sm';
}
}
return sprintf(
self::$inputGroupFormat,
trim($sSpecialClass),
$sMarkup
self::$inputGroupFormat, trim($sSpecialClass), $sMarkup
);
}
return $sMarkup;
Expand All @@ -79,12 +87,17 @@ public function render(\Zend\Form\ElementInterface $oElement)
* @throws \LogicException
* @return string
*/
protected function renderAddOn($aAddOnOptions)
{
if (empty($aAddOnOptions)) throw new \InvalidArgumentException('Addon options are empty');
if ($aAddOnOptions instanceof \Zend\Form\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');
protected function renderAddOn($aAddOnOptions) {
if (empty($aAddOnOptions)) {
throw new \InvalidArgumentException('Addon options are empty');
}
if ($aAddOnOptions instanceof \Zend\Form\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');
}

$sMarkup = '';
$sAddonTagName = 'span';
Expand All @@ -100,20 +113,19 @@ 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))
is_array($aAddOnOptions['element']) || ($aAddOnOptions['element'] instanceof \Traversable && !($aAddOnOptions['element'] instanceof \Zend\Form\ElementInterface))
) {
$oFactory = new \Zend\Form\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'])
'"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)));
$sMarkup .= $this->render($aAddOnOptions['element']);

if($aAddOnOptions['element'] instanceof \Zend\Form\Element\Button) {
if ($aAddOnOptions['element'] instanceof \Zend\Form\Element\Button) {
$sAddonClass .= ' input-group-btn';
//Element contains dropdown, so add-on container must be a "div"
if ($aAddOnOptions['element']->getOption('dropdown')) {
Expand All @@ -130,24 +142,24 @@ protected function renderAddOn($aAddOnOptions)
/**
* Sets translator to use in helper
* @see \Zend\I18n\Translator\TranslatorAwareInterface::setTranslator()
* @param \Zend\I18n\Translator\Translator $oTranslator : [optional] translator. Default is null, which sets no translator.
* @param \Zend\I18n\Translator\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
*/
public function setTranslator(\Zend\I18n\Translator\Translator $oTranslator = null, $sTextDomain = null)
{
public function setTranslator(\Zend\I18n\Translator\TranslatorInterface $oTranslator = null, $sTextDomain = null) {
$this->translator = $oTranslator;
if (null !== $sTextDomain) $this->setTranslatorTextDomain($sTextDomain);
if (null !== $sTextDomain) {
$this->setTranslatorTextDomain($sTextDomain);
}
return $this;
}

/**
* Returns translator used in helper
* @see \Zend\I18n\Translator\TranslatorAwareInterface::getTranslator()
* @return null|\Zend\I18n\Translator\Translator
* @return null|\Zend\I18n\Translator\TranslatorInterface
*/
public function getTranslator()
{
public function getTranslator() {
return $this->isTranslatorEnabled() ? $this->translator : null;
}

Expand All @@ -156,8 +168,7 @@ public function getTranslator()
* @see \Zend\I18n\Translator\TranslatorAwareInterface::hasTranslator()
* @return boolean
*/
public function hasTranslator()
{
public function hasTranslator() {
return !!$this->getTranslator();
}

Expand All @@ -167,8 +178,7 @@ public function hasTranslator()
* @param boolean $bEnabled
* @return \TwbBundle\Form\View\Helper\TwbBundleFormElement
*/
public function setTranslatorEnabled($bEnabled = true)
{
public function setTranslatorEnabled($bEnabled = true) {
$this->translatorEnabled = !!$bEnabled;
return $this;
}
Expand All @@ -178,8 +188,7 @@ public function setTranslatorEnabled($bEnabled = true)
* @see \Zend\I18n\Translator\TranslatorAwareInterface::isTranslatorEnabled()
* @return boolean
*/
public function isTranslatorEnabled()
{
public function isTranslatorEnabled() {
return $this->translatorEnabled;
}

Expand All @@ -189,8 +198,7 @@ public function isTranslatorEnabled()
* @param string $sTextDomain
* @return \TwbBundle\Form\View\Helper\TwbBundleFormElement
*/
public function setTranslatorTextDomain($sTextDomain = 'default')
{
public function setTranslatorTextDomain($sTextDomain = 'default') {
$this->translatorTextDomain = $sTextDomain;
return $this;
}
Expand All @@ -200,8 +208,8 @@ public function setTranslatorTextDomain($sTextDomain = 'default')
* @see \Zend\I18n\Translator\TranslatorAwareInterface::getTranslatorTextDomain()
* @return string
*/
public function getTranslatorTextDomain()
{
public function getTranslatorTextDomain() {
return $this->translatorTextDomain;
}

}
Loading

0 comments on commit 19460e2

Please sign in to comment.