/library/Zend/Form/Element/Checkbox.php

http://github.com/zendframework/zf2 · PHP · 205 lines · 77 code · 17 blank · 111 comment · 8 complexity · 9d3500cbc45f9752f0010adbbdf56a75 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 Element
  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\Element;
  25. /**
  26. * Checkbox form element
  27. *
  28. * @uses \Zend\Form\Element\Xhtml
  29. * @category Zend
  30. * @package Zend_Form
  31. * @subpackage Element
  32. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. class Checkbox extends Xhtml
  36. {
  37. /**
  38. * Is the checkbox checked?
  39. * @var bool
  40. */
  41. public $checked = false;
  42. /**
  43. * Use formCheckbox view helper by default
  44. * @var string
  45. */
  46. public $helper = 'formCheckbox';
  47. /**
  48. * Options that will be passed to the view helper
  49. * @var array
  50. */
  51. public $options = array(
  52. 'checkedValue' => '1',
  53. 'uncheckedValue' => '0',
  54. );
  55. /**
  56. * Value when checked
  57. * @var string
  58. */
  59. protected $_checkedValue = '1';
  60. /**
  61. * Value when not checked
  62. * @var string
  63. */
  64. protected $_uncheckedValue = '0';
  65. /**
  66. * Current value
  67. * @var string 0 or 1
  68. */
  69. protected $_value = '0';
  70. /**
  71. * Set options
  72. *
  73. * Intercept checked and unchecked values and set them early; test stored
  74. * value against checked and unchecked values after configuration.
  75. *
  76. * @param array $options
  77. * @return \Zend\Form\Element\Checkbox
  78. */
  79. public function setOptions(array $options)
  80. {
  81. if (array_key_exists('checkedValue', $options)) {
  82. $this->setCheckedValue($options['checkedValue']);
  83. unset($options['checkedValue']);
  84. }
  85. if (array_key_exists('uncheckedValue', $options)) {
  86. $this->setUncheckedValue($options['uncheckedValue']);
  87. unset($options['uncheckedValue']);
  88. }
  89. parent::setOptions($options);
  90. $curValue = $this->getValue();
  91. $test = array($this->getCheckedValue(), $this->getUncheckedValue());
  92. if (!in_array($curValue, $test)) {
  93. $this->setValue($curValue);
  94. }
  95. return $this;
  96. }
  97. /**
  98. * Set value
  99. *
  100. * If value matches checked value, sets to that value, and sets the checked
  101. * flag to true.
  102. *
  103. * Any other value causes the unchecked value to be set as the current
  104. * value, and the checked flag to be set as false.
  105. *
  106. *
  107. * @param mixed $value
  108. * @return \Zend\Form\Element\Checkbox
  109. */
  110. public function setValue($value)
  111. {
  112. if ($value == $this->getCheckedValue()) {
  113. parent::setValue($value);
  114. $this->checked = true;
  115. } else {
  116. parent::setValue($this->getUncheckedValue());
  117. $this->checked = false;
  118. }
  119. return $this;
  120. }
  121. /**
  122. * Set checked value
  123. *
  124. * @param string $value
  125. * @return \Zend\Form\Element\Checkbox
  126. */
  127. public function setCheckedValue($value)
  128. {
  129. $this->_checkedValue = (string) $value;
  130. $this->options['checkedValue'] = $value;
  131. return $this;
  132. }
  133. /**
  134. * Get value when checked
  135. *
  136. * @return string
  137. */
  138. public function getCheckedValue()
  139. {
  140. return $this->_checkedValue;
  141. }
  142. /**
  143. * Set unchecked value
  144. *
  145. * @param string $value
  146. * @return \Zend\Form\Element\Checkbox
  147. */
  148. public function setUncheckedValue($value)
  149. {
  150. $this->_uncheckedValue = (string) $value;
  151. $this->options['uncheckedValue'] = $value;
  152. return $this;
  153. }
  154. /**
  155. * Get value when not checked
  156. *
  157. * @return string
  158. */
  159. public function getUncheckedValue()
  160. {
  161. return $this->_uncheckedValue;
  162. }
  163. /**
  164. * Set checked flag
  165. *
  166. * @param bool $flag
  167. * @return \Zend\Form\Element\Checkbox
  168. */
  169. public function setChecked($flag)
  170. {
  171. $this->checked = (bool) $flag;
  172. if ($this->checked) {
  173. $this->setValue($this->getCheckedValue());
  174. } else {
  175. $this->setValue($this->getUncheckedValue());
  176. }
  177. return $this;
  178. }
  179. /**
  180. * Get checked flag
  181. *
  182. * @return bool
  183. */
  184. public function isChecked()
  185. {
  186. return $this->checked;
  187. }
  188. }