-
Notifications
You must be signed in to change notification settings - Fork 0
/
Input.php
65 lines (55 loc) · 2.27 KB
/
Input.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
namespace Teknicode\Form;
class Input{
private $attributes = array("class"=>"form-control");
public $width;
function __construct($width){
$this->width=$width;
}
public function set(){
if(func_num_args() == 2) {
$key = func_get_arg(0);
$value = func_get_arg(1);
if ($key == "class" && !in_array($this->get('type'),["radio","checkbox"])) {
$value = "form-control " . $value;
}
$this->attributes[$key] = $value;
}else{
foreach(func_get_args()[0] as $key => $value){
if($key == "class" && !in_array($this->get('type'),["radio","checkbox"]))$value = "form-control ".$value;
$this->attributes[$key]=$value;
}
}
return $this;
}
public function get($key){
return (isset($this->attributes[$key]) ? $this->attributes[$key] : null);
}
public function html(){
$atts = '';
$return = '<div class="form-group">'.($this->get('label')?'<label'.($this->get('id')? ' for="'.$this->get('id').'"':'').'>'.$this->get('label').'</label> ':'');
if(in_array($this->get('type'),["radio","checkbox"])){
$this->set("class",str_replace("form-control","",$this->get('class')));
}
unset($this->attributes['label']);
foreach($this->attributes as $attribute => $value){
if($attribute!="options") {
if(in_array($this->get('type'),["textarea","radio"]) && in_array($attribute,["value","type"])) continue;
$atts .= (!empty($atts) ? ' ' : '') . $attribute . '="' . $value . '"';
}
}
if($this->get('type') == "textarea"){
$return .= '<textarea '.$atts.'>'.$this->get('value').'</textarea>';
}elseif($this->get('type') == "radio"){
$return .= '<div class="bg-white p-2">';
foreach($this->get('options') as $label => $value){
$return .= $label.' <input type="radio" '.$atts.' value="'.$value.'"'.($this->get("value")==$value?' checked="checked"':'').'/> ';
}
$return .= '</div>';
}else{
$return .= '<input '.$atts.'/>';
}
$return .= '</div>';
return $return;
}
}