-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractFields.php
More file actions
executable file
·175 lines (159 loc) · 4.53 KB
/
AbstractFields.php
File metadata and controls
executable file
·175 lines (159 loc) · 4.53 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
<?php
/**
* @Package: MaplePHP - Form builder
* @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\FormFieldsInterface;
use MaplePHP\Form\Interfaces\FieldInterface;
abstract class AbstractFields
{
protected const LOAD_FIELD_METHODS = [
"label" => null,
"description" => null,
"config" => [],
"items" => [],
"value" => null,
"fields" => [],
"attr" => []
];
protected $name = "form";
protected $fields;
protected $type;
protected $args = array();
protected $values = array();
protected $inpArr = array();
/**
* Form creator
* @param FormFieldsInterface $fields Form template class
*/
public function __construct(FormFieldsInterface $fields)
{
$this->fields = $fields;
}
/**
* Quick create and return field (Chainable resource)
* @param string $method
* @param array $args
* @return FormFieldsInterface
*/
public function __call($method, $args): FormFieldsInterface
{
// Reset fields instance
if (!is_null($this->type)) {
$this->fields = $this->fields->withField();
}
if ($this instanceof FieldInterface) {
$this->fields->setFieldInst($this);
}
$this->type = $method;
$this->args = $args;
return $this->fields;
}
/**
* Get a list of supported field configuration
* @return array
*/
public function getConfigs(): array
{
return static::LOAD_FIELD_METHODS;
}
/**
* You can create a new form
* @param string $name
* @return self
*/
public function withForm(string $name): self
{
$clone = clone $this;
$clone->name = $name;
return $clone;
}
/**
* You can split the form into multiple partials with the help with withForm or new instance
* Every form partial will then be validate
* @param FieldInterface $inst
* @return self
*/
public function setPartial(FieldInterface $inst): self
{
$this->add($inst->getFields(), $inst->getFormName());
return $this;
}
/**
* Set values
* @param array|object $values Will be converted to array
*/
public function setValues(array|object $values): void
{
$values = (array)$values;
foreach ($values as $k => $val) {
if (!is_array($val)) {
$val = (string)$val;
}
$this->values[$k] = $val;
}
}
/**
* Delete search and find a array item
* @param array $key Possible to traverse to form field with the comma select property
* @return void
*/
final protected function findDelete(array &$array, array $key): void
{
$firstKey = array_shift($key);
if (isset($array[$firstKey])) {
if (count($key) > 0) {
$this->findDelete($array[$firstKey], $key);
} else {
unset($array[$firstKey]);
}
}
}
/**
* Make grouped template fields into grouped fields
* @return array
*/
final protected function resolveGrpName(): array
{
$get = array();
foreach ($this->inpArr as $a1) {
foreach ($a1 as $k => $a2) {
if (isset($a2['type'])) {
if (isset($a2['fields'])) {
switch ($a2['type']) {
case "group":
$this->resolveNameNest($a2['fields'], $get, $k);
break;
}
} else {
$get[$k] = $a2;
}
}
}
}
return $get;
}
/**
* Set input fields name multidimensional
* @param array $array
* @param array &$get
* @param string $keyA
* @return void
*/
private function resolveNameNest(array $array, array &$get, string $keyA): void
{
foreach ($array as $keyB => $row) {
if (isset($row['type'])) {
$newKey = ($keyA) ? $keyA . ",{$keyB}" : $keyB;
if (isset($row['fields'])) {
$this->resolveNameNest($row['fields'], $get, $newKey);
} else {
$get[$newKey] = $row;
}
}
}
}
}