1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Class AbstractFormHandler | AbstractFormHandler.php |
4
|
|
|
* @package Faulancer\Form |
5
|
|
|
*/ |
6
|
|
|
namespace Faulancer\Form; |
7
|
|
|
|
8
|
|
|
use Faulancer\Controller\Controller; |
9
|
|
|
use Faulancer\Form\Validator\AbstractValidator; |
10
|
|
|
use Faulancer\Service\Config; |
11
|
|
|
use Faulancer\ServiceLocator\ServiceLocator; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class AbstractFormHandler |
15
|
|
|
*/ |
16
|
|
|
abstract class AbstractFormHandler extends Controller |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Return the called form handler |
21
|
|
|
* |
22
|
|
|
* @return self |
23
|
|
|
*/ |
24
|
|
|
protected function getForm() :self |
25
|
|
|
{ |
26
|
|
|
return $this; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param string $field |
31
|
|
|
* @return string |
32
|
|
|
*/ |
33
|
|
|
public function getFormData(string $field) :string |
34
|
|
|
{ |
35
|
|
|
return $this->getRequest()->getPostData()[$field]; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Check data validity |
40
|
|
|
* |
41
|
|
|
* @return boolean |
42
|
|
|
*/ |
43
|
|
|
protected function isValid() :bool |
44
|
|
|
{ |
45
|
|
|
$result = $this->validate(); |
46
|
|
|
$errors = []; |
47
|
|
|
|
48
|
|
|
foreach ($result as $field => $data) { |
49
|
|
|
|
50
|
|
|
if ($data['valid']) { |
51
|
|
|
continue; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$errors[$field][] = [ |
55
|
|
|
'message' => $data['message'] |
56
|
|
|
]; |
57
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if (empty($errors)) { |
61
|
|
|
return true; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$this->getSessionManager()->setFlashbag('errors', $errors); |
65
|
|
|
|
66
|
|
|
return false; |
67
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Validate with the defined validators |
72
|
|
|
* |
73
|
|
|
* @return array |
74
|
|
|
*/ |
75
|
|
|
private function validate() :array |
76
|
|
|
{ |
77
|
|
|
$result = []; |
78
|
|
|
|
79
|
|
|
$this->getSessionManager()->setFlashbagFormData($this->getRequest()->getPostData()); |
80
|
|
|
|
81
|
|
|
foreach ($this->getRequest()->getPostData() as $key => $data) { |
82
|
|
|
|
83
|
|
|
if (strpos($key, '/') === false) { |
84
|
|
|
continue; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$parts = explode('/', $key); |
88
|
|
|
$validator = ucfirst($parts[0]); |
89
|
|
|
$value = $data; |
90
|
|
|
|
91
|
|
|
$validatorClass = '\Faulancer\Form\Validator\Type\\' . $validator; |
92
|
|
|
|
93
|
|
|
if (!class_exists($validatorClass)) { |
94
|
|
|
|
95
|
|
|
/** @var Config $config */ |
96
|
|
|
$config = ServiceLocator::instance()->get(Config::class); |
97
|
|
|
$nsPrefix = $config->get('namespacePrefix'); |
98
|
|
|
$validatorClass = str_replace('Faulancer', $nsPrefix, $validatorClass); |
99
|
|
|
|
100
|
|
|
if (!class_exists($validatorClass)) { |
101
|
|
|
continue; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** @var AbstractValidator $val */ |
107
|
|
|
$val = new $validatorClass(); |
108
|
|
|
$isValid = $val->process($value); |
109
|
|
|
|
110
|
|
|
$result[$key]['valid'] = $isValid; |
111
|
|
|
|
112
|
|
|
if (!$isValid) { |
113
|
|
|
$result[$key]['message'] = $val->getMessage(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return $result; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Init method which must be implemented |
123
|
|
|
* |
124
|
|
|
* @return mixed |
125
|
|
|
*/ |
126
|
|
|
abstract public function run(); |
127
|
|
|
|
128
|
|
|
} |