PageRenderTime 38ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Zend/Form/Decorator/Fieldset.php

http://github.com/zendframework/zf2
PHP | 172 lines | 79 code | 17 blank | 76 comment | 13 complexity | 6d159636e271911915ada7fd2fe96f4f MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Form
  17. * @subpackage Decorator
  18. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * @namespace
  23. */
  24. namespace Zend\Form\Decorator;
  25. /**
  26. * Zend_Form_Decorator_Fieldset
  27. *
  28. * Any options passed will be used as HTML attributes of the fieldset tag.
  29. *
  30. * @uses \Zend\Form\Decorator\AbstractDecorator
  31. * @category Zend
  32. * @package Zend_Form
  33. * @subpackage Decorator
  34. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. */
  37. class Fieldset extends AbstractDecorator
  38. {
  39. /**
  40. * Attribs that should be removed prior to rendering
  41. * @var array
  42. */
  43. public $stripAttribs = array(
  44. 'action',
  45. 'enctype',
  46. 'helper',
  47. 'method',
  48. 'name',
  49. );
  50. /**
  51. * Fieldset legend
  52. * @var string
  53. */
  54. protected $_legend;
  55. /**
  56. * Default placement: surround content
  57. * @var string
  58. */
  59. protected $_placement = null;
  60. /**
  61. * Get option
  62. *
  63. * @param string $key
  64. * @return mixed
  65. */
  66. public function getOption($key)
  67. {
  68. if (null !== $this->_element) {
  69. $this->getOptions();
  70. }
  71. return parent::getOption($key);
  72. }
  73. /**
  74. * Get options
  75. *
  76. * Merges in element attributes as well.
  77. *
  78. * @return array
  79. */
  80. public function getOptions()
  81. {
  82. $options = parent::getOptions();
  83. if (null !== ($element = $this->getElement())) {
  84. $attribs = $element->getAttribs();
  85. $options = array_merge($attribs, $options);
  86. $this->setOptions($options);
  87. }
  88. return $options;
  89. }
  90. /**
  91. * Set legend
  92. *
  93. * @param string $value
  94. * @return \Zend\Form\Decorator\Fieldset
  95. */
  96. public function setLegend($value)
  97. {
  98. $this->_legend = (string) $value;
  99. return $this;
  100. }
  101. /**
  102. * Get legend
  103. *
  104. * @return string
  105. */
  106. public function getLegend()
  107. {
  108. $legend = $this->_legend;
  109. if ((null === $legend) && (null !== ($element = $this->getElement()))) {
  110. if (method_exists($element, 'getLegend')) {
  111. $legend = $element->getLegend();
  112. $this->setLegend($legend);
  113. }
  114. }
  115. if ((null === $legend) && (null !== ($legend = $this->getOption('legend')))) {
  116. $this->setLegend($legend);
  117. $this->removeOption('legend');
  118. }
  119. return $legend;
  120. }
  121. /**
  122. * Render a fieldset
  123. *
  124. * @param string $content
  125. * @return string
  126. */
  127. public function render($content)
  128. {
  129. $element = $this->getElement();
  130. $view = $element->getView();
  131. if (null === $view) {
  132. return $content;
  133. }
  134. $legend = $this->getLegend();
  135. $attribs = $this->getOptions();
  136. $name = $element->getFullyQualifiedName();
  137. $id = (string)$element->getId();
  138. if (!array_key_exists('id', $attribs) && '' !== $id) {
  139. $attribs['id'] = 'fieldset-' . $id;
  140. }
  141. if (null !== $legend) {
  142. if (null !== ($translator = $element->getTranslator())) {
  143. $legend = $translator->translate($legend);
  144. }
  145. $attribs['legend'] = $legend;
  146. }
  147. foreach (array_keys($attribs) as $attrib) {
  148. $testAttrib = strtolower($attrib);
  149. if (in_array($testAttrib, $this->stripAttribs)) {
  150. unset($attribs[$attrib]);
  151. }
  152. }
  153. return $view->fieldset($name, $content, $attribs);
  154. }
  155. }