-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractArguments.php
More file actions
executable file
·218 lines (194 loc) · 4.83 KB
/
AbstractArguments.php
File metadata and controls
executable file
·218 lines (194 loc) · 4.83 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<?php
/**
* @Package: MaplePHP - Form builder engine
* @Author: Daniel Ronkainen
* @Licence: Apache-2.0 license, Copyright © Daniel Ronkainen
Don't delete this comment, its part of the license.
*/
namespace MaplePHP\Form;
use MaplePHP\Form\Interfaces\FieldInterface;
abstract class AbstractArguments
{
protected $inst;
protected $attr;
protected $fieldType;
protected $attrArr = array();
protected $name;
protected $value;
protected $default;
protected $label;
protected $description;
protected $validate = array();
protected $items = array();
protected $config = array();
protected $fields = array();
protected $identifier;
/**
* Main field instance
* @param FieldInterface $inst
* @return void
*/
public function setFieldInst(FieldInterface $inst): void
{
$this->inst = $inst;
}
/**
* Get request (Get filtered requests)
* @return mixed
*/
public function getRequest(): mixed
{
return $this->inst->request;
}
/**
* Get all values
* @return mixed
*/
public function getValues(): mixed
{
return $this->inst->getValues();
}
/**
* Same as above
* @return mixed
*/
public function values(): mixed
{
return $this->getValues();
}
/**
* Get value
* @return mixed
*/
public function getValue(): mixed
{
return $this->value;
}
/**
* Same as above
* @return mixed
*/
public function value(): mixed
{
return $this->getValue();
}
/**
* Get field type
* @return string
*/
public function getFieldType(): ?string
{
return $this->fieldType;
}
/**
* Get name
* @return string
*/
public function getName(): ?string
{
return $this->name;
}
/**
* Get default value
* @return string
*/
public function getDefault(): ?string
{
return $this->default;
}
/**
* Get form field
* @return FieldInterface
*/
public function get()
{
$this->value();
return $this->inst->get();
}
protected function setAttr(): string
{
$this->attr = "";
foreach ($this->attrArr as $key => $value) {
$this->attr .= "{$key}=\"{$value}\" ";
}
return $this->attr;
}
/**
* Group fields / custom fields with dynamic and nested fields names
* @param callable $callback Container room for customization
* @param bool $manipulateName Manipulate the input field name
* @return string
*/
protected function groupFields(callable $callback, bool $manipulateName = true)
{
$out = "";
if (!is_array($this->value)) {
$this->value = array(0);
} // This will add new value
foreach ($this->value as $k => $a) {
$outB = "";
foreach ($this->fields as $name => $arr) {
$fieldKey = ($manipulateName) ? "{$this->identifier},{$k},{$name}" : $name;
$outB .= $this->inst->html([$fieldKey => $arr]);
}
$out .= $callback($outB, $a);
}
return $out;
}
/**
* This will inherit the parent name and build upon it.
* @return string
*/
protected function inheritField()
{
$out = "";
foreach ($this->fields as $name => $arr) {
$fieldKey = "{$this->identifier},{$name}";
$out .= $this->inst->html([$fieldKey => $arr]);
}
return $out;
}
/**
* Check if filed is checked/active
* @param string $val
* @return boolean
*/
protected function isChecked($val): bool
{
if (is_array($this->value)) {
return (bool)in_array((string)$val, $this->value);
}
return ((string)$val === (string)$this->value);
}
/**
* Get the last key can be used with @groupFields to create dynamic custom fields
* Can be used to make the dynamic input name alwas uniqe
* @return int
*/
protected function lastKey(): int
{
$findKey = 0;
if (!is_null($this->value) && is_array($this->value)) {
$findKey = $this->value;
krsort($findKey);
$findKey = key($findKey);
}
return $findKey;
}
/**
* Used in to help make sence of validate data
* @param mixed $jsonStr
* @return mixed
*/
final protected function json($jsonStr)
{
if (is_string($jsonStr)) {
if ($data = json_decode($jsonStr, true)) {
return $data;
} else {
throw new \Exception("JSON ERROR CODE: " . json_last_error(), 1);
}
}
return $jsonStr;
}
}