Last active
July 12, 2019 18:57
-
-
Save erandirjunior/51dd12821bb96c9e0584d12412f18b88 to your computer and use it in GitHub Desktop.
validação de array de dados utilizando o respect validation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require 'vendor/autoload.php'; | |
use Respect\Validation\Exceptions\ValidationException; | |
use Respect\Validation\Exceptions\NestedValidationException; | |
use Respect\Validation\Validator as v; | |
abstract class Validator | |
{ | |
protected $fail = true; | |
protected $rules; | |
protected $messages = []; | |
protected function make($content, $rules) | |
{ | |
foreach ($rules as $key => $value) { | |
try { | |
$validator = v::key($key, $value); | |
if (!$validator->validate($content[$key])) { | |
$this->fail = false; | |
} | |
$validator->assert($content); | |
} catch (ValidationException $exception) { | |
$this->messages[] = $exception->getMessages()[0]; | |
} | |
} | |
} | |
public function fail() | |
{ | |
return $this->fail; | |
} | |
public function messages() | |
{ | |
return $this->messages; | |
} | |
public function validate($content) | |
{ | |
$this->make($content, $this->rules); | |
} | |
} | |
class UserValidator extends Validator | |
{ | |
public function __construct() | |
{ | |
$this->rules = [ | |
'lastname' => v::notEmpty()->alpha("-'"), | |
'firstname' => v::optional(v::alpha("-'")), | |
'phone' => v::optional(v::phone()), | |
'email' => v::email(), | |
'message' => v::optional(v::length(5, 500)) | |
]; | |
} | |
} | |
$data = [ | |
'lastname' => 'abc-aa', | |
'firstname' => 'abc', | |
'phone' => 'abc', | |
'email' => 'abc', | |
'message' => 'abc' | |
]; | |
$user = new UserValidator(); | |
$user->validate($data); | |
var_dump($user->fail()); | |
var_dump($user->messages()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require 'vendor/autoload.php'; | |
use Respect\Validation\Exceptions\ValidationException; | |
use Respect\Validation\Exceptions\NestedValidationException; | |
use Respect\Validation\Validator as v; | |
$traducao = [ | |
'lastname' => 'Error for lastname', | |
'firstname' => 'Custom error for firstname', | |
'phone' => 'Custom error for phone', | |
'email' => 'Custom error for email', | |
'message' => 'Custom error for message' | |
]; | |
$messages = [ | |
'lastname', | |
'firstname', | |
'phone', | |
'email', | |
'message' | |
]; | |
$messages2 = [ | |
'lastname' => v::notEmpty()->alpha("-'"), | |
'firstname' => v::optional(v::alpha("-'")), | |
'phone' => v::optional(v::phone()), | |
'email' => v::email(), | |
'message' => v::optional(v::length(5, 500)) | |
]; | |
$data = [ | |
'lastname' => 'abc-aa', | |
'firstname' => 'abc', | |
'phone' => 'abc', | |
'email' => 'abc', | |
'message' => 'abc' | |
]; | |
$rule = v::key('lastname', v::notEmpty()->alpha("-'")) | |
->key('firstname', v::optional(v::alpha("-'"))) | |
->key('phone', v::optional(v::phone())) | |
->key('email', v::email()) | |
->key('message', v::optional(v::length(5, 500))); | |
try { | |
$rule->assert($data); | |
} catch (ValidationException $exception) { | |
var_dump($exception->getMessages()); | |
//var_dump($exception->findMessages($this->messages)); | |
} | |
echo "<hr>"; | |
foreach ($messages2 as $key => $value) { | |
$rule = v::key($key, $value); | |
var_dump($rule->validate($data[$key])); | |
try { | |
$rule->assert($data); | |
} catch (ValidationException $exception) { | |
var_dump([$exception->getMessages(), $exception->getFullMessage()]); | |
var_dump($exception->findMessages($traducao)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require 'vendor/autoload.php'; | |
use Respect\Validation\Exceptions\ValidationException; | |
use Respect\Validation\Exceptions\NestedValidationException; | |
use Respect\Validation\Validator as v; | |
$traducao = [ | |
'lastname' => 'Error for lastname', | |
'firstname' => 'Custom error for firstname', | |
'phone' => 'Custom error for phone', | |
'email' => 'Custom error for email', | |
'message' => 'Custom error for message' | |
]; | |
$messages = [ | |
'lastname', | |
'firstname', | |
'phone', | |
'email', | |
'message' | |
]; | |
$messages2 = [ | |
'lastname' => v::notEmpty()->alpha("-'"), | |
'firstname' => v::optional(v::alpha("-'")), | |
'phone' => v::optional(v::phone()), | |
'email' => v::email(), | |
'message' => v::optional(v::length(5, 500)) | |
]; | |
$data = [ | |
'lastname' => 'abc-aa', | |
'firstname' => 'abc', | |
'phone' => 'abc', | |
'email' => 'abc', | |
'message' => 'abc' | |
]; | |
$rule = v::key('lastname', v::notEmpty()->alpha("-'")) | |
->key('firstname', v::optional(v::alpha("-'"))) | |
->key('phone', v::optional(v::phone())) | |
->key('email', v::email()) | |
->key('message', v::optional(v::length(5, 500))); | |
try { | |
$rule->assert($data); | |
} catch (ValidationException $exception) { | |
var_dump($exception->getMessages()); | |
//var_dump($exception->findMessages($this->messages)); | |
} | |
echo "<hr>"; | |
foreach ($messages2 as $key => $value) { | |
$rule = v::key($key, $value); | |
var_dump($rule->validate($data[$key])); | |
try { | |
$rule->assert($data); | |
} catch (ValidationException $exception) { | |
var_dump($exception->getMessages()); | |
var_dump($exception->getFullMessage()); | |
var_dump($exception->findMessages($traducao)[$key]); | |
echo "<br><br>"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment