@@ -9,30 +9,30 @@ |
||
9 | 9 | |
10 | 10 | public function __construct(object $model) |
11 | 11 | { |
12 | - $this->form['model'] = ucfirst(get_class($model)); |
|
13 | - } |
|
12 | + $this->form['model'] = ucfirst(get_class($model)); |
|
13 | + } |
|
14 | 14 | |
15 | 15 | public function setAction(string $action): Rules |
16 | 16 | { |
17 | - $this->action = $action; |
|
18 | - return $this; |
|
19 | - } |
|
17 | + $this->action = $action; |
|
18 | + return $this; |
|
19 | + } |
|
20 | 20 | |
21 | 21 | public function addField(string $field, array $test): Rules |
22 | 22 | { |
23 | - if(empty($this->action)){ |
|
23 | + if(empty($this->action)){ |
|
24 | 24 | self::$errors[] = "Form action not registered."; |
25 | 25 | } |
26 | 26 | |
27 | - if(empty($this->form[$this->action][$field])){ |
|
27 | + if(empty($this->form[$this->action][$field])){ |
|
28 | 28 | $this->form[$this->action][$field] = $test; |
29 | 29 | } |
30 | 30 | |
31 | - return $this; |
|
32 | - } |
|
31 | + return $this; |
|
32 | + } |
|
33 | 33 | |
34 | 34 | public function getRules(string $action): ?array |
35 | 35 | { |
36 | - return (array_key_exists($action, $this->form)) ? $this->form[$action] : null; |
|
37 | - } |
|
36 | + return (array_key_exists($action, $this->form)) ? $this->form[$action] : null; |
|
37 | + } |
|
38 | 38 | } |
@@ -22,11 +22,11 @@ |
||
22 | 22 | |
23 | 23 | public function addField(string $field, array $test): Rules |
24 | 24 | { |
25 | - if(empty($this->action)){ |
|
25 | + if (empty($this->action)) { |
|
26 | 26 | self::$errors[] = "Form action not registered."; |
27 | 27 | } |
28 | 28 | |
29 | - if(empty($this->form[$this->action][$field])){ |
|
29 | + if (empty($this->form[$this->action][$field])) { |
|
30 | 30 | $this->form[$this->action][$field] = $test; |
31 | 31 | } |
32 | 32 |
@@ -5,20 +5,20 @@ |
||
5 | 5 | use HnrAzevedo\Validator\Validator; |
6 | 6 | use HnrAzevedo\Validator\Rules; |
7 | 7 | |
8 | -Class User{ |
|
8 | +Class User { |
|
9 | 9 | |
10 | 10 | public function __construct() |
11 | 11 | { |
12 | 12 | |
13 | - Validator::add($this, function(Rules $rules){ |
|
13 | + Validator::add($this, function(Rules $rules) { |
|
14 | 14 | $rules->setAction('login') |
15 | 15 | //->addField('email',['minlength'=>1,'regex'=>'/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/','required'=>true]) |
16 | - ->addField('email',['minlength'=>1,'filter'=>FILTER_VALIDATE_EMAIL,'required'=>true]) |
|
17 | - ->addField('password',['minlength'=>6,'maxlength'=>20,'required'=>true]) |
|
18 | - ->addField('password2',['equals'=>'password','required'=>true]) |
|
19 | - ->addField('remember',['minlength'=>2,'maxlength'=>2,'required'=>false]) |
|
20 | - ->addField('birth',['type'=>'date','required'=>true]) |
|
21 | - ->addField('phones',['mincount'=>2,'maxcount'=>3,'required'=>true,'minlength'=>8,'maxlength'=>9]); |
|
16 | + ->addField('email', ['minlength'=>1, 'filter'=>FILTER_VALIDATE_EMAIL, 'required'=>true]) |
|
17 | + ->addField('password', ['minlength'=>6, 'maxlength'=>20, 'required'=>true]) |
|
18 | + ->addField('password2', ['equals'=>'password', 'required'=>true]) |
|
19 | + ->addField('remember', ['minlength'=>2, 'maxlength'=>2, 'required'=>false]) |
|
20 | + ->addField('birth', ['type'=>'date', 'required'=>true]) |
|
21 | + ->addField('phones', ['mincount'=>2, 'maxcount'=>3, 'required'=>true, 'minlength'=>8, 'maxlength'=>9]); |
|
22 | 22 | |
23 | 23 | return $rules; |
24 | 24 | }); |
@@ -11,15 +11,15 @@ |
||
11 | 11 | { |
12 | 12 | Validator::add($this, function(Rules $rules){ |
13 | 13 | $rules->setAction('login') |
14 | - //->addField('email',['minlength'=>1,'regex'=>'/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/','required'=>true]) |
|
15 | - ->addField('email',['minlength'=>1,'filter'=>FILTER_VALIDATE_EMAIL,'required'=>true]) |
|
16 | - ->addField('password',['minlength'=>6,'maxlength'=>20,'required'=>true]) |
|
17 | - ->addField('password2',['equals'=>'password','required'=>true]) |
|
18 | - ->addField('remember',['minlength'=>2,'maxlength'=>2,'required'=>false]) |
|
19 | - ->addField('birth',['type'=>'date','required'=>true]) |
|
20 | - ->addField('phones',['mincount'=>2,'maxcount'=>3,'required'=>true,'minlength'=>8,'maxlength'=>9]); |
|
14 | + //->addField('email',['minlength'=>1,'regex'=>'/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/','required'=>true]) |
|
15 | + ->addField('email',['minlength'=>1,'filter'=>FILTER_VALIDATE_EMAIL,'required'=>true]) |
|
16 | + ->addField('password',['minlength'=>6,'maxlength'=>20,'required'=>true]) |
|
17 | + ->addField('password2',['equals'=>'password','required'=>true]) |
|
18 | + ->addField('remember',['minlength'=>2,'maxlength'=>2,'required'=>false]) |
|
19 | + ->addField('birth',['type'=>'date','required'=>true]) |
|
20 | + ->addField('phones',['mincount'=>2,'maxcount'=>3,'required'=>true,'minlength'=>8,'maxlength'=>9]); |
|
21 | 21 | |
22 | - return $rules; |
|
22 | + return $rules; |
|
23 | 23 | }); |
24 | 24 | |
25 | 25 | return $this; |
@@ -77,7 +77,7 @@ |
||
77 | 77 | |
78 | 78 | runMiddlewares($serverRequest); |
79 | 79 | |
80 | -}catch(Exception $er){ |
|
80 | +} catch(Exception $er){ |
|
81 | 81 | |
82 | 82 | die("Code Error: {$er->getCode()}<br>Line: {$er->getLine()}<br>File: {$er->getFile()}<br>Message: {$er->getMessage()}."); |
83 | 83 |
@@ -10,20 +10,20 @@ discard block |
||
10 | 10 | use Psr\Http\Message\ResponseInterface; |
11 | 11 | use Psr\Http\Server\MiddlewareInterface; |
12 | 12 | |
13 | -try{ |
|
13 | +try { |
|
14 | 14 | $serverRequest = (new Factory())->createServerRequest('GET', new Uri('/')); |
15 | - $serverRequest = $serverRequest->withAttribute('validator',[ |
|
15 | + $serverRequest = $serverRequest->withAttribute('validator', [ |
|
16 | 16 | 'namespace' => 'HnrAzevedo\\Validator\\Example\\Rules', |
17 | 17 | 'data' => $data |
18 | 18 | ]); |
19 | 19 | |
20 | - class App implements MiddlewareInterface{ |
|
20 | + class App implements MiddlewareInterface { |
|
21 | 21 | public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
22 | 22 | { |
23 | - if(!$request->getAttribute('validator')['valid']){ |
|
23 | + if (!$request->getAttribute('validator')['valid']) { |
|
24 | 24 | $err = ''; |
25 | - foreach($request->getAttribute('validator')['errors'] as $er => $error){ |
|
26 | - $err .= (is_array($error)) ? implode('',array_keys($error)) . ' ' . implode('', array_values($error)) : $error; |
|
25 | + foreach ($request->getAttribute('validator')['errors'] as $er => $error) { |
|
26 | + $err .= (is_array($error)) ? implode('', array_keys($error)).' '.implode('', array_values($error)) : $error; |
|
27 | 27 | $err .= ', '; |
28 | 28 | } |
29 | 29 | throw new \Exception(substr($err, 0, -2)); |
@@ -33,7 +33,7 @@ discard block |
||
33 | 33 | } |
34 | 34 | } |
35 | 35 | |
36 | - define('GLOBAL_MIDDLEWARES',[ |
|
36 | + define('GLOBAL_MIDDLEWARES', [ |
|
37 | 37 | Validator::class, |
38 | 38 | App::class |
39 | 39 | ]); |
@@ -69,7 +69,7 @@ discard block |
||
69 | 69 | |
70 | 70 | function runMiddlewares($serverRequest) |
71 | 71 | { |
72 | - nextExample(new class implements RequestHandlerInterface{ |
|
72 | + nextExample(new class implements RequestHandlerInterface { |
|
73 | 73 | public function handle(ServerRequestInterface $request): ResponseInterface |
74 | 74 | { |
75 | 75 | return (new Factory())->createResponse(200); |
@@ -79,7 +79,7 @@ discard block |
||
79 | 79 | |
80 | 80 | runMiddlewares($serverRequest); |
81 | 81 | |
82 | -}catch(Exception $er){ |
|
82 | +}catch (Exception $er) { |
|
83 | 83 | |
84 | 84 | die("Code Error: {$er->getCode()}<br>Line: {$er->getLine()}<br>File: {$er->getFile()}<br>Message: {$er->getMessage()}."); |
85 | 85 |
@@ -6,12 +6,12 @@ |
||
6 | 6 | use Psr\Http\Server\RequestHandlerInterface; |
7 | 7 | use Psr\Http\Message\ResponseInterface; |
8 | 8 | |
9 | -trait MiddlewareTrait{ |
|
9 | +trait MiddlewareTrait { |
|
10 | 10 | |
11 | 11 | public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
12 | 12 | { |
13 | - $data = ($request->getAttribute('validator') !== null && isset($request->getAttribute('validator')['data'])) ? $request->getAttribute('validator')['data'] : $_REQUEST; |
|
14 | - $namespace = ($request->getAttribute('validator') !== null && isset($request->getAttribute('validator')['namespace'])) ? $request->getAttribute('validator')['namespace'] : ''; |
|
13 | + $data = ($request->getAttribute('validator')!==null && isset($request->getAttribute('validator')['data'])) ? $request->getAttribute('validator')['data'] : $_REQUEST; |
|
14 | + $namespace = ($request->getAttribute('validator')!==null && isset($request->getAttribute('validator')['namespace'])) ? $request->getAttribute('validator')['namespace'] : ''; |
|
15 | 15 | return $handler->handle($request->withAttribute('validator', [ |
16 | 16 | 'valid' => self::namespace($namespace)->execute($data), |
17 | 17 | 'errors' => self::getErrors() |
@@ -4,17 +4,17 @@ discard block |
||
4 | 4 | |
5 | 5 | use HnrAzevedo\Validator\Validator; |
6 | 6 | |
7 | -try{ |
|
7 | +try { |
|
8 | 8 | |
9 | 9 | /* Checks whether the passed data is valid for the selected function */ |
10 | 10 | $valid = Validator::namespace('HnrAzevedo\\Validator\\Example\\Rules')->execute($data); |
11 | 11 | |
12 | 12 | $errors = []; |
13 | 13 | |
14 | - if(!$valid){ |
|
15 | - foreach(Validator::getErrors() as $err => $message){ |
|
14 | + if (!$valid) { |
|
15 | + foreach (Validator::getErrors() as $err => $message) { |
|
16 | 16 | $errors[] = [ |
17 | - 'input' => array_keys($message)[0], // Return name input error |
|
17 | + 'input' => array_keys($message)[0], // Return name input error |
|
18 | 18 | 'message' => $message[array_keys($message)[0]] // Return message error |
19 | 19 | ]; |
20 | 20 | } |
@@ -23,7 +23,7 @@ discard block |
||
23 | 23 | /* Transforms validation to Json format to be validated on the client if desired */ |
24 | 24 | $json = Validator::namespace('HnrAzevedo\\Validator\\Example\\Rules')->toJson($data); |
25 | 25 | |
26 | -}catch(Exception $er){ |
|
26 | +}catch (Exception $er) { |
|
27 | 27 | |
28 | 28 | die("Code Error: {$er->getCode()}<br> Line: {$er->getLine()}<br> File: {$er->getFile()}<br> Message: {$er->getMessage()}"); |
29 | 29 |
@@ -23,7 +23,7 @@ |
||
23 | 23 | /* Transforms validation to Json format to be validated on the client if desired */ |
24 | 24 | $json = Validator::namespace('HnrAzevedo\\Validator\\Example\\Rules')->toJson($data); |
25 | 25 | |
26 | -}catch(Exception $er){ |
|
26 | +} catch(Exception $er){ |
|
27 | 27 | |
28 | 28 | die("Code Error: {$er->getCode()}<br> Line: {$er->getLine()}<br> File: {$er->getFile()}<br> Message: {$er->getMessage()}"); |
29 | 29 |
@@ -8,7 +8,7 @@ |
||
8 | 8 | 'password' => 123456, |
9 | 9 | 'password2' => 123456, |
10 | 10 | 'phones' => [ |
11 | - '949164770','949164771','949164772' |
|
11 | + '949164770', '949164771', '949164772' |
|
12 | 12 | ], |
13 | 13 | 'birth' => '28/09/1996', |
14 | 14 | 'PROVIDER' => 'user', |
@@ -24,10 +24,10 @@ discard block |
||
24 | 24 | |
25 | 25 | protected function data($field = null, ?array $values = null) |
26 | 26 | { |
27 | - if(null !== $values){ |
|
27 | + if (null!==$values) { |
|
28 | 28 | $this->data[$field] = $values; |
29 | 29 | } |
30 | - if(null !== $field){ |
|
30 | + if (null!==$field) { |
|
31 | 31 | return $this->data[$field]; |
32 | 32 | } |
33 | 33 | return $this->data; |
@@ -35,7 +35,7 @@ discard block |
||
35 | 35 | |
36 | 36 | protected function validator(string $model, ?object $callback = null) |
37 | 37 | { |
38 | - if(null !== $callback){ |
|
38 | + if (null!==$callback) { |
|
39 | 39 | $this->validators[$model] = $callback; |
40 | 40 | } |
41 | 41 | return $this->validators[$model]; |
@@ -43,7 +43,7 @@ discard block |
||
43 | 43 | |
44 | 44 | protected function model(?string $model = null): string |
45 | 45 | { |
46 | - if(null !== $model){ |
|
46 | + if (null!==$model) { |
|
47 | 47 | $this->model = $model; |
48 | 48 | } |
49 | 49 | return $this->model; |
@@ -8,9 +8,9 @@ discard block |
||
8 | 8 | |
9 | 9 | protected function checkRequireds(): void |
10 | 10 | { |
11 | - if(count(self::getInstance()->required()) > 0){ |
|
11 | + if (count(self::getInstance()->required())>0) { |
|
12 | 12 | self::getInstance()->error([ |
13 | - 'As seguintes informações não poderam ser validadas: '.implode(', ',array_keys(self::getInstance()->required())) |
|
13 | + 'As seguintes informações não poderam ser validadas: '.implode(', ', array_keys(self::getInstance()->required())) |
|
14 | 14 | ]); |
15 | 15 | } |
16 | 16 | } |
@@ -18,7 +18,7 @@ discard block |
||
18 | 18 | public function validateDate($date, $format = 'Y-m-d H:i:s') |
19 | 19 | { |
20 | 20 | $d = \DateTime::createFromFormat($format, $date); |
21 | - return $d && $d->format($format) == $date; |
|
21 | + return $d && $d->format($format)==$date; |
|
22 | 22 | } |
23 | 23 | |
24 | 24 | protected function checkRequired(string $param): bool |
@@ -31,12 +31,12 @@ discard block |
||
31 | 31 | |
32 | 32 | protected function toNext(string $param, $value): bool |
33 | 33 | { |
34 | - return (self::getInstance()->checkRequired($param) || strlen($value > 0)); |
|
34 | + return (self::getInstance()->checkRequired($param) || strlen($value>0)); |
|
35 | 35 | } |
36 | 36 | |
37 | 37 | protected function testArray(string $param, $value): ?array |
38 | 38 | { |
39 | - if(!is_array($value)){ |
|
39 | + if (!is_array($value)) { |
|
40 | 40 | self::getInstance()->error([ |
41 | 41 | $param => 'Era esperado uma informação em formato array para está informação' |
42 | 42 | ]); |
@@ -9,18 +9,18 @@ discard block |
||
9 | 9 | |
10 | 10 | protected function checkMinlength(string $param, $value): void |
11 | 11 | { |
12 | - if(self::getInstance()->toNext($param, self::getInstance()->data($param))){ |
|
12 | + if (self::getInstance()->toNext($param, self::getInstance()->data($param))) { |
|
13 | 13 | |
14 | 14 | $realval = (is_array(self::getInstance()->data($param))) ? self::getInstance()->data($param) : [self::getInstance()->data($param)]; |
15 | 15 | |
16 | - foreach($realval as $val){ |
|
17 | - if(strlen($val) === 0) { |
|
16 | + foreach ($realval as $val) { |
|
17 | + if (strlen($val)===0) { |
|
18 | 18 | self::getInstance()->error([ |
19 | 19 | $param => ' é obrigatório' |
20 | 20 | ]); |
21 | 21 | continue; |
22 | 22 | } |
23 | - if($value > strlen($val)) { |
|
23 | + if ($value>strlen($val)) { |
|
24 | 24 | self::getInstance()->error([ |
25 | 25 | $param => 'não atingiu o mínimo de caracteres esperado' |
26 | 26 | ]); |
@@ -31,12 +31,12 @@ discard block |
||
31 | 31 | |
32 | 32 | protected function checkRegex(string $param, $value): void |
33 | 33 | { |
34 | - if(self::getInstance()->toNext($param, self::getInstance()->data($param))){ |
|
34 | + if (self::getInstance()->toNext($param, self::getInstance()->data($param))) { |
|
35 | 35 | |
36 | 36 | $realval = (is_array(self::getInstance()->data($param))) ? self::getInstance()->data($param) : [self::getInstance()->data($param)]; |
37 | 37 | |
38 | - foreach($realval as $val){ |
|
39 | - if(!preg_match(self::getInstance()->validator(self::getInstance()->model())->getRules(self::getInstance()->data('ROLE'))[$param]['regex'], $val)){ |
|
38 | + foreach ($realval as $val) { |
|
39 | + if (!preg_match(self::getInstance()->validator(self::getInstance()->model())->getRules(self::getInstance()->data('ROLE'))[$param]['regex'], $val)) { |
|
40 | 40 | self::getInstance()->error([ |
41 | 41 | $param => 'inválido(a)' |
42 | 42 | ]); |
@@ -47,9 +47,9 @@ discard block |
||
47 | 47 | |
48 | 48 | protected function checkMincount(string $param, $value): void |
49 | 49 | { |
50 | - if(self::getInstance()->toNext($param, self::getInstance()->data($param))){ |
|
50 | + if (self::getInstance()->toNext($param, self::getInstance()->data($param))) { |
|
51 | 51 | $array = self::getInstance()->testArray($param, self::getInstance()->data($param)); |
52 | - if(count($array) < $value){ |
|
52 | + if (count($array)<$value) { |
|
53 | 53 | self::getInstance()->error([ |
54 | 54 | $param => 'não atingiu o mínimo esperado' |
55 | 55 | ]); |
@@ -59,9 +59,9 @@ discard block |
||
59 | 59 | |
60 | 60 | protected function checkMaxcount(string $param, $value): void |
61 | 61 | { |
62 | - if(self::getInstance()->toNext($param, self::getInstance()->data($param))){ |
|
62 | + if (self::getInstance()->toNext($param, self::getInstance()->data($param))) { |
|
63 | 63 | $array = self::getInstance()->testArray($param, self::getInstance()->data($param)); |
64 | - if(count($array) > $value){ |
|
64 | + if (count($array)>$value) { |
|
65 | 65 | self::getInstance()->error([ |
66 | 66 | $param => 'ultrapassou o esperado' |
67 | 67 | ]); |
@@ -71,13 +71,13 @@ discard block |
||
71 | 71 | |
72 | 72 | protected function checkEquals(string $param, $value): void |
73 | 73 | { |
74 | - if(!array_key_exists($param, self::getInstance()->data())){ |
|
74 | + if (!array_key_exists($param, self::getInstance()->data())) { |
|
75 | 75 | self::getInstance()->error([ |
76 | 76 | $param => "O servidor não encontrou a informação '{$value}' para ser comparada" |
77 | 77 | ]); |
78 | 78 | } |
79 | 79 | |
80 | - if(self::getInstance()->data($param) != self::getInstance()->data($value)){ |
|
80 | + if (self::getInstance()->data($param)!=self::getInstance()->data($value)) { |
|
81 | 81 | self::getInstance()->error([ |
82 | 82 | $param => 'está diferente de '.ucfirst($value) |
83 | 83 | ]); |
@@ -86,13 +86,13 @@ discard block |
||
86 | 86 | |
87 | 87 | protected function checkMaxlength(string $param, $value) |
88 | 88 | { |
89 | - if(self::getInstance()->toNext($param, self::getInstance()->data($param))){ |
|
89 | + if (self::getInstance()->toNext($param, self::getInstance()->data($param))) { |
|
90 | 90 | |
91 | 91 | $realval = (is_array(self::getInstance()->data($param))) ? self::getInstance()->data($param) : [self::getInstance()->data($param)]; |
92 | 92 | |
93 | - foreach($realval as $val){ |
|
93 | + foreach ($realval as $val) { |
|
94 | 94 | |
95 | - if($value < strlen($val)) { |
|
95 | + if ($value<strlen($val)) { |
|
96 | 96 | self::getInstance()->error([ |
97 | 97 | $param => 'ultrapassou o máximo de caracteres esperado' |
98 | 98 | ]); |
@@ -104,11 +104,11 @@ discard block |
||
104 | 104 | |
105 | 105 | protected function checkType(string $param, $value) |
106 | 106 | { |
107 | - if(self::getInstance()->toNext($param, self::getInstance()->data($param))){ |
|
107 | + if (self::getInstance()->toNext($param, self::getInstance()->data($param))) { |
|
108 | 108 | |
109 | 109 | switch ($value) { |
110 | 110 | case 'date': |
111 | - if(!self::getInstance()->validateDate(self::getInstance()->data($param) , 'd/m/Y')){ |
|
111 | + if (!self::getInstance()->validateDate(self::getInstance()->data($param), 'd/m/Y')) { |
|
112 | 112 | self::getInstance()->error([ |
113 | 113 | $param => 'não é uma data válida' |
114 | 114 | ]); |
@@ -120,9 +120,9 @@ discard block |
||
120 | 120 | |
121 | 121 | protected function checkFilter(string $param, $value) |
122 | 122 | { |
123 | - if(self::getInstance()->toNext($param, self::getInstance()->data($param))){ |
|
123 | + if (self::getInstance()->toNext($param, self::getInstance()->data($param))) { |
|
124 | 124 | |
125 | - if(!filter_var(self::getInstance()->data($param), $value)){ |
|
125 | + if (!filter_var(self::getInstance()->data($param), $value)) { |
|
126 | 126 | self::getInstance()->error([ |
127 | 127 | $param => 'não passou pela filtragem de dados' |
128 | 128 | ]); |