| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Win\Html\Form; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Win\Html\Html; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * Select | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * <select> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | class Select extends Html { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | 	protected $options; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | 	protected $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | 	 * Retorna "selected" se os valores são iguais | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | 	 * @param mixed $value1 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | 	 * @param mixed $value2 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | 	 * @return string | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 22 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  | 	public static function select($value1, $value2 = true) { | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  | 		return ($value1 == $value2) ? 'selected ' : ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 	 * Cria um <select> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 	 * @param string $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 	 * @param string[] $options | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 	 * @param string|int $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 	 * @param string[] $attributes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 	public function __construct($name, $options, $value = null, $attributes = []) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 		if (in_array($value, $options)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 			$value = array_search($value, $options); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 		$this->options = $options; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 		$this->value = $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 		parent::__construct($name, $attributes); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 	/** @return string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 	public function getValue() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 		$value = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 		if (key_exists($this->value, $this->options)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 			$value = $this->options[$this->value]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 		return $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 	/** @return string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 	public function html() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 		return '<select name="' . $this->name . '" ' . $this->attributes() . '> ' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 				. $this->htmlContent() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 				. '</select>'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 	 * Retorna o HTML dos <options> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 	 * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 	public function htmlContent() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 		return $this->htmlOptions($this->options); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 	/** @return string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 	protected function htmlOptions($options = []) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 		$html = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 		foreach ($options as $value => $option) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 			if (is_string($option)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 				$html .= '<option ' . static::select($value, $this->value) . 'value="' . $value . '">' . $option . '</option> '; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 			} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 				$html .= '<optgroup label="' . $value . '"> ' . $this->htmlOptions($option) . '</optgroup>'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 		return $html; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 80 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 81 |  |  |  |