|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Formaggio; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class Formaggio |
|
7
|
|
|
* @package Formaggio |
|
8
|
|
|
*/ |
|
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 = []) |
|
23
|
|
|
{ |
|
24
|
9 |
|
$this->formAttributes = $this->clearAttributesForm($attributes); |
|
25
|
9 |
|
$this->formAttributes["action"] = $action; |
|
26
|
9 |
|
$this->formAttributes["method"] = $method; |
|
27
|
9 |
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param $attributes |
|
31
|
|
|
* @return array |
|
32
|
|
|
*/ |
|
33
|
9 |
|
private function clearAttributesForm($attributes): array |
|
34
|
|
|
{ |
|
35
|
9 |
|
unset($attributes['action'], $attributes['method']); |
|
36
|
9 |
|
return $attributes; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param int $index |
|
41
|
|
|
* @return mixed|null |
|
42
|
|
|
*/ |
|
43
|
7 |
|
public function getField($index = 0) |
|
44
|
|
|
{ |
|
45
|
7 |
|
return $this->fields[$index] ?? null; |
|
46
|
|
|
} |
|
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()) |
|
115
|
|
|
{ |
|
116
|
5 |
|
$this->addFieldByType('text', $name, $attributes); |
|
117
|
5 |
|
return $this; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
9 |
|
private function addFieldByType($type, $name, $attributes) |
|
121
|
|
|
{ |
|
122
|
9 |
|
$attributes = $this->clearAttributesInput($attributes); |
|
123
|
|
|
$field = [ |
|
124
|
9 |
|
"type" => $type, |
|
125
|
9 |
|
"name" => $name, |
|
126
|
|
|
]; |
|
127
|
9 |
|
$field = array_merge($field, $attributes); |
|
128
|
9 |
|
$this->fields[] = $field; |
|
129
|
9 |
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @param $attributes |
|
133
|
|
|
* @return mixed |
|
134
|
|
|
*/ |
|
135
|
9 |
|
private function clearAttributesInput($attributes) |
|
136
|
|
|
{ |
|
137
|
9 |
|
unset($attributes['name'], $attributes['type']); |
|
138
|
9 |
|
return $attributes; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* @param string $name |
|
143
|
|
|
* @param array $attributes |
|
144
|
|
|
* @return $this |
|
145
|
|
|
*/ |
|
146
|
2 |
|
public function email($name = "email", $attributes = array()) |
|
147
|
|
|
{ |
|
148
|
2 |
|
$this->addFieldByType('email', $name, $attributes); |
|
149
|
2 |
|
return $this; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* @param string $name |
|
154
|
|
|
* @param array $attributes |
|
155
|
|
|
* @return $this |
|
156
|
|
|
*/ |
|
157
|
2 |
|
public function password($name = "password", $attributes = array()) |
|
158
|
|
|
{ |
|
159
|
2 |
|
$this->addFieldByType('password', $name, $attributes); |
|
160
|
2 |
|
return $this; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* @param $placeholder |
|
165
|
|
|
* @return $this |
|
166
|
|
|
*/ |
|
167
|
1 |
|
public function place($placeholder) |
|
168
|
|
|
{ |
|
169
|
1 |
|
$this->fields[count($this->fields) - 1]['placeholder'] = $placeholder; |
|
170
|
1 |
|
return $this; |
|
171
|
|
|
} |
|
172
|
|
|
} |