PageRenderTime 47ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/src/application/libraries/Zend/Form/Decorator/Description.php

https://bitbucket.org/masnug/grc276-blog-laravel
PHP | 199 lines | 91 code | 22 blank | 86 comment | 14 complexity | 6e7868a9f98d1a15b3f7823c0dcc9939 MD5 | raw file
  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-2011 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /** Zend_Form_Decorator_Abstract */
  22. require_once 'Zend/Form/Decorator/Abstract.php';
  23. /**
  24. * Zend_Form_Decorator_Description
  25. *
  26. * Accepts the options:
  27. * - separator: separator to use between label and content (defaults to PHP_EOL)
  28. * - placement: whether to append or prepend label to content (defaults to prepend)
  29. * - tag: if set, used to wrap the label in an additional HTML tag
  30. * - class: if set, override default class used with HTML tag
  31. * - escape: whether or not to escape description (true by default)
  32. *
  33. * Any other options passed will be used as HTML attributes of the HTML tag used.
  34. *
  35. * @category Zend
  36. * @package Zend_Form
  37. * @subpackage Decorator
  38. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  39. * @license http://framework.zend.com/license/new-bsd New BSD License
  40. * @version $Id: Description.php 23775 2011-03-01 17:25:24Z ralph $
  41. */
  42. class Zend_Form_Decorator_Description extends Zend_Form_Decorator_Abstract
  43. {
  44. /**
  45. * Whether or not to escape the description
  46. * @var bool
  47. */
  48. protected $_escape;
  49. /**
  50. * Default placement: append
  51. * @var string
  52. */
  53. protected $_placement = 'APPEND';
  54. /**
  55. * HTML tag with which to surround description
  56. * @var string
  57. */
  58. protected $_tag;
  59. /**
  60. * Set HTML tag with which to surround description
  61. *
  62. * @param string $tag
  63. * @return Zend_Form_Decorator_Description
  64. */
  65. public function setTag($tag)
  66. {
  67. $this->_tag = (string) $tag;
  68. return $this;
  69. }
  70. /**
  71. * Get HTML tag, if any, with which to surround description
  72. *
  73. * @return string
  74. */
  75. public function getTag()
  76. {
  77. if (null === $this->_tag) {
  78. $tag = $this->getOption('tag');
  79. if (null !== $tag) {
  80. $this->removeOption('tag');
  81. } else {
  82. $tag = 'p';
  83. }
  84. $this->setTag($tag);
  85. return $tag;
  86. }
  87. return $this->_tag;
  88. }
  89. /**
  90. * Get class with which to define description
  91. *
  92. * Defaults to 'hint'
  93. *
  94. * @return string
  95. */
  96. public function getClass()
  97. {
  98. $class = $this->getOption('class');
  99. if (null === $class) {
  100. $class = 'hint';
  101. $this->setOption('class', $class);
  102. }
  103. return $class;
  104. }
  105. /**
  106. * Set whether or not to escape description
  107. *
  108. * @param bool $flag
  109. * @return Zend_Form_Decorator_Description
  110. */
  111. public function setEscape($flag)
  112. {
  113. $this->_escape = (bool) $flag;
  114. return $this;
  115. }
  116. /**
  117. * Get escape flag
  118. *
  119. * @return true
  120. */
  121. public function getEscape()
  122. {
  123. if (null === $this->_escape) {
  124. if (null !== ($escape = $this->getOption('escape'))) {
  125. $this->setEscape($escape);
  126. $this->removeOption('escape');
  127. } else {
  128. $this->setEscape(true);
  129. }
  130. }
  131. return $this->_escape;
  132. }
  133. /**
  134. * Render a description
  135. *
  136. * @param string $content
  137. * @return string
  138. */
  139. public function render($content)
  140. {
  141. $element = $this->getElement();
  142. $view = $element->getView();
  143. if (null === $view) {
  144. return $content;
  145. }
  146. $description = $element->getDescription();
  147. $description = trim($description);
  148. if (!empty($description) && (null !== ($translator = $element->getTranslator()))) {
  149. $description = $translator->translate($description);
  150. }
  151. if (empty($description)) {
  152. return $content;
  153. }
  154. $separator = $this->getSeparator();
  155. $placement = $this->getPlacement();
  156. $tag = $this->getTag();
  157. $class = $this->getClass();
  158. $escape = $this->getEscape();
  159. $options = $this->getOptions();
  160. if ($escape) {
  161. $description = $view->escape($description);
  162. }
  163. if (!empty($tag)) {
  164. require_once 'Zend/Form/Decorator/HtmlTag.php';
  165. $options['tag'] = $tag;
  166. $decorator = new Zend_Form_Decorator_HtmlTag($options);
  167. $description = $decorator->render($description);
  168. }
  169. switch ($placement) {
  170. case self::PREPEND:
  171. return $description . $separator . $content;
  172. case self::APPEND:
  173. default:
  174. return $content . $separator . $description;
  175. }
  176. }
  177. }