1 | <?php |
||
2 | |||
3 | namespace HnrAzevedo\Validator; |
||
4 | |||
5 | use HnrAzevedo\Validator\Rules; |
||
6 | use Psr\Http\Server\MiddlewareInterface; |
||
7 | |||
8 | Class Validator implements MiddlewareInterface |
||
9 | { |
||
10 | use Check, |
||
11 | ExtraCheck, |
||
12 | MiddlewareTrait, |
||
13 | Helper; |
||
14 | |||
15 | public function __construct(?string $lang = null) |
||
16 | { |
||
17 | $lang = (null !== $lang) ? $lang : 'en'; |
||
18 | require __DIR__.DIRECTORY_SEPARATOR.'languages'. DIRECTORY_SEPARATOR . $lang .'.php'; |
||
19 | self::$err = $VALIDATOR_LANG; |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
![]() |
|||
20 | } |
||
21 | |||
22 | public static function lang(string $lang): Validator |
||
23 | { |
||
24 | unset($VALIDATOR_LANG); |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
25 | require __DIR__.DIRECTORY_SEPARATOR.'languages'. DIRECTORY_SEPARATOR . $lang .'.php'; |
||
26 | self::$err = $VALIDATOR_LANG; |
||
27 | return self::getInstance($lang); |
||
28 | } |
||
29 | |||
30 | private string $namespace = ''; |
||
31 | private array $defaultData = [ |
||
32 | 'REQUEST_METHOD', |
||
33 | '_PROVIDER', |
||
34 | '_ROLE' |
||
35 | ]; |
||
36 | |||
37 | public static function add(object $model, \Closure $return): void |
||
38 | { |
||
39 | self::getInstance()->model(get_class($model)); |
||
40 | self::getInstance()->validator(self::getInstance()->model, $return(new Rules(get_class($model)))); |
||
41 | } |
||
42 | |||
43 | private static function getClass(string $class) |
||
44 | { |
||
45 | if(!class_exists($class)){ |
||
46 | throw new \RuntimeException(self::$err['nFormID']); |
||
47 | } |
||
48 | |||
49 | $class = get_class(new $class()); |
||
50 | |||
51 | return $class; |
||
52 | } |
||
53 | |||
54 | private function existRole($getRules) |
||
55 | { |
||
56 | if(empty(self::getInstance()->validator($getRules)->getRules(self::getInstance()->data['_ROLE']))){ |
||
57 | throw new \RuntimeException(self::$err['nFoundForm']); |
||
58 | } |
||
59 | } |
||
60 | |||
61 | public function checkDatas(array $data): void |
||
62 | { |
||
63 | if(!isset($data['_PROVIDER']) || !isset($data['_ROLE'])){ |
||
64 | throw new \RuntimeException(self::$err['issetData']); |
||
65 | } |
||
66 | } |
||
67 | |||
68 | public static function execute(array $data): bool |
||
69 | { |
||
70 | try{ |
||
71 | self::getInstance()->checkDatas($data); |
||
72 | |||
73 | self::getInstance()->data = $data; |
||
74 | |||
75 | $model = self::getInstance()->namespace.'\\'.ucfirst(self::getInstance()->data['_PROVIDER']); |
||
76 | |||
77 | self::getInstance()->model = self::getInstance()->getClass($model); |
||
78 | |||
79 | self::getInstance()->existRole(self::getInstance()->model); |
||
80 | |||
81 | foreach ( (self::getInstance()->validator(self::getInstance()->model)->getRules($data['_ROLE'])) as $key => $value) { |
||
82 | if(@$value['required'] === true){ |
||
83 | self::getInstance()->required[$key] = $value; |
||
84 | } |
||
85 | } |
||
86 | |||
87 | self::getInstance()->errors = []; |
||
88 | |||
89 | self::getInstance()->validate(); |
||
90 | self::getInstance()->requireds(); |
||
91 | }catch(\Exception $er){ |
||
92 | self::getInstance()->errors[] = ['form' => $er->getMessage()]; |
||
93 | } |
||
94 | |||
95 | return self::errors(); |
||
96 | } |
||
97 | |||
98 | public static function errors(): bool |
||
99 | { |
||
100 | return (count(self::getInstance()->errors) === 0); |
||
101 | } |
||
102 | |||
103 | public function validate(): void |
||
104 | { |
||
105 | foreach ( (self::getInstance()->validator(self::getInstance()->model)->getRules(self::getInstance()->data['_ROLE'])) as $key => $value) { |
||
106 | |||
107 | foreach (self::getInstance()->data as $keyy => $valuee) { |
||
108 | |||
109 | if($keyy!==$key){ |
||
110 | continue; |
||
111 | } |
||
112 | |||
113 | self::getInstance()->checkExpected($keyy); |
||
114 | |||
115 | unset(self::getInstance()->required[$key]); |
||
116 | |||
117 | foreach ($value as $subkey => $subvalue) { |
||
118 | |||
119 | $function = strtolower($subkey); |
||
120 | |||
121 | self::getInstance()->hasMethod($function)->$function($keyy, $subvalue, (self::getInstance()->validator(self::getInstance()->model)->getRules(self::getInstance()->data['_ROLE'])) ); |
||
122 | } |
||
123 | |||
124 | } |
||
125 | } |
||
126 | } |
||
127 | |||
128 | private function checkExpected(string $keyy): void |
||
129 | { |
||
130 | if(!array_key_exists($keyy, (self::getInstance()->validator(self::getInstance()->model)->getRules(self::getInstance()->data['_ROLE'])) ) && !in_array($keyy, self::getInstance()->defaultData)){ |
||
131 | if(!$this->checkExpectedArray($keyy)){ |
||
132 | throw new \RuntimeException($keyy . self::$err['nExpected']); |
||
133 | } |
||
134 | } |
||
135 | } |
||
136 | |||
137 | private function checkExpectedArray(string $keyy): bool |
||
138 | { |
||
139 | return (!array_key_exists($keyy.'[]', (self::getInstance()->validator(self::getInstance()->model)->getRules(self::getInstance()->data['_ROLE'])) ) && !in_array($keyy.'[]', self::getInstance()->defaultData)); |
||
140 | } |
||
141 | |||
142 | public static function getErrors(): array |
||
143 | { |
||
144 | return self::getInstance()->errors; |
||
145 | } |
||
146 | |||
147 | public function hasMethod($method): Validator |
||
148 | { |
||
149 | if(!method_exists(static::class, $method)){ |
||
150 | throw new \RuntimeException($method . self::$err['nMethod']); |
||
151 | } |
||
152 | |||
153 | return $this; |
||
154 | } |
||
155 | |||
156 | public static function toJson(array $request): string |
||
157 | { |
||
158 | $response = null; |
||
159 | |||
160 | self::getInstance()->checkDatas($request); |
||
161 | |||
162 | self::getInstance()->data['_PROVIDER'] = $request['_PROVIDER']; |
||
163 | self::getInstance()->data['_ROLE'] = $request['_ROLE']; |
||
164 | |||
165 | $model = self::getInstance()->namespace.'\\'.ucfirst($request['_PROVIDER']); |
||
166 | |||
167 | self::getInstance()->model(self::getClass($model)); |
||
168 | |||
169 | self::getInstance()->existRole(self::getInstance()->model()); |
||
170 | |||
171 | foreach ( self::getInstance()->validator(self::getInstance()->model())->getRules($request['_ROLE']) as $field => $r) { |
||
172 | $r = self::getInstance()->replaceRegex($r); |
||
173 | $response .= ("'$field':".json_encode(array_reverse($r))).','; |
||
174 | } |
||
175 | |||
176 | return '{'.substr($response, 0, -1).'}'; |
||
177 | } |
||
178 | |||
179 | private function replaceRegex(array $getRules): array |
||
180 | { |
||
181 | if(array_key_exists('regex', $getRules)){ |
||
182 | $getRules['regex'] = substr($getRules['regex'], 1, -2); |
||
183 | } |
||
184 | |||
185 | return $getRules; |
||
186 | } |
||
187 | |||
188 | public static function namespace(string $namespace): Validator |
||
189 | { |
||
190 | self::getInstance()->namespace = $namespace; |
||
191 | return self::getInstance(); |
||
192 | } |
||
193 | } |
||
194 |