1 2/** 3 * This file is part of the Phalcon. 4 * 5 * (c) Phalcon Team <team@phalcon.com> 6 * 7 * For the full copyright and license information, please view the LICENSE 8 * file that was distributed with this source code. 9 */ 10 11namespace Phalcon\Html\Helper\Input; 12 13use Phalcon\Escaper\EscaperInterface; 14use Phalcon\Helper\Arr; 15 16/** 17 * Class Checkbox 18 * 19 * @property array $label 20 */ 21class Checkbox extends AbstractInput 22{ 23 /** 24 * @var array 25 */ 26 protected label = []; 27 28 /** 29 * @var string 30 */ 31 protected type = "checkbox"; 32 33 /** 34 * AbstractHelper constructor. 35 * 36 * @param EscaperInterface $escaper 37 */ 38 public function __construct(<EscaperInterface> escaper) 39 { 40 parent::__construct(escaper); 41 42 let this->label = [ 43 "start" : "", 44 "text" : "", 45 "end" : "" 46 ]; 47 } 48 49 /** 50 * Returns the HTML for the input. 51 * 52 * @return string 53 */ 54 public function __toString() 55 { 56 var element, label, unchecked; 57 58 this->processChecked(); 59 60 let unchecked = this->processUnchecked(), 61 element = parent::__toString(), 62 label = this->label, 63 this->label = [ 64 "start" : "", 65 "text" : "", 66 "end" : "" 67 ]; 68 69 return unchecked 70 . label["start"] 71 . element 72 . label["text"] 73 . label["end"]; 74 } 75 76 /** 77 * Attaches a label to the element 78 * 79 * @param array $attributes 80 * 81 * @return Checkbox 82 */ 83 public function label(array attributes = []) -> <Checkbox> 84 { 85 var text = ""; 86 87 let text = Arr::get(attributes, "text", ""); 88 89 unset attributes["text"]; 90 91 let attributes = array_merge( 92 [ 93 "for" : this->attributes["id"] 94 ], 95 attributes 96 ); 97 98 let this->label = [ 99 "start" : this->renderTag("label", attributes), 100 "text" : text, 101 "end" : "</label>" 102 ]; 103 104 return this; 105 } 106 107 /** 108 * Processes the checked value 109 */ 110 private function processChecked() -> void 111 { 112 var checked, value; 113 array attributes; 114 115 let attributes = this->attributes, 116 checked = Arr::get(attributes, "checked", ""); 117 118 unset attributes["checked"]; 119 120 if !empty checked { 121 let value = Arr::get(attributes, "value", ""); 122 if checked === value { 123 let attributes["checked"] = "checked"; 124 } 125 } 126 127 let this->attributes = attributes; 128 } 129 130 /** 131 * Returns the unchecked hidden element if available 132 * 133 * @return string 134 */ 135 private function processUnchecked() -> string 136 { 137 var unchecked; 138 array attributes; 139 140 let attributes = this->attributes, 141 unchecked = Arr::get(attributes, "unchecked", ""); 142 143 unset attributes["unchecked"]; 144 145 if !empty unchecked { 146 let unchecked = this->renderTag( 147 "hidden", 148 [ 149 "name" : this->attributes["name"], 150 "value" : unchecked 151 ] 152 ); 153 } 154 155 let this->attributes = attributes; 156 157 return unchecked; 158 } 159} 160