1
|
|
|
<?php |
2
|
|
|
namespace WebServCo\Framework; |
3
|
|
|
|
4
|
|
|
abstract class AbstractForm extends \WebServCo\Framework\AbstractLibrary |
5
|
|
|
{ |
6
|
|
|
protected $errors; |
7
|
|
|
|
8
|
|
|
protected $filtered; |
9
|
|
|
|
10
|
|
|
protected $submitFields; |
11
|
|
|
|
12
|
|
|
protected $submitField; |
13
|
|
|
|
14
|
|
|
protected $valid; |
15
|
|
|
|
16
|
|
|
use \WebServCo\Framework\Traits\ExposeLibrariesTrait; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @return bool |
20
|
|
|
*/ |
21
|
|
|
abstract protected function filter(); |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @return bool |
25
|
|
|
*/ |
26
|
|
|
abstract protected function validate(); |
27
|
|
|
|
28
|
|
|
public function __construct($settings, $defaultData = [], $submitFields = []) |
29
|
|
|
{ |
30
|
|
|
parent::__construct($settings); |
31
|
|
|
|
32
|
|
|
$this->submitFields = $submitFields; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Set form data |
36
|
|
|
*/ |
37
|
|
|
foreach ($this->setting('meta', []) as $field => $title) { |
38
|
|
|
if ($this->isSent()) { |
39
|
|
|
$data = $this->request()->data($field, null); |
40
|
|
|
} else { |
41
|
|
|
$data = \WebServCo\Framework\Utils\Arrays::get($defaultData, $field, null); |
42
|
|
|
} |
43
|
|
|
$this->setData($field, $data); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$this->errors = []; |
47
|
|
|
|
48
|
|
|
$this->filtered = $this->filter(); |
49
|
|
|
|
50
|
|
|
if ($this->isSent()) { |
51
|
|
|
$this->valid = $this->validate(); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
final public function clear() |
56
|
|
|
{ |
57
|
|
|
$this->clearData(); |
58
|
|
|
$this->filtered = []; |
59
|
|
|
$this->errors = []; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
final public function errors($key, $defaultValue = false) |
63
|
|
|
{ |
64
|
|
|
return \WebServCo\Framework\ArrayStorage::get( |
65
|
|
|
$this->errors, |
66
|
|
|
$key, |
67
|
|
|
$defaultValue |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
final public function getSubmitField() |
72
|
|
|
{ |
73
|
|
|
if (!$this->isSent() || empty($this->submitFields)) { |
74
|
|
|
return false; |
75
|
|
|
} |
76
|
|
|
return $this->submitField; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
final public function help($key, $defaultValue = false) |
80
|
|
|
{ |
81
|
|
|
return $this->setting( |
82
|
|
|
sprintf('help/%s', $key), |
83
|
|
|
$defaultValue |
84
|
|
|
); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
final public function isSent() |
88
|
|
|
{ |
89
|
|
|
if (!empty($this->submitFields)) { |
90
|
|
|
foreach ($this->submitFields as $field) { |
91
|
|
|
if (false !== $this->request()->data($field)) { |
92
|
|
|
$this->submitField = $field; |
93
|
|
|
return true; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
return false; |
97
|
|
|
} |
98
|
|
|
return $this->request()->getMethod() === \WebServCo\Framework\Http\Method::POST; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
final public function isValid() |
102
|
|
|
{ |
103
|
|
|
return $this->valid; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
final public function meta($key, $defaultValue = false) |
107
|
|
|
{ |
108
|
|
|
return $this->setting( |
109
|
|
|
sprintf('meta/%s', $key), |
110
|
|
|
$defaultValue |
111
|
|
|
); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
final public function required($key, $defaultValue = false) |
115
|
|
|
{ |
116
|
|
|
return $this->setting( |
117
|
|
|
sprintf('required/%s', $key), |
118
|
|
|
$defaultValue |
119
|
|
|
); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
final public function toArray() |
123
|
|
|
{ |
124
|
|
|
return [ |
125
|
|
|
'meta' => $this->setting('meta', []), |
126
|
|
|
'help' => $this->setting('help', []), |
127
|
|
|
'required' => array_fill_keys($this->setting('required', []), true), |
128
|
|
|
'custom' => $this->setting('custom', []), |
129
|
|
|
'data' => $this->getData(), |
130
|
|
|
'errors' => $this->errors, |
131
|
|
|
]; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|