getElement(); if ((!$form instanceof Zend_Form) && (!$form instanceof Zend_Form_DisplayGroup)) { return $content; } $separator = $this->getSeparator(); $translator = $form->getTranslator(); $items = []; $view = $form->getView(); $wrappers = $this->getOption('wrappers'); /** @var Zend_Form_Element|Zend_Form|Zend_Form_DisplayGroup $item */ foreach ($form as $item) { $item->setView($view)->setTranslator($translator); $items[$item->getName()] = $item->render(); } if ($wrappers) { $items = $this->applyWrappers($items, $wrappers); } $elementContent = implode($separator, $items); switch ($this->getPlacement()) { case self::PREPEND: return $elementContent . $separator . $content; case self::APPEND: default: return $content . $separator . $elementContent; } } /** * @param array $elements * @param array $decorators * @param null $name - assign name for further wrapping * @return $this */ public function addWrapper(array $elements, array $decorators, $name = null) { $wrappers = (array) $this->getOption('wrappers'); if (null === $name) { $wrappers[] = compact('elements', 'decorators'); } else { $wrappers[$name] = compact('elements', 'decorators'); } $this->setOption('wrappers', $wrappers); return $this; } protected function applyWrappers(array $items, array $wrappers) { foreach ($wrappers as $name => $options) { $items = $this->applyWrapper($items, $options, $name); } return $items; } protected function applyWrapper(array $items, $wrapper, $name) { if (!array_key_exists('elements', $wrapper) || empty($wrapper['elements'])) { trigger_error('Invalid elements for wrapper "' . $name . '"'); return $this; } if (!array_key_exists('decorators', $wrapper) || empty($wrapper['decorators'])) { trigger_error('Invalid decorators for wrapper "' . $name . '"'); return $this; } $decorators = $this->_getDecorators($wrapper['decorators']); $content = ''; $result = []; if (empty($name) || is_numeric($name)) { $names = array_filter(array_keys($items), 'intval'); $name = (empty($names)) ? 0 : (int) max($names) + 1; } foreach (array_keys($items) as $itemName) { if (!in_array($itemName, $wrapper['elements'])) { $result[$itemName] = $items[$itemName]; } else { $content .= $items[$itemName]; if (!array_key_exists($name, $result)) { $result[$name] = ''; } } } $result[$name] = $this->renderDecorators($decorators, $content); return $result; } protected function renderDecorators($decorators, $content) { /** @var \Zend_Form_Decorator_Interface $decorator */ foreach ($decorators as $decorator) { $content = $decorator->render($content); } return $content; } protected function _getDecorators(array $decoratorsInfo) { $decorators = []; foreach ($decoratorsInfo as $decoratorInfo) { $decorators[] = $this->_getDecorator($decoratorInfo); } return $decorators; } /** * @param $decoratorInfo * @return $this|Zend_Form_Decorator_Interface * @throws Zend_Form_Exception */ protected function _getDecorator($decoratorInfo) { if ($decoratorInfo instanceof Zend_Form_Decorator_Interface) { return $decoratorInfo; } elseif (is_string($decoratorInfo)) { return $this->_loadDecorator($decoratorInfo); } elseif (is_array($decoratorInfo)) { $argc = count($decoratorInfo); $options = []; if (isset($decoratorInfo['decorator'])) { $decorator = $decoratorInfo['decorator']; if (isset($decoratorInfo['options'])) { $options = $decoratorInfo['options']; } return $this->_loadDecorator($decorator, $options); } else { switch (true) { case (0 == $argc): break; case (1 <= $argc): $decorator = array_shift($decoratorInfo); case (2 <= $argc): $options = array_shift($decoratorInfo); default: return $this->_loadDecorator($decorator, $options); break; } } } else { throw new Zend_Form_Exception('Invalid wrapper decorator'); } return $this; } protected function _loadDecorator($name, $options = []) { $class = $this->getElement()->getPluginLoader(Qs_Form::DECORATOR)->load($name); if (null === $options) { $decorator = new $class; } else { $decorator = new $class($options); } $name = $decorator->getOption('name'); $decorator->setElement(new Zend_Form_Element_Hidden($name ? $name : 'custom')); return $decorator; } }