Skip to content

Commit

Permalink
Add "Validate" directory, deprecate "Validators" directory (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
rougin committed Mar 28, 2024
1 parent 1891601 commit 9b17ed8
Show file tree
Hide file tree
Showing 14 changed files with 567 additions and 83 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Expand Up @@ -13,11 +13,13 @@ All notable changes to `Weasley` will be documented in this file.
- `Controllers` directory to `Routes` directory
- `Middleware` directory to `Handlers` directory
- `Illuminate` directory to `Packages` directory
- `Validators` directory to `Validate` directory

### Deprecated
- `Controllers` directory (use `Routes` directory instead)
- `Middleware` directory (use `Handlers` directory instead)
- `Illuminate` directory (use `Packages` directory instead)
- `Validators` directory (use `Validate` directory instead)

### Fixed
- Unit tests in running `SessionIntegration`
Expand Down Expand Up @@ -173,4 +175,4 @@ All notable changes to `Weasley` will be documented in this file.
## 0.1.0 - 2017-05-02

### Added
- `Weasley` library
- `Weasley` library
48 changes: 48 additions & 0 deletions README.md
Expand Up @@ -77,6 +77,54 @@ Mutators are classes that mutates (transforms) specified result (e.g., [PSR-07](

**NOTE**: The `Laravel/Paginate` package must be included to use the parsing capabilities of `RestMutator`.

### Validation

Weasley also provides a validation class on top of [Valitron](https://github.com/vlucas/valitron). Kindly create a class that extends to the `Check` class:

``` php
use Rougin\Weasley\Check;

class UserCheck extends Check
{
protected $labels =
[
'name' => 'Name',
'email' => 'Email',
'age' => 'Age',
];

protected $rules =
[
'name' => 'required',
'setting' => 'required|email',
'type' => 'required|numeric',
];
}
```

Once created, the data can be submitted to the said class for validation:

``` php
$check = new UserCheck;

$data = /* e.g., data from request */;

if ($check->valid($data))
{
// $data passed from validation
}
else
{
// Get the available errors ---
$errors = $check->errors();
// ----------------------------

// Or get the first error only ---
echo $check->firstError();
// -------------------------------
}
```

## Changelog

Please see [CHANGELOG][link-changelog] for more information what has changed recently.
Expand Down
60 changes: 1 addition & 59 deletions src/Check.php
Expand Up @@ -2,68 +2,10 @@

namespace Rougin\Weasley;

use Valitron\Validator;

/**
* @package Weasley
* @author Rougin Gutib <rougingutib@gmail.com>
*/
abstract class Check
class Check extends Validate\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;
}
}
5 changes: 5 additions & 0 deletions src/Commands/MakeValidatorCommand.php
Expand Up @@ -19,6 +19,11 @@ class MakeValidatorCommand extends CreateCheck
*/
protected $command = 'make:validator';

/**
* @var string
*/
protected $filename = 'Validate.stub';

/**
* @var boolean
*/
Expand Down
23 changes: 6 additions & 17 deletions src/Scripts/Templates/Check.stub
Expand Up @@ -13,27 +13,16 @@ use Rougin\Weasley\Check;
class $CLASS extends Check
{
/**
* Sets the labels in the validator.
*
* @return array<string, string>
* @var array<string, string>
*/
protected function labels()
{
$labels = array();

protected $labels = array(
//

return $labels;
}
);

/**
* Sets the rules in the validator.
*
* @param array<string, mixed> $data
* @return void
* @var array<string, string>
*/
protected function rules(array $data = array())
{
protected $rules = array(
//
}
);
}
39 changes: 39 additions & 0 deletions src/Scripts/Templates/Validate.stub
@@ -0,0 +1,39 @@
<?php

namespace $NAMESPACE;

use Rougin\Weasley\Validators\AbstractValidator;

/**
* $CLASS
*
* @package $PACKAGE
* @author $AUTHOR
*/
class $CLASS extends AbstractValidator
{
/**
* Sets the labels in the validator.
*
* @return array<string, string>
*/
protected function labels()
{
$labels = array();

//

return $labels;
}

/**
* Sets the rules in the validator.
*
* @param array<string, mixed> $data
* @return void
*/
protected function rules(array $data = array())
{
//
}
}
156 changes: 156 additions & 0 deletions src/Validate/Check.php
@@ -0,0 +1,156 @@
<?php

namespace Rougin\Weasley\Validate;

use Valitron\Validator;

/**
* Check
*
* @package Weasley
* @author Rougin Gutib <rougingutib@gmail.com>
*/
class Check
{
/**
* @var array<string, string[]>
*/
protected $errors = array();

/**
* @var array<string, mixed>
*/
protected $data = array();

/**
* @var array<string, string>
*/
protected $labels = array();

/**
* @var array<string, string>
*/
protected $rules = array();

/**
* Returns the generated errors after validation.
*
* @return array<string, string[]>
*/
public function errors()
{
return $this->errors;
}

/**
* Returns the first error after validation.
*
* @return string|null
*/
public function firstError()
{
if (! $this->errors)
{
return null;
}

/** @var string[][] */
$values = array_values($this->errors);

return (string) $values[0][0];
}

/**
* Returns the specified labels.
*
* @return array<string, string>
*/
public function labels()
{
return $this->labels;
}

/**
* Returns the specified rules based from the payload.
*
* @param array<string, mixed> $data
* @return array<string, string>
*/
public function rules($data)
{
return $this->rules;
}

/**
* Adds a new error message to the specified key.
*
* @param string $key
* @param string $text
* @return self
*/
public function setError($key, $text)
{
if (! isset($this->errors[$key]))
{
$this->errors[$key] = array();
}

array_push($this->errors[$key], $text);

return $this;
}

/**
* Checks if the payload is valid againsts the specified rules.
*
* @param array<string, mixed>|null $data
* @return boolean
*/
public function valid(array $data = null)
{
$valid = new Validator;

$valid->labels($this->labels());

if (! $data)
{
$data = $this->data;
}

$rules = $this->rules($data);

foreach ($rules as $key => $rule)
{
// Break down multiple rules ---
$items = explode('|', $rule);

// TODO: Allow custom rules from existing, new ones ---
foreach ($items as $item)
{
$valid->rule($item, $key);
}
// ----------------------------------------------------
// -----------------------------
}

$valid = $valid->withData($data);

if ($valid->validate())
{
return count($this->errors) === 0;
}

/** @var array<string, string[]> */
$result = $valid->errors();

foreach ($result as $name => $errors)
{
foreach ($errors as $error)
{
$this->setError($name, $error);
}
}

return count($this->errors) === 0;
}
}

0 comments on commit 9b17ed8

Please sign in to comment.