-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add "Validate" directory, deprecate "Validators" directory
- Loading branch information
Showing
3 changed files
with
59 additions
and
63 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,68 +2,10 @@ | |
|
||
namespace Rougin\Weasley; | ||
|
||
use Valitron\Validator; | ||
|
||
/** | ||
* @package Weasley | ||
* @author Rougin Gutib <[email protected]> | ||
*/ | ||
abstract class Check | ||
class Check | ||
{ | ||
/** | ||
* @var array<string, string[]> | ||
*/ | ||
public $errors = array(); | ||
|
||
/** | ||
* @var \Valitron\Validator | ||
*/ | ||
protected $validator; | ||
|
||
public function __construct() | ||
{ | ||
$this->validator = new Validator; | ||
} | ||
|
||
/** | ||
* Sets the labels in the validator. | ||
* | ||
* @return array<string, string> | ||
*/ | ||
abstract protected function labels(); | ||
|
||
/** | ||
* Sets the rules in the validator. | ||
* | ||
* @param array<string, mixed> $data | ||
* @return void | ||
*/ | ||
abstract protected function rules(array $data = array()); | ||
|
||
/** | ||
* Validates the given data against the specified rules. | ||
* | ||
* @param array<string, mixed> $data | ||
* @return boolean | ||
*/ | ||
public function validate(array $data) | ||
{ | ||
$this->validator->labels($this->labels()); | ||
|
||
$this->rules($data); | ||
|
||
$validator = $this->validator->withData($data); | ||
|
||
if ($validator->validate()) | ||
{ | ||
return true; | ||
} | ||
|
||
/** @var array<string, string[]> */ | ||
$errors = $validator->errors(); | ||
|
||
$this->errors = $errors; | ||
|
||
return false; | ||
} | ||
} |
Empty file.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,16 +2,70 @@ | |
|
||
namespace Rougin\Weasley\Validators; | ||
|
||
use Rougin\Weasley\Check; | ||
use Valitron\Validator; | ||
|
||
/** | ||
* @deprecated Use "Check" instead. | ||
* | ||
* Abstract Validator | ||
* | ||
* @package Weasley | ||
* @author Rougin Gutib <[email protected]> | ||
*/ | ||
abstract class AbstractValidator extends Check | ||
abstract class AbstractValidator | ||
{ | ||
/** | ||
* @var array<string, string[]> | ||
*/ | ||
public $errors = array(); | ||
|
||
/** | ||
* @var \Valitron\Validator | ||
*/ | ||
protected $validator; | ||
|
||
public function __construct() | ||
{ | ||
$this->validator = new Validator; | ||
} | ||
|
||
/** | ||
* Sets the labels in the validator. | ||
* | ||
* @return array<string, string> | ||
*/ | ||
abstract protected function labels(); | ||
|
||
/** | ||
* Sets the rules in the validator. | ||
* | ||
* @param array<string, mixed> $data | ||
* @return void | ||
*/ | ||
abstract protected function rules(array $data = array()); | ||
|
||
/** | ||
* Validates the given data against the specified rules. | ||
* | ||
* @param array<string, mixed> $data | ||
* @return boolean | ||
*/ | ||
public function validate(array $data) | ||
{ | ||
$this->validator->labels($this->labels()); | ||
|
||
$this->rules($data); | ||
|
||
$validator = $this->validator->withData($data); | ||
|
||
if ($validator->validate()) | ||
{ | ||
return true; | ||
} | ||
|
||
/** @var array<string, string[]> */ | ||
$errors = $validator->errors(); | ||
|
||
$this->errors = $errors; | ||
|
||
return false; | ||
} | ||
} |