Skip to content

Commit

Permalink
Add "Validate" directory, deprecate "Validators" directory
Browse files Browse the repository at this point in the history
  • Loading branch information
rougin committed Mar 21, 2024
1 parent 322715b commit 4744efa
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 63 deletions.
60 changes: 1 addition & 59 deletions src/Check.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 added src/Validate/Check.php
Empty file.
62 changes: 58 additions & 4 deletions src/Validators/AbstractValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit 4744efa

Please sign in to comment.