1 | <?php |
||
9 | class Formaggio |
||
10 | { |
||
11 | /** @var array */ |
||
12 | private $fields = []; |
||
13 | /** @var array */ |
||
14 | private $formAttributes = []; |
||
15 | |||
16 | /** |
||
17 | * Formaggio constructor. |
||
18 | * @param string $action |
||
19 | * @param string $method |
||
20 | * @param array $attributes |
||
21 | */ |
||
22 | 9 | public function __construct($action = "/", $method = "post", $attributes = []) |
|
28 | |||
29 | /** |
||
30 | * @param $attributes |
||
31 | * @return array |
||
32 | */ |
||
33 | 9 | private function clearAttributesForm($attributes): array |
|
38 | |||
39 | /** |
||
40 | * @param int $index |
||
41 | * @return mixed|null |
||
42 | */ |
||
43 | 7 | public function getField($index = 0) |
|
47 | |||
48 | 1 | public function render() |
|
49 | { |
||
50 | 1 | echo $this->get() . PHP_EOL; |
|
51 | 1 | } |
|
52 | |||
53 | /** |
||
54 | * @return string |
||
55 | */ |
||
56 | 2 | public function get(): string |
|
57 | { |
||
58 | 2 | return $this->getFormRendered($this->formAttributes); |
|
59 | } |
||
60 | |||
61 | /** |
||
62 | * @param array $attributes |
||
63 | * @return string |
||
64 | */ |
||
65 | 2 | private function getFormRendered($attributes = []) |
|
66 | { |
||
67 | 2 | $output = '<form ' . $this->buildAttributes($attributes) . '>'; |
|
68 | 2 | $output .= $this->getFieldsRendered($this->fields); |
|
69 | 2 | $output .= '</form>'; |
|
70 | 2 | return $output; |
|
71 | } |
||
72 | |||
73 | /** |
||
74 | * @param $attributes |
||
75 | * @return string |
||
76 | */ |
||
77 | 2 | private function buildAttributes($attributes) |
|
78 | { |
||
79 | 2 | $rawAttr = []; |
|
80 | 2 | foreach ($attributes as $key => $value) { |
|
81 | 2 | $rawAttr[] = $key . '="' . $value . '"'; |
|
82 | } |
||
83 | 2 | return implode(' ', $rawAttr); |
|
84 | } |
||
85 | |||
86 | /** |
||
87 | * @param $fields |
||
88 | * @return string |
||
89 | */ |
||
90 | 2 | private function getFieldsRendered($fields) |
|
91 | { |
||
92 | 2 | $output = ''; |
|
93 | 2 | foreach ($fields as $field) { |
|
94 | 2 | $output .= $this->getFieldRendered($field); |
|
95 | } |
||
96 | 2 | return $output; |
|
97 | } |
||
98 | |||
99 | /** |
||
100 | * @param $field |
||
101 | * @param string $tag |
||
102 | * @return string |
||
103 | */ |
||
104 | 2 | private function getFieldRendered($field, $tag = 'input') |
|
105 | { |
||
106 | 2 | return "<$tag " . $this->buildAttributes($field) . '>'; |
|
107 | } |
||
108 | |||
109 | /** |
||
110 | * @param string $name |
||
111 | * @param array $attributes |
||
112 | * @return $this |
||
113 | */ |
||
114 | 5 | public function text($name = "text", $attributes = array()) |
|
119 | |||
120 | 9 | private function addFieldByType($type, $name, $attributes) |
|
130 | |||
131 | /** |
||
132 | * @param $attributes |
||
133 | * @return mixed |
||
134 | */ |
||
135 | 9 | private function clearAttributesInput($attributes) |
|
140 | |||
141 | /** |
||
142 | * @param string $name |
||
143 | * @param array $attributes |
||
144 | * @return $this |
||
145 | */ |
||
146 | 2 | public function email($name = "email", $attributes = array()) |
|
151 | |||
152 | /** |
||
153 | * @param string $name |
||
154 | * @param array $attributes |
||
155 | * @return $this |
||
156 | */ |
||
157 | 2 | public function password($name = "password", $attributes = array()) |
|
162 | |||
163 | /** |
||
164 | * @param $placeholder |
||
165 | * @return $this |
||
166 | */ |
||
167 | 1 | public function place($placeholder) |
|
172 | } |