PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/library/Zend/Form/Decorator/Label.php

http://github.com/zendframework/zf2
PHP | 332 lines | 174 code | 36 blank | 122 comment | 33 complexity | 140e3e247da8104c8f7a67ffe630eced 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_Label
  27. *
  28. * Accepts the options:
  29. * - separator: separator to use between label and content (defaults to PHP_EOL)
  30. * - placement: whether to append or prepend label to content (defaults to prepend)
  31. * - tag: if set, used to wrap the label in an additional HTML tag
  32. * - opt(ional)Prefix: a prefix to the label to use when the element is optional
  33. * - opt(iona)lSuffix: a suffix to the label to use when the element is optional
  34. * - req(uired)Prefix: a prefix to the label to use when the element is required
  35. * - req(uired)Suffix: a suffix to the label to use when the element is required
  36. *
  37. * Any other options passed will be used as HTML attributes of the label tag.
  38. *
  39. * @uses \Zend\Form\Decorator\AbstractDecorator
  40. * @uses \Zend\Form\Decorator\HtmlTag
  41. * @uses \Zend\Form\Decorator\Exception
  42. * @category Zend
  43. * @package Zend_Form
  44. * @subpackage Decorator
  45. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  46. * @license http://framework.zend.com/license/new-bsd New BSD License
  47. */
  48. class Label extends AbstractDecorator
  49. {
  50. /**
  51. * Default placement: prepend
  52. * @var string
  53. */
  54. protected $_placement = 'PREPEND';
  55. /**
  56. * HTML tag with which to surround label
  57. * @var string
  58. */
  59. protected $_tag;
  60. /**
  61. * Set element ID
  62. *
  63. * @param string $id
  64. * @return \Zend\Form\Decorator\Label
  65. */
  66. public function setId($id)
  67. {
  68. $this->setOption('id', $id);
  69. return $this;
  70. }
  71. /**
  72. * Retrieve element ID (used in 'for' attribute)
  73. *
  74. * If none set in decorator, looks first for element 'id' attribute, and
  75. * defaults to element name.
  76. *
  77. * @return string
  78. */
  79. public function getId()
  80. {
  81. $id = $this->getOption('id');
  82. if (null === $id) {
  83. if (null !== ($element = $this->getElement())) {
  84. $id = $element->getId();
  85. $this->setId($id);
  86. }
  87. }
  88. return $id;
  89. }
  90. /**
  91. * Set HTML tag with which to surround label
  92. *
  93. * @param string $tag
  94. * @return \Zend\Form\Decorator\Label
  95. */
  96. public function setTag($tag)
  97. {
  98. if (empty($tag)) {
  99. $this->_tag = null;
  100. } else {
  101. $this->_tag = (string) $tag;
  102. }
  103. $this->removeOption('tag');
  104. return $this;
  105. }
  106. /**
  107. * Get HTML tag, if any, with which to surround label
  108. *
  109. * @return void
  110. */
  111. public function getTag()
  112. {
  113. if (null === $this->_tag) {
  114. $tag = $this->getOption('tag');
  115. if (null !== $tag) {
  116. $this->removeOption('tag');
  117. $this->setTag($tag);
  118. }
  119. return $tag;
  120. }
  121. return $this->_tag;
  122. }
  123. /**
  124. * Get class with which to define label
  125. *
  126. * Appends either 'optional' or 'required' to class, depending on whether
  127. * or not the element is required.
  128. *
  129. * @return string
  130. */
  131. public function getClass()
  132. {
  133. $class = '';
  134. $element = $this->getElement();
  135. $decoratorClass = $this->getOption('class');
  136. if (!empty($decoratorClass)) {
  137. $class .= ' ' . $decoratorClass;
  138. }
  139. $type = $element->isRequired() ? 'required' : 'optional';
  140. if (!strstr($class, $type)) {
  141. $class .= ' ' . $type;
  142. $class = trim($class);
  143. }
  144. return $class;
  145. }
  146. /**
  147. * Load an optional/required suffix/prefix key
  148. *
  149. * @param string $key
  150. * @return void
  151. */
  152. protected function _loadOptReqKey($key)
  153. {
  154. if (!isset($this->$key)) {
  155. $value = $this->getOption($key);
  156. $this->$key = (string) $value;
  157. if (null !== $value) {
  158. $this->removeOption($key);
  159. }
  160. }
  161. }
  162. /**
  163. * Overloading
  164. *
  165. * Currently overloads:
  166. *
  167. * - getOpt(ional)Prefix()
  168. * - getOpt(ional)Suffix()
  169. * - getReq(uired)Prefix()
  170. * - getReq(uired)Suffix()
  171. * - setOpt(ional)Prefix()
  172. * - setOpt(ional)Suffix()
  173. * - setReq(uired)Prefix()
  174. * - setReq(uired)Suffix()
  175. *
  176. * @param string $method
  177. * @param array $args
  178. * @return mixed
  179. * @throws \Zend\Form\Decorator\Exception for unsupported methods
  180. */
  181. public function __call($method, $args)
  182. {
  183. $tail = substr($method, -6);
  184. $head = substr($method, 0, 3);
  185. if (in_array($head, array('get', 'set'))
  186. && (('Prefix' == $tail) || ('Suffix' == $tail))
  187. ) {
  188. $position = substr($method, -6);
  189. $type = strtolower(substr($method, 3, 3));
  190. switch ($type) {
  191. case 'req':
  192. $key = 'required' . $position;
  193. break;
  194. case 'opt':
  195. $key = 'optional' . $position;
  196. break;
  197. default:
  198. throw new Exception\BadMethodCallException(sprintf('Invalid method "%s" called in Label decorator, and detected as type %s', $method, $type));
  199. }
  200. switch ($head) {
  201. case 'set':
  202. if (0 === count($args)) {
  203. throw new Exception\InvalidArgumentException(sprintf('Method "%s" requires at least one argument; none provided', $method));
  204. }
  205. $value = array_shift($args);
  206. $this->$key = $value;
  207. return $this;
  208. case 'get':
  209. default:
  210. if (null === ($element = $this->getElement())) {
  211. $this->_loadOptReqKey($key);
  212. } elseif (isset($element->$key)) {
  213. $this->$key = (string) $element->$key;
  214. } else {
  215. $this->_loadOptReqKey($key);
  216. }
  217. return $this->$key;
  218. }
  219. }
  220. throw new Exception\BadMethodCallException(sprintf('Invalid method "%s" called in Label decorator', $method));
  221. }
  222. /**
  223. * Get label to render
  224. *
  225. * @return void
  226. */
  227. public function getLabel()
  228. {
  229. if (null === ($element = $this->getElement())) {
  230. return '';
  231. }
  232. $label = $element->getLabel();
  233. $label = trim($label);
  234. if (empty($label)) {
  235. return '';
  236. }
  237. if (null !== ($translator = $element->getTranslator())) {
  238. $label = $translator->translate($label);
  239. }
  240. $optPrefix = $this->getOptPrefix();
  241. $optSuffix = $this->getOptSuffix();
  242. $reqPrefix = $this->getReqPrefix();
  243. $reqSuffix = $this->getReqSuffix();
  244. $separator = $this->getSeparator();
  245. if (!empty($label)) {
  246. if ($element->isRequired()) {
  247. $label = $reqPrefix . $label . $reqSuffix;
  248. } else {
  249. $label = $optPrefix . $label . $optSuffix;
  250. }
  251. }
  252. return $label;
  253. }
  254. /**
  255. * Render a label
  256. *
  257. * @param string $content
  258. * @return string
  259. */
  260. public function render($content)
  261. {
  262. $element = $this->getElement();
  263. $view = $element->getView();
  264. if (null === $view) {
  265. return $content;
  266. }
  267. $label = $this->getLabel();
  268. $separator = $this->getSeparator();
  269. $placement = $this->getPlacement();
  270. $tag = $this->getTag();
  271. $id = $this->getId();
  272. $class = $this->getClass();
  273. $options = $this->getOptions();
  274. if (empty($label) && empty($tag)) {
  275. return $content;
  276. }
  277. if (!empty($label)) {
  278. $options['class'] = $class;
  279. $label = $view->formLabel($element->getFullyQualifiedName(), trim($label), $options);
  280. } else {
  281. $label = '&#160;';
  282. }
  283. if (null !== $tag) {
  284. $decorator = new HtmlTag();
  285. $decorator->setOptions(array('tag' => $tag,
  286. 'id' => $id . '-label'));
  287. $label = $decorator->render($label);
  288. }
  289. switch ($placement) {
  290. case self::APPEND:
  291. return $content . $separator . $label;
  292. case self::PREPEND:
  293. return $label . $separator . $content;
  294. }
  295. }
  296. }