Skip to content

Commit

Permalink
Added global custom rules, improved type-hinting, version bump
Browse files Browse the repository at this point in the history
  • Loading branch information
biohzrdmx committed Oct 12, 2023
1 parent 9eb8d08 commit abbee87
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 43 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "vecode/caldera-validation",
"description": "Validation layer, part of Vecode Caldera",
"version": "1.0",
"version": "1.1",
"type": "library",
"license": "MIT",
"authors": [
Expand Down
41 changes: 33 additions & 8 deletions src/Validation/Condition.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,31 @@ class Condition {

use ValidatesData;

/**
* Validation instance
*/
protected Validation $validation;

/**
* Rules array
* @var array
*/
protected $rules = [];
protected array $rules = [];

/**
* Error bag
* @var array
*/
protected $errors = [];
protected array $errors = [];

/**
* Constructor
* @param Validation $validation Validation instance
*/
public function __construct(Validation $validation) {
$this->validation = $validation;
}

/**
* Get errors
* @return array
*/
public function getErrors(): array {
return $this->errors;
Expand All @@ -47,7 +57,7 @@ public function getErrors(): array {
* @param mixed $rule Rule to add
* @return $this
*/
public function rule($rule) {
public function rule(mixed $rule) {
if ( is_array($rule) ) {
foreach ($rule as $item) {
$this->rule($item);
Expand Down Expand Up @@ -85,7 +95,6 @@ public function rule($rule) {
* @param array $fields Fields array
* @param string $key Field key
* @param bool $bail Bail flag
* @return bool
*/
public function check(array $fields, string $key, bool $bail = false): bool {
$passed = true;
Expand All @@ -112,7 +121,23 @@ public function check(array $fields, string $key, bool $bail = false): bool {
if ( method_exists($this, $method) ) {
$callable = [$this, $method];
} else {
throw new RuntimeException('Unknown rule type');
if ( $this->validation->hasRule($type) ) {
$handler = $this->validation->getRule($type);
if ($handler instanceof Closure) {
$callable = $handler;
} else if ( class_exists($handler) ) {
$instance = new $handler();
if ($instance instanceof RuleInterface) {
$callable = $instance;
} else {
throw new RuntimeException('Must implement RuleInterface');
}
} else {
throw new RuntimeException("Unknown rule type: {$type}");
}
} else {
throw new RuntimeException("Unknown rule type: {$type}");
}
}
}
}
Expand Down
1 change: 0 additions & 1 deletion src/Validation/RuleInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ interface RuleInterface {
* @param string $key Field key
* @param array $options Options array
* @param Closure $fail Failure callback
* @return void
*/
public function __invoke(array $fields, string $key, array $options, Closure $fail): void;
}
20 changes: 1 addition & 19 deletions src/Validation/ValidatesData.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ trait ValidatesData {
* @param string $key Field name
* @param array $options Rule options
* @param Closure $fail Failure callback
* @return void
*/
public function validateRequired(array $fields, string $key, array $options, Closure $fail): void {
$value = $fields[$key] ?? null;
Expand All @@ -37,7 +36,6 @@ public function validateRequired(array $fields, string $key, array $options, Clo
* @param string $key Field name
* @param array $options Rule options
* @param Closure $fail Failure callback
* @return void
*/
public function validateAlpha(array $fields, string $key, array $options, Closure $fail): void {
$value = $fields[$key] ?? null;
Expand All @@ -54,7 +52,6 @@ public function validateAlpha(array $fields, string $key, array $options, Closur
* @param string $key Field name
* @param array $options Rule options
* @param Closure $fail Failure callback
* @return void
*/
public function validateAlphanum(array $fields, string $key, array $options, Closure $fail): void {
$value = $fields[$key] ?? null;
Expand All @@ -71,7 +68,6 @@ public function validateAlphanum(array $fields, string $key, array $options, Clo
* @param string $key Field name
* @param array $options Rule options
* @param Closure $fail Failure callback
* @return void
*/
public function validateNum(array $fields, string $key, array $options, Closure $fail): void {
$value = $fields[$key] ?? null;
Expand All @@ -88,7 +84,6 @@ public function validateNum(array $fields, string $key, array $options, Closure
* @param string $key Field name
* @param array $options Rule options
* @param Closure $fail Failure callback
* @return void
*/
public function validateSlug(array $fields, string $key, array $options, Closure $fail): void {
$value = $fields[$key] ?? null;
Expand All @@ -105,7 +100,6 @@ public function validateSlug(array $fields, string $key, array $options, Closure
* @param string $key Field name
* @param array $options Rule options
* @param Closure $fail Failure callback
* @return void
*/
public function validateRegex(array $fields, string $key, array $options, Closure $fail): void {
$value = $fields[$key] ?? null;
Expand All @@ -122,7 +116,6 @@ public function validateRegex(array $fields, string $key, array $options, Closur
* @param string $key Field name
* @param array $options Rule options
* @param Closure $fail Failure callback
* @return void
*/
public function validateEmail(array $fields, string $key, array $options, Closure $fail): void {
$value = $fields[$key] ?? null;
Expand All @@ -139,7 +132,6 @@ public function validateEmail(array $fields, string $key, array $options, Closur
* @param string $key Field name
* @param array $options Rule options
* @param Closure $fail Failure callback
* @return void
*/
public function validateSame(array $fields, string $key, array $options, Closure $fail): void {
$value = $fields[$key] ?? null;
Expand All @@ -157,7 +149,6 @@ public function validateSame(array $fields, string $key, array $options, Closure
* @param string $key Field name
* @param array $options Rule options
* @param Closure $fail Failure callback
* @return void
*/
public function validateDifferent(array $fields, string $key, array $options, Closure $fail): void {
$value = $fields[$key] ?? null;
Expand All @@ -175,7 +166,6 @@ public function validateDifferent(array $fields, string $key, array $options, Cl
* @param string $key Field name
* @param array $options Rule options
* @param Closure $fail Failure callback
* @return void
*/
public function validateAfter(array $fields, string $key, array $options, Closure $fail): void {
$value = $fields[$key] ?? null;
Expand All @@ -193,7 +183,6 @@ public function validateAfter(array $fields, string $key, array $options, Closur
* @param string $key Field name
* @param array $options Rule options
* @param Closure $fail Failure callback
* @return void
*/
public function validateBefore(array $fields, string $key, array $options, Closure $fail): void {
$value = $fields[$key] ?? null;
Expand All @@ -211,7 +200,6 @@ public function validateBefore(array $fields, string $key, array $options, Closu
* @param string $key Field name
* @param array $options Rule options
* @param Closure $fail Failure callback
* @return void
*/
public function validateBetween(array $fields, string $key, array $options, Closure $fail): void {
$value = $fields[$key] ?? null;
Expand Down Expand Up @@ -239,7 +227,6 @@ public function validateBetween(array $fields, string $key, array $options, Clos
* @param string $key Field name
* @param array $options Rule options
* @param Closure $fail Failure callback
* @return void
*/
public function validateMin(array $fields, string $key, array $options, Closure $fail): void {
$value = $fields[$key] ?? null;
Expand All @@ -266,7 +253,6 @@ public function validateMin(array $fields, string $key, array $options, Closure
* @param string $key Field name
* @param array $options Rule options
* @param Closure $fail Failure callback
* @return void
*/
public function validateMax(array $fields, string $key, array $options, Closure $fail): void {
$value = $fields[$key] ?? null;
Expand All @@ -293,7 +279,6 @@ public function validateMax(array $fields, string $key, array $options, Closure
* @param string $key Field name
* @param array $options Rule options
* @param Closure $fail Failure callback
* @return void
*/
public function validateSize(array $fields, string $key, array $options, Closure $fail): void {
$value = $fields[$key] ?? null;
Expand All @@ -320,7 +305,6 @@ public function validateSize(array $fields, string $key, array $options, Closure
* @param string $key Field name
* @param array $options Rule options
* @param Closure $fail Failure callback
* @return void
*/
public function validateArray(array $fields, string $key, array $options, Closure $fail): void {
$value = $fields[$key] ?? null;
Expand All @@ -336,7 +320,6 @@ public function validateArray(array $fields, string $key, array $options, Closur
* @param string $key Field name
* @param array $options Rule options
* @param Closure $fail Failure callback
* @return void
*/
public function validateNumeric(array $fields, string $key, array $options, Closure $fail): void {
$value = $fields[$key] ?? null;
Expand All @@ -352,7 +335,6 @@ public function validateNumeric(array $fields, string $key, array $options, Clos
* @param string $key Field name
* @param array $options Rule options
* @param Closure $fail Failure callback
* @return void
*/
public function validateString(array $fields, string $key, array $options, Closure $fail): void {
$value = $fields[$key] ?? null;
Expand All @@ -361,4 +343,4 @@ public function validateString(array $fields, string $key, array $options, Closu
$fail('validation.string');
}
}
}
}
56 changes: 48 additions & 8 deletions src/Validation/Validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,33 +20,59 @@ class Validation {

/**
* Conditions array
* @var array
*/
protected $conditions = [];
protected array $conditions = [];

/**
* Rules array
*/
protected array $rules = [];

/**
* Error bag
* @var array
*/
protected $errors = [];
protected array $errors = [];

/**
* Get conditions array
* @return array
*/
public function getConditions(): array {
return $this->conditions;
}

/**
* Get rules array
*/
public function getRules(): array {
return $this->rules;
}

/**
* Check if rule exists
*/
public function hasRule(string $name): bool {
return isset( $this->rules[$name] );
}

/**
* Get rule handler
*/
public function getRule(string $name): mixed {
if (! isset( $this->rules[$name] ) ) {
throw new InvalidArgumentException("Rule {$name} does not exist");
}
return $this->rules[$name];
}

/**
* Add condition
* @param string $field Field name
* @param mixed $rules Condition rules
* @return $this
*/
public function condition(string $field, $rules) {
public function condition(string $field, mixed $rules) {
if (! isset( $this->conditions[$field] ) ) {
$this->conditions[$field] = new Condition();
$this->conditions[$field] = new Condition($this);
}
if ( is_array($rules) || is_string($rules) || $rules instanceof Closure ) {
$this->conditions[$field]->rule($rules);
Expand All @@ -56,11 +82,25 @@ public function condition(string $field, $rules) {
return $this;
}

/**
* Add rule
* @param string $name Rule name
* @param mixed $handler Rule handler
* @return $this
*/
public function rule(string $name, mixed $handler) {
if ( is_string($handler) || $handler instanceof Closure ) {
$this->rules[$name] = $handler;
} else {
throw new InvalidArgumentException('Invalid handler type specified');
}
return $this;
}

/**
* Validate fields
* @param array $fields Fields array
* @param bool $bail Bail flag
* @return bool
*/
public function validate(array $fields, bool $bail = false): bool {
$this->errors = [];
Expand Down
4 changes: 1 addition & 3 deletions src/Validation/ValidationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ class ValidationException extends RuntimeException {

/**
* Error bag
* @var array
*/
protected $errors;
protected array $errors;

/**
* Constructor
Expand All @@ -33,7 +32,6 @@ public function __construct(array $errors, string $message = '', int $code = 0,

/**
* Get errors instance
* @return array
*/
public function getErrors(): array {
return $this->errors;
Expand Down
Loading

0 comments on commit abbee87

Please sign in to comment.