diff --git a/example/arrays.php b/example/arrays.php index e981080..0c90145 100644 --- a/example/arrays.php +++ b/example/arrays.php @@ -9,42 +9,54 @@ $schema = new Schema(); $validate = new Validate(); -$source = [ - "name" =>"wepesi", - "email" => "info@wepesi.com", - "possessions" => [ - "cars" => 2, - "plane" => 0, - "houses" => 4, - "children" => -3, - "children_name" => ['alfa', 3, false], - "children_age" => [12,'6.2', 'rachel'], +$data_source_pass = [ + "family" => [ + "children" => 3, + "children_name" => ['alfa', 'beta', 'omega'], + "children_age" => [12, 9, 3], "location" => [ "goma" => "Q.Virunga 08", - "bukabu" => "Bagira 10", - "kinshasa" => "matadi kibala 05" + "bukabu" => "Bagira 10" + ] + ] +]; +$data_source_fail = [ + "family" => [ + "children" => 3, + "children_name" => ['alfa', 3, 'blue'], + "children_age" => [12, '6.2', 'rachel'], + "location" => [ + "goma" => "goma", + "bukabu" => "Bagira", + "kinshasa" => "Gombe ", + "Lubumbashi" => "lushi" ] ] ]; -$rules =[ - "name" => $schema->string()->min(1)->max(10)->required()->generate(), - "email" => $schema->string()->email()->required()->generate(), - "possessions" => $schema->array()->min(1)->max(20)->required()->structure([ - "cars" => $schema->number()->min(1)->required()->generate(), - "plane" => $schema->number()->min(1)->required()->generate(), - "houses" => $schema->number()->min(6)->required()->generate(), - "children" => $schema->number()->positive()->required()->generate(), - 'children_name' => $schema->array()->string()->generate(), - 'children_age' => $schema->array()->number()->generate(), - "location" => $schema->array()->min(2)->structure([ - "goma" => $schema->string()->min(20)->generate(), - "bukabu" => $schema->string()->min(20)->generate(), - "kinshasa" => $schema->string()->min(20)->generate(), - ])->generate() - ])->generate() +$rules = [ + "family" => $schema->array()->min(1)->max(20)->required()->structure([ + "children" => $schema->number()->positive()->min(1)->max(5)->required(), + 'children_name' => $schema->array()->string(), + 'children_age' => $schema->array()->number(), + "location" => $schema->array()->min(2)->max(3)->structure([ + "goma" => $schema->string(), + "bukabu" => $schema->string(), + "kinshasa" => $schema->any(), + "Lubumbashi" => $schema->any(), + ]) + ]) ]; -$validate->check($source, $rules); -//// check if the validation passed or not -include_once __DIR__ . '/vardump.php'; \ No newline at end of file +// check if the validation passed or not +$validate->check($data_source_pass, $rules); +var_dump([ + 'passed' => $validate->passed(), + 'errors' => $validate->errors() +]); + +$validate->check($data_source_fail, $rules); +var_dump([ + 'passed' => $validate->passed(), + 'errors' => $validate->errors() +]); \ No newline at end of file diff --git a/example/boolean.php b/example/boolean.php index 631e6e7..0dcd684 100644 --- a/example/boolean.php +++ b/example/boolean.php @@ -12,10 +12,13 @@ ]; //define the schema model for validation $rules=[ - "status" => $schema->boolean()->required()->max(true)->isValid('TRUE')->generate(), - "activated" => $schema->boolean()->required()->min(false)->isValid('FALSE')->generate(), + "status" => $schema->boolean()->required()->isValid('TRUE')->generate(), + "activated" => $schema->boolean()->required()->isValid('FALSE')->generate(), ]; $validate->check($data_source,$rules); // check if the validation passed or not -include_once __DIR__ . '/vardump.php'; \ No newline at end of file +var_dump([ + 'passed' => $validate->passed(), + 'errors' => $validate->errors() +]); \ No newline at end of file diff --git a/example/date.php b/example/date.php index 2d40f89..45e73dd 100644 --- a/example/date.php +++ b/example/date.php @@ -3,17 +3,35 @@ * Copyright (c) 2022. Wepesi validation. * @author Boss Ibrahim Mussa */ -$validate = new \Wepesi\App\Validate(); -$schema = new \Wepesi\App\Schema(); -$data_source = [ +use Wepesi\App\Schema; +use Wepesi\App\Validate; + +$validate = new Validate(); +$schema = new Schema(); + +$data_source_pass = [ + 'birth_day' => date('Y-m-d',strtotime('19years + now')), + 'date_created' => date('Y-m-d H:i:s',strtotime('now + 1second')) +]; +$data_source_fail = [ 'birth_day' => '2021-05-23', 'date_created' => '2021-05-23' ]; -$rules=[ - "birth_day" => $schema->date()->min("-18years")->required()->generate(), - "date_created" => $schema->date()->now()->max("100years")->required()->generate() - ]; - $validate->check($data_source,$rules); +$rules = [ + 'birth_day' => $schema->date()->min("18years")->required()->generate(), + 'date_created' => $schema->date()->now()->max("100years")->required()->generate() +]; +$validate->check($data_source_pass, $rules); + +var_dump([ + 'passed' => $validate->passed(), + 'errors' => $validate->errors() +]); + +$validate->check($data_source_fail, $rules); -include_once __DIR__ . '/vardump.php'; \ No newline at end of file +var_dump([ + 'passed' => $validate->passed(), + 'errors' => $validate->errors() +]); \ No newline at end of file diff --git a/example/index.php b/example/index.php index c34c0e0..b3cdb36 100644 --- a/example/index.php +++ b/example/index.php @@ -4,6 +4,9 @@ * @author Boss Ibrahim Mussa */ +use Wepesi\App\Validator\StringValidator; + +require_once __DIR__ . '/../vendor/autoload.php'; $data_source = [ "name" => "ibrahim", "age" => 12, diff --git a/example/number.php b/example/number.php index 72d164a..cbbbdd6 100644 --- a/example/number.php +++ b/example/number.php @@ -4,22 +4,38 @@ * @author Boss Ibrahim Mussa */ -$validate = new \Wepesi\App\Validate(); -$schema = new \Wepesi\App\Schema(); -$data_source = [ +use Wepesi\App\Schema; +use Wepesi\App\Validate; + +$validate = new Validate(); +$schema = new Schema(); +$data_source_pass = [ + "age" => 10, + "length" => 3, + "height" => 10, + "level" => -10, +]; + +$data_source_fail = [ "age" => 20, "length" => 0, - "height" =>"35", - "width" =>"", - "direction" => -7 + "height" => -35, + "level" => 10, +]; +$rules = [ + "age" => $schema->number()->min(8)->max(15)->required(), + "length" => $schema->number()->min(1)->max(10), + "height" => $schema->number()->positive(), + "level" => $schema->number()->negative(), ]; -$rules=[ - "age" => $schema->number()->min(8)->max(15)->required()->generate(), - "length" => $schema->number()->min(1)->max(10)->required()->generate(), - "height" => $schema->number()->min(18)->max(50)->required()->generate(), - "width" => $schema->number()->min(3)->max(50)->required()->generate(), - "direction" => $schema->number()->min(3)->max(50)->positive()->required()->generate(), - ]; -$validate->check($data_source,$rules); +$validate->check($data_source_pass, $rules); +var_dump([ + 'passed' => $validate->passed(), + 'errors' => $validate->errors() +]); -include_once __DIR__ . '/vardump.php'; \ No newline at end of file +$validate->check($data_source_fail, $rules); +var_dump([ + 'passed' => $validate->passed(), + 'errors' => $validate->errors() +]); \ No newline at end of file diff --git a/example/string.php b/example/string.php index 985a097..7d2a963 100644 --- a/example/string.php +++ b/example/string.php @@ -12,24 +12,44 @@ $schema = new Schema(); $validate = new Validate(); -$source = [ +$source_to_pass = [ 'name' => 'ibrahim', - 'country' => "", + 'country' => null, 'password' => '1234567', - 'new_password' => 123456, + 'new_password' => 1234567, 'email' => 'infos@wepesi.com', 'link' => 'https://github.com/bim-g/wepesi_validation/', + 'ip' => '127.0.0.1', +]; +$source_to_fail = [ + 'name' => 'ibrahim', + 'country' => "", + 'password' => '1234567', + 'new_password' => 123456, + 'email' => 'i@w.com', + 'link' => 'google', 'ip' => '192.168', ]; $rules = [ - "name" => $schema->string()->email()->min(35)->max(50)->required()->generate(), - "country" => $schema->string()->min(30)->max(40)->required()->generate(), - "password" => $schema->string()->min(3)->max(40)->generate(), - "new_password" => $schema->string()->min(3)->max(40)->match("password")->generate(), - "email" => $schema->string()->min(3)->max(40)->email()->generate(), - "link" => $schema->string()->min(3)->max(40)->url()->generate(), - "ip" => $schema->string()->addressIp()->generate() + "name" => $schema->string()->min(3)->max(50)->required(), + "country" => $schema->any(), + "password" => $schema->string()->min(3)->max(40), + "new_password" => $schema->string()->min(3)->max(40)->match("password"), + "email" => $schema->string()->min(10)->max(40)->email(), + "link" => $schema->string()->min(10)->max(45)->url(), + "ip" => $schema->string()->addressIp() ]; -$validate->check($source, $rules); -//// check if the validation passed or not -include_once __DIR__."/vardump.php"; \ No newline at end of file + +$validate->check($source_to_pass, $rules); +// validation pass +var_dump([ + 'passed' => $validate->passed(), + 'errors' => $validate->errors() +]); + +// validation fail +$validate->check($source_to_fail, $rules); +var_dump([ + 'passed' => $validate->passed(), + 'errors' => $validate->errors() +]); \ No newline at end of file diff --git a/index.php b/index.php deleted file mode 100644 index 35c23fc..0000000 --- a/index.php +++ /dev/null @@ -1,10 +0,0 @@ -items; } + + /** + * @param $method + * @param $arg + * @return mixed|void + */ + public function __call($method, $arg) + { + if (method_exists($this, $method)) { + return call_user_func_array([$this, $method], $arg); + } + } } \ No newline at end of file diff --git a/example/vardump.php b/src/Providers/Contracts/MessageBuilderContracts.php similarity index 50% rename from example/vardump.php rename to src/Providers/Contracts/MessageBuilderContracts.php index ed869e7..dd76f81 100644 --- a/example/vardump.php +++ b/src/Providers/Contracts/MessageBuilderContracts.php @@ -3,7 +3,10 @@ * Copyright (c) 2023. Wepesi validation. * @author Boss Ibrahim Mussa */ -var_dump([ - 'passed' => $validate->passed(), - 'errors' => $validate->errors() -]); \ No newline at end of file + +namespace Wepesi\App\Providers\Contracts; + +interface MessageBuilderContracts +{ + +} \ No newline at end of file diff --git a/src/Providers/SChemaProvider.php b/src/Providers/SchemaProvider.php similarity index 51% rename from src/Providers/SChemaProvider.php rename to src/Providers/SchemaProvider.php index dc986d6..e087688 100644 --- a/src/Providers/SChemaProvider.php +++ b/src/Providers/SchemaProvider.php @@ -11,7 +11,7 @@ /** * */ -abstract class SChemaProvider implements SchemaContracts +abstract class SchemaProvider implements SchemaContracts { /** * @var array @@ -22,22 +22,13 @@ abstract class SChemaProvider implements SchemaContracts */ protected string $class_name; - /** - * @param string $class_name - */ - public function __construct(string $class_name) - { - $this->class_name = $class_name; - $this->schema[$this->class_name] = []; - } - /** * @param int $rule - * @return SChemaProvider + * @return SchemaProvider */ - public function min(int $rule): SChemaProvider + public function min(int $rule): SchemaProvider { - $this->schema[$this->class_name]["min"] = $rule; + $this->schema["min"] = $rule; return $this; } @@ -45,18 +36,18 @@ public function min(int $rule): SChemaProvider * @param $rule * @return $this */ - public function max($rule): SChemaProvider + public function max($rule): SchemaProvider { - $this->schema[$this->class_name]["max"] = $rule; + $this->schema["max"] = $rule; return $this; } /** * @return $this */ - public function required(): SChemaProvider + public function required(): SchemaProvider { - $this->schema[$this->class_name]["required"] = true; + $this->schema["required"] = true; return $this; } @@ -65,6 +56,13 @@ public function required(): SChemaProvider */ public function generate(): array { - return $this->schema; + $reflexion = new \ReflectionClass($this); + return [$reflexion->getName() => $this->schema]; + } + + public function __call($method, $args){ + if(method_exists($this,$method)){ + return call_user_func_array([$this,$method], $args); + } } } \ No newline at end of file diff --git a/src/Providers/ValidatorProvider.php b/src/Providers/ValidatorProvider.php index 4b22a31..bbd06c0 100644 --- a/src/Providers/ValidatorProvider.php +++ b/src/Providers/ValidatorProvider.php @@ -8,6 +8,7 @@ use Wepesi\App\MessageErrorBuilder; use Wepesi\App\Providers\Contracts\Contracts; +use Wepesi\App\Providers\Contracts\MessageBuilderContracts; /** * Validator provider model @@ -33,15 +34,15 @@ abstract class ValidatorProvider implements Contracts /** * @var MessageErrorBuilder */ - protected MessageErrorBuilder $messageItem; - /** - * - */ + protected MessageErrorBuilder $message_error_builder; - function __construct() + function __construct(string $item, array $data_source = []) { $this->errors = []; - $this->messageItem = new MessageErrorBuilder(); + $this->message_error_builder = new MessageErrorBuilder(); + $this->data_source = $data_source; + $this->field_name = $item; + $this->field_value = $data_source[$item]; } /** @@ -60,14 +61,18 @@ abstract public function max(int $rule); * Provide validation module name * @return string */ - abstract protected function classProvider(): string ; + protected function classProvider(): string { + $reflexion = new \ReflectionClass($this); + return str_replace('validator','',strtolower($reflexion->getShortName())); + } /** * @return string */ private function getClassProvider(): string { - return $this->classProvider && strlen($this->classProvider) > 0 ? $this->classProvider : 'unknown'; + $reflexion = new \ReflectionClass($this); + return str_replace('validator','',strtolower($reflexion->getShortName())); } /** * @return void @@ -76,30 +81,30 @@ public function required() { if (is_array($this->field_value)) { if (count($this->field_value) == 0) { - $this->messageItem + $this->message_error_builder ->type($this->getClassProvider() . ' required') ->label($this->field_name) ->message("'$this->field_name' is required"); - $this->addError($this->messageItem); + $this->addError($this->message_error_builder); } } else { $required_value = trim($this->field_value); if (strlen($required_value) == 0) { - $this->messageItem - ->type($this->classProvider() . ' required') + $this->message_error_builder + ->type($this->getClassProvider() . ' required') ->message("'$this->field_name' is required") ->label($this->field_name); - $this->addError($this->messageItem); + $this->addError($this->message_error_builder); } } } /** * - * @param array $value + * @param MessageBuilderContracts $item * @return void */ - public function addError(MessageErrorBuilder $item): void + public function addError(MessageBuilderContracts $item): void { $this->errors[] = $item->generate(); } @@ -117,18 +122,22 @@ public function result(): array * @param bool $max * @return bool */ - protected function positiveParamMethod(int $rule, bool $max = false): bool + protected function checkNotPositiveParamMethod(int $rule, bool $max = false): bool { - $status = true; + $status = false; if ($rule < 1) { - $method = $max ? "max" : "min"; - $this->messageItem + $method = $max ? 'max' : 'min'; + $this->message_error_builder ->type($this->getClassProvider() . ' method ' . $method) ->message("'$this->field_name' $method param should be a positive number") ->label($this->field_name); - $this->addError($this->messageItem); - $status = false; + $this->addError($this->message_error_builder); + $status = true; } return $status; } + + protected function typeError(string $type){ + return $this->getClassProvider() . ".$type"; + } } \ No newline at end of file diff --git a/src/Schema.php b/src/Schema.php index 17b6f6d..b5674ee 100644 --- a/src/Schema.php +++ b/src/Schema.php @@ -15,7 +15,7 @@ /** * */ -class Schema +final class Schema { /** diff --git a/src/Schema/ArraySchema.php b/src/Schema/ArraySchema.php index 1fbf1c6..367bb47 100644 --- a/src/Schema/ArraySchema.php +++ b/src/Schema/ArraySchema.php @@ -6,31 +6,22 @@ namespace Wepesi\App\Schema; -use Wepesi\App\Providers\SChemaProvider; +use Wepesi\App\Providers\SchemaProvider; /** - * Arraye schema validation + * Array schema validation */ -final class ArraySchema extends SChemaProvider +final class ArraySchema extends SchemaProvider { - /** - * - */ - public function __construct() - { - parent::__construct(__CLASS__); - } - /** * @param array $elements data array to be validated * @return $this */ public function structure(array $elements): ?ArraySchema { - if (isset($this->schema[$this->class_name]['string']) || isset($this->schema[$this->class_name]['number'])) { - return false; + if (! isset($this->schema['string']) || ! isset($this->schema['number'])) { + $this->schema['structure'] = $elements; } - $this->schema[$this->class_name]['structure'] = $elements; return $this; } @@ -40,10 +31,9 @@ public function structure(array $elements): ?ArraySchema */ public function string(): ?ArraySchema { - if (isset($this->schema[$this->class_name]['number'])) { - return false; + if (! isset($this->schema['number'])) { + $this->schema['string'] = true; } - $this->schema[$this->class_name]['string'] = true; return $this; } @@ -52,10 +42,9 @@ public function string(): ?ArraySchema */ public function number(): ?ArraySchema { - if (isset($this->schema[$this->class_name]['string'])) { - return false; + if (! isset($this->schema['string'])) { + $this->schema['number'] = true; } - $this->schema[$this->class_name]['number'] = true; return $this; } } \ No newline at end of file diff --git a/src/Schema/BooleanSchema.php b/src/Schema/BooleanSchema.php index 1261bc4..037bd1f 100644 --- a/src/Schema/BooleanSchema.php +++ b/src/Schema/BooleanSchema.php @@ -11,30 +11,21 @@ * and open the template in the editor. */ -use Wepesi\App\Providers\SChemaProvider; +use Wepesi\App\Providers\SchemaProvider; /** * Description of String * * @author Domeshow */ -final class BooleanSchema extends SChemaProvider +final class BooleanSchema extends SchemaProvider { - - /** - * - */ - function __construct() - { - parent::__construct(__CLASS__); - } - /** * @return $this */ function isValid(): BooleanSchema { - $this->schema[$this->class_name]['isValid'] = true; + $this->schema['isValid'] = true; return $this; } } diff --git a/src/Schema/DateSchema.php b/src/Schema/DateSchema.php index f9fd059..203b3b6 100644 --- a/src/Schema/DateSchema.php +++ b/src/Schema/DateSchema.php @@ -6,21 +6,14 @@ namespace Wepesi\App\Schema; -use Wepesi\App\Providers\SChemaProvider; +use Wepesi\App\Providers\SchemaProvider; /** * Schema datetime * */ -final class DateSchema extends SChemaProvider +final class DateSchema extends SchemaProvider { - /** - * - */ - function __construct() - { - parent::__construct(__CLASS__); - } /** * @param string $rule @@ -28,7 +21,7 @@ function __construct() */ public function min($rule): DateSchema { - $this->schema[$this->class_name]['min'] = $rule; + $this->schema['min'] = $rule; return $this; } @@ -38,7 +31,7 @@ public function min($rule): DateSchema */ public function max($rule): DateSchema { - $this->schema[$this->class_name]['max'] = $rule; + $this->schema['max'] = $rule; return $this; } @@ -47,7 +40,7 @@ public function max($rule): DateSchema */ function now(): DateSchema { - $this->schema[$this->class_name]["now"] = true; + $this->schema["now"] = true; return $this; } @@ -56,7 +49,7 @@ function now(): DateSchema */ function today(): DateSchema { - $this->schema[$this->class_name]["today"] = true; + $this->schema["today"] = true; return $this; } } \ No newline at end of file diff --git a/src/Schema/NumberSchema.php b/src/Schema/NumberSchema.php index 9e7d17c..9d06cba 100644 --- a/src/Schema/NumberSchema.php +++ b/src/Schema/NumberSchema.php @@ -6,29 +6,29 @@ namespace Wepesi\App\Schema; -use Wepesi\App\Providers\SChemaProvider; +use Wepesi\App\Providers\SchemaProvider; /** * Schema number validation * validate any format number */ -final class NumberSchema extends SChemaProvider +final class NumberSchema extends SchemaProvider { - /** - * + * @return $this */ - function __construct() + public function positive(): NumberSchema { - parent::__construct(__CLASS__); + $this->schema['positive'] = true; + return $this; } /** * @return $this */ - function positive(): NumberSchema + public function negative(): NumberSchema { - $this->schema[$this->class_name]['positive'] = true; + $this->schema['negative'] = true; return $this; } } diff --git a/src/Schema/StringSchema.php b/src/Schema/StringSchema.php index 08e2eee..9fd78e9 100644 --- a/src/Schema/StringSchema.php +++ b/src/Schema/StringSchema.php @@ -11,30 +11,21 @@ * and open the template in the editor. */ -use Wepesi\App\Providers\SChemaProvider; +use Wepesi\App\Providers\SchemaProvider; /** - * String valiation schema + * String validation schema * validate string value */ -final class StringSchema extends SChemaProvider +final class StringSchema extends SchemaProvider { - - /** - * - */ - public function __construct() - { - parent::__construct(__CLASS__); - } - /** * @return $this */ public function email(): StringSchema { - $this->schema[$this->class_name]["email"] = true; + $this->schema["email"] = true; return $this; } @@ -44,7 +35,7 @@ public function email(): StringSchema */ public function url(): StringSchema { - $this->schema[$this->class_name]["url"] = true; + $this->schema["url"] = true; return $this; } @@ -55,20 +46,20 @@ public function url(): StringSchema */ public function match(string $key_to_match): StringSchema { - $this->schema[$this->class_name]["match"] = $key_to_match; + $this->schema["match"] = $key_to_match; return $this; } /** - * @param string $ip_address + * @param bool $ipv6 * @return $this */ public function addressIp(bool $ipv6 = false): StringSchema { if ($ipv6) { - $this->schema[$this->class_name]['addressIpv6'] = true; + $this->schema['addressIpv6'] = true; } else { - $this->schema[$this->class_name]['addressIp'] = true; + $this->schema['addressIp'] = true; } return $this; } diff --git a/src/Traits/ExceptionTraits.php b/src/Traits/ExceptionTraits.php index a5648fc..01ed627 100644 --- a/src/Traits/ExceptionTraits.php +++ b/src/Traits/ExceptionTraits.php @@ -19,6 +19,6 @@ trait ExceptionTraits */ protected function exception($ex): array { - return ['exception' => $ex->getMessage()]; + return ['exception' => $ex]; } } \ No newline at end of file diff --git a/src/Validate.php b/src/Validate.php index 075108e..8872111 100644 --- a/src/Validate.php +++ b/src/Validate.php @@ -6,12 +6,13 @@ namespace Wepesi\App; +use Wepesi\App\Providers\Contracts\MessageBuilderContracts; use Wepesi\Resolver\OptionsResolver; use Wepesi\Resolver\Option; /** * */ -class Validate +final class Validate { /** * @var array @@ -55,13 +56,13 @@ function check(array $resource, array $schema) $resolver = new OptionsResolver($option_resolver); $options = $resolver->resolve($schema); - $exceptions = isset($options['exception']) || isset($options['InvalidArgumentException']) ?? false; + $exceptions = isset($options['InvalidArgumentException']) ?? false; if ($exceptions) { $this->message ->type('object.unknown') - ->message($options['exception'] ?? $options['InvalidArgumentException']) + ->message($options['InvalidArgumentException']) ->label("exception"); - $this->addError($message); + $this->addError($this->message); } else { foreach ($schema as $item => $rules) { if (!is_array($rules) && is_object($rules)) { @@ -71,6 +72,7 @@ function check(array $resource, array $schema) $rules = $rules->generate(); } $class_namespace = array_keys($rules)[0]; + // In case the method is defined as any it will not be checked. if ($class_namespace == "any") continue; $validator_class = str_replace("Schema", "Validator", $class_namespace); $reflexion = new \ReflectionClass($validator_class); @@ -97,10 +99,10 @@ function check(array $resource, array $schema) } /** - * @param array $message + * @param MessageBuilderContracts $item * @return void */ - private function addError(MessageErrorBuilder $item) + private function addError(MessageBuilderContracts $item) { $this->errors[] = $item->generate(); } diff --git a/src/Validator/ArrayValidator.php b/src/Validator/ArrayValidator.php index 532d236..636139d 100644 --- a/src/Validator/ArrayValidator.php +++ b/src/Validator/ArrayValidator.php @@ -20,11 +20,7 @@ final class ArrayValidator extends ValidatorProvider */ public function __construct(string $item, array $data_source = []) { - $this->errors = []; - $this->data_source = $data_source; - $this->field_name = $item; - $this->field_value = $data_source[$item]; - parent::__construct(); + parent::__construct($item,$data_source); } /** @@ -35,14 +31,14 @@ public function __construct(string $item, array $data_source = []) public function min(int $rule): void { // TODO: Implement min() method. - if ($this->positiveParamMethod($rule)) return; + if ($this->checkNotPositiveParamMethod($rule)) return; if (count($this->field_value) < $rule) { - $this->messageItem - ->type('array.min') + $this->message_error_builder + ->type($this->typeError('min')) ->message("`$this->field_name` should have a minimum of `$rule` elements") ->label($this->field_name) ->limit($rule); - $this->addError($this->messageItem); + $this->addError($this->message_error_builder); } } @@ -53,14 +49,14 @@ public function min(int $rule): void public function max(int $rule): void { // TODO: Implement max() method. - if ($this->positiveParamMethod($rule, true)) return; + if ($this->checkNotPositiveParamMethod($rule, true)) return; if (count($this->field_value) > $rule) { - $this->messageItem - ->type('array.max') + $this->message_error_builder + ->type($this->typeError('max')) ->message("`$this->field_name` should have a maximum of `$rule` elements") ->label($this->field_name) ->limit($rule); - $this->addError($this->messageItem); + $this->addError($this->message_error_builder); } } @@ -74,7 +70,14 @@ public function structure(array $elements): void $element_source = $this->data_source[$this->field_name]; $validate->check($element_source, $elements); if (!$validate->passed()) { - foreach ($validate->errors() as $error) $this->addError($error); + foreach ($validate->errors() as $error) { + $this->message_error_builder + ->type($error['type']) + ->message($error['message']) + ->label($error['label']) + ->limit($error['limit']??1); + $this->addError($this->message_error_builder); + } } } @@ -94,12 +97,12 @@ public function string(): void $keys = array_keys($filter); for ($i = 0; $i < count($keys); $i++) { $position = $keys[$i]; - $this->messageItem - ->type('array.string') + $this->message_error_builder + ->type($this->typeError('string')) ->message("`$this->field_name[$position]` should be a string") ->label($this->field_name) ->limit(1); - $this->addError($this->messageItem); + $this->addError($this->message_error_builder); } } } @@ -120,21 +123,13 @@ public function number(): void $keys = array_keys($filter); for ($i = 0; $i < count($keys); $i++) { $position = $keys[$i]; - $this->messageItem + $this->message_error_builder ->type('array.number') ->message("`$this->field_name[$position]` should be a number") ->label($this->field_name) ->limit(count($keys)); - $this->addError($this->messageItem); + $this->addError($this->message_error_builder); } } } - - /** - * @return string - */ - protected function classProvider(): string - { - return 'array'; - } } \ No newline at end of file diff --git a/src/Validator/BooleanValidator.php b/src/Validator/BooleanValidator.php index 875c441..74a61fa 100644 --- a/src/Validator/BooleanValidator.php +++ b/src/Validator/BooleanValidator.php @@ -28,14 +28,8 @@ final class BooleanValidator extends ValidatorProvider */ function __construct(string $item, array $source) { - $this->field_name = $item; - $this->data_source = $source; - $this->field_value = $source[$this->field_name]; - - if ($this->isBoolean()) { - $this->field_value = $source[$item]; - } - parent::__construct(); + parent::__construct($item, $source); + $this->isBoolean(); } /** @@ -57,23 +51,21 @@ public function max($rule) /** * * @param string|null $itemKey - * @return boolean + * @return void */ - private function isBoolean(string $itemKey = null): bool + private function isBoolean(string $itemKey = null): void { $item_to_check = !$itemKey ? $this->field_name : $itemKey; $val = $this->data_source[$item_to_check]; $regex = "/^(true|false)$/"; if (!preg_match($regex, is_bool($val) ? ($val ? 'true' : 'false') : $val)) { - $this->messageItem - ->type('boolean.unknown') + $this->message_error_builder + ->type($this->typeError('unknown')) ->message("`$item_to_check` should be a boolean") ->label($item_to_check); - $this->addError($this->messageItem); - return false; + $this->addError($this->message_error_builder); } - return true; } /** @@ -86,30 +78,22 @@ public function isValid(string $value) $required_value = strtolower($passed_value); $check = $required_value == 'true' || $required_value == 'false'; if (!($check)) { - $this->messageItem - ->type('boolean.required') + $this->message_error_builder + ->type($this->typeError('required')) ->message("isValid param must be boolean but you put `$required_value`") ->label($this->field_name); - $this->addError($this->messageItem); + $this->addError($this->message_error_builder); } else { // $sting_value= is_bool($this->string_value); $incoming_value = $this->field_value ? 'true' : 'false'; if ($incoming_value != $required_value) { - $this->messageItem - ->type('boolean.valid') + $this->message_error_builder + ->type($this->typeError('valid')) ->message("`$incoming_value` is not validValue required. You must put `$required_value`") ->label($this->field_name); - $this->addError($this->messageItem); + $this->addError($this->message_error_builder); } } } - - /** - * @return string - */ - protected function classProvider(): string - { - return 'boolean'; - } } diff --git a/src/Validator/DateValidator.php b/src/Validator/DateValidator.php index 61e2531..c55df4a 100644 --- a/src/Validator/DateValidator.php +++ b/src/Validator/DateValidator.php @@ -23,11 +23,8 @@ final class DateValidator extends ValidatorProvider */ public function __construct(string $item, array $data_source) { - $this->field_name = $item; - $this->field_value = $data_source[$item]; - $this->data_source = $data_source; + parent::__construct($item, $data_source); $this->checkExist(); - parent::__construct(); } /** @@ -39,12 +36,12 @@ public function now() $min_date = date('d/F/Y', $min_date_time); $date_value_time = strtotime($this->field_value); if ($date_value_time < $min_date_time) { - $this->messageItem - ->type('date.now') + $this->message_error_builder + ->type($this->typeError('now')) ->message("`$this->field_name` should be greater than now") ->label($this->field_name) ->limit($min_date); - $this->addError($this->messageItem); + $this->addError($this->message_error_builder); } } @@ -59,12 +56,12 @@ public function today(string $times = null) $min_date = date('d/F/Y', $min_date_time); $date_value_time = strtotime($this->field_value); if ($date_value_time > $min_date_time) { - $this->messageItem - ->type('date.now') + $this->message_error_builder + ->type($this->typeError('now')) ->message("`$this->field_name` should be greater than today ") ->label($this->field_name) ->limit($min_date); - $this->addError($this->messageItem); + $this->addError($this->message_error_builder); } } @@ -86,12 +83,12 @@ public function min($rule) $min_date = date('d/F/Y', $min_date_time); $date_value_time = strtotime($this->field_value); if ($date_value_time > $min_date_time) { - $this->messageItem - ->type('date.min') + $this->message_error_builder + ->type($this->typeError('min')) ->message("`$this->field_name` should be greater than `$min_date`") ->label($this->field_name) ->limit($min_date); - $this->addError($this->messageItem); + $this->addError($this->message_error_builder); } } @@ -107,12 +104,12 @@ public function max($rule) $max_date = date('d/F/Y', $max_date_time); $date_value_time = strtotime($this->field_value); if ($max_date_time < $date_value_time) { - $this->messageItem - ->type('date.max') + $this->message_error_builder + ->type($this->typeError('max')) ->message("`$this->field_name` should be less than `$max_date`") ->label($this->field_name) ->limit($max_date); - $this->addError($this->messageItem); + $this->addError($this->message_error_builder); } } @@ -122,29 +119,20 @@ public function max($rule) */ protected function checkExist(string $itemKey = null): void { - $item_to_check = $itemKey ? $itemKey : $this->field_name; + $item_to_check = $itemKey ?? $this->field_name; $regex = '#[a-zA-Z0-9]#'; - $this->_errors = []; if (!isset($this->data_source[$item_to_check])) { - $this->messageItem + $this->message_error_builder ->type('any.unknown') ->message("`$item_to_check` is unknown") ->label($item_to_check); - $this->addError($this->messageItem); + $this->addError($this->message_error_builder); } else if (!preg_match($regex, $this->data_source[$item_to_check]) || strlen(trim($this->data_source[$item_to_check])) == 0) { - $this->messageItem - ->type('date.unknown') + $this->message_error_builder + ->type($this->typeError('unknown')) ->message("`$item_to_check` should be a date.") ->label($item_to_check); - $this->addError($this->messageItem); + $this->addError($this->message_error_builder); } } - - /** - * @return string - */ - protected function classProvider(): string - { - return 'date'; - } } \ No newline at end of file diff --git a/src/Validator/NumberValidator.php b/src/Validator/NumberValidator.php index 95f67f3..0e6cbda 100644 --- a/src/Validator/NumberValidator.php +++ b/src/Validator/NumberValidator.php @@ -28,13 +28,8 @@ final class NumberValidator extends ValidatorProvider */ public function __construct(string $item, array $data_source) { - $this->data_source = $data_source; - $this->field_name = $item; - $this->field_value = $data_source[$item]; - if ($this->isNumber()) { - $this->field_value = $data_source[$item]; - } - parent::__construct(); + parent::__construct($item, $data_source); + $this->isNumber(); } /** @@ -43,14 +38,13 @@ public function __construct(string $item, array $data_source) */ public function min(int $rule) { - if ($this->positiveParamMethod($rule)) return; if ((int)$this->field_value < $rule) { - $this->messageItem - ->type('number.min') + $this->message_error_builder + ->type($this->typeError('min')) ->message("`$this->field_name` should be greater than `$rule`") ->label($this->field_name) ->limit($rule); - $this->addError($this->messageItem); + $this->addError($this->message_error_builder); } } @@ -60,14 +54,14 @@ public function min(int $rule) */ public function max(int $rule) { - if ($this->positiveParamMethod($rule, true)) return; + if ($this->checkNotPositiveParamMethod($rule, true)) return; if ((int)$this->field_value > $rule) { - $this->messageItem - ->type('number.max') + $this->message_error_builder + ->type($this->typeError('max')) ->message("`$this->field_name` should be less than `$rule`") ->label($this->field_name) ->limit($rule); - $this->addError($this->messageItem); + $this->addError($this->message_error_builder); } } @@ -77,12 +71,26 @@ public function max(int $rule) public function positive() { if ((int)$this->field_value < 0) { - $this->messageItem - ->type('number.positive') + $this->message_error_builder + ->type($this->typeError('positive')) ->message("`$this->field_name` should be a positive number") ->label($this->field_name) ->limit(1); - $this->addError($this->messageItem); + $this->addError($this->message_error_builder); + } + } + /** + * + */ + public function negative() + { + if ((int)$this->field_value > 0) { + $this->message_error_builder + ->type($this->typeError('negative')) + ->message("`$this->field_name` should be a negative number") + ->label($this->field_name) + ->limit(1); + $this->addError($this->message_error_builder); } } @@ -93,21 +101,13 @@ protected function isNumber(): bool { $regex_string = '#[a-zA-Z]#'; if (preg_match($regex_string, trim($this->data_source[$this->field_name])) || !is_integer($this->data_source[$this->field_name])) { - $this->messageItem - ->type('number.unknown') + $this->message_error_builder + ->type($this->typeError('unknown')) ->message("`$this->field_name` should be a number") ->label($this->field_name); - $this->addError($this->messageItem); + $this->addError($this->message_error_builder); return false; } return true; } - - /** - * @return string - */ - protected function classProvider(): string - { - return 'number'; - } } diff --git a/src/Validator/StringValidator.php b/src/Validator/StringValidator.php index a603817..3b146c4 100644 --- a/src/Validator/StringValidator.php +++ b/src/Validator/StringValidator.php @@ -22,11 +22,7 @@ final class StringValidator extends ValidatorProvider */ public function __construct(string $item, array $data_source) { - $this->errors = []; - $this->data_source = $data_source; - $this->field_name = $item; - $this->field_value = $data_source[$item]; - parent::__construct(); + parent::__construct($item, $data_source); } /** @@ -36,14 +32,14 @@ public function __construct(string $item, array $data_source) */ public function min(int $rule): void { - if ($this->positiveParamMethod($rule)) return; + if ($this->checkNotPositiveParamMethod($rule)) return; if (strlen($this->field_value) < $rule) { - $this->messageItem - ->type('string.min') + $this->message_error_builder + ->type($this->typeError('min')) ->message("`$this->field_name` should have minimum of `$rule` characters") ->label($this->field_name) ->limit($rule); - $this->addError($this->messageItem); + $this->addError($this->message_error_builder); } } @@ -54,14 +50,14 @@ public function min(int $rule): void */ public function max(int $rule): void { - if ($this->positiveParamMethod($rule, true)) return; + if ($this->checkNotPositiveParamMethod($rule, true)) return; if (strlen($this->field_value) > $rule) { - $this->messageItem - ->type('string.max') + $this->message_error_builder + ->type($this->typeError('max')) ->message("`$this->field_name` should have maximum of `$rule` characters") ->label($this->field_name) ->limit($rule); - $this->addError($this->messageItem); + $this->addError($this->message_error_builder); } } @@ -71,11 +67,11 @@ public function max(int $rule): void public function email() { if (!filter_var($this->field_value, FILTER_VALIDATE_EMAIL)) { - $this->messageItem - ->type('string.email') + $this->message_error_builder + ->type($this->typeError('email')) ->message("`$this->field_name` should be an email.") ->label($this->field_name); - $this->addError($this->messageItem); + $this->addError($this->message_error_builder); } } @@ -89,11 +85,11 @@ public function email() public function url() { if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i", $this->field_value)) { - $this->messageItem + $this->message_error_builder ->type('string.url') ->message("'$this->field_name' this should be a link(url)") ->label($this->field_name); - $this->addError($this->messageItem); + $this->addError($this->message_error_builder); } } @@ -105,12 +101,12 @@ public function url() public function match(string $key_to_match) { $this->isStringAndValid($key_to_match); - if (isset($this->data_source[$key_to_match]) && ($this->field_value != $this->data_source[$key_to_match])) { - $this->messageItem + if (isset($this->data_source[$key_to_match]) && (strlen($this->field_value) != strlen($this->data_source[$key_to_match])) && ($this->field_value != $this->data_source[$key_to_match])) { + $this->message_error_builder ->type('string.match') ->message("`$this->field_name` should match `$key_to_match`") ->label($this->field_name); - $this->addError($this->messageItem); + $this->addError($this->message_error_builder); } } @@ -121,11 +117,11 @@ public function match(string $key_to_match) public function addressIp() { if (!filter_var($this->field_value, FILTER_VALIDATE_IP)) { - $this->messageItem + $this->message_error_builder ->type('string.ip_address') ->message("`$this->field_name` is not a valid Ip address") ->label($this->field_name); - $this->addError($this->messageItem); + $this->addError($this->message_error_builder); } } @@ -136,11 +132,11 @@ public function addressIp() public function addressIpv6(string $ip_address) { if (!filter_var($ip_address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { - $this->messageItem + $this->message_error_builder ->type('string.ip_address_v6') ->message("`$this->field_name` is not a valid ip address (ipv6)") ->label($this->field_name); - $this->addError($this->messageItem); + $this->addError($this->message_error_builder); } } @@ -154,22 +150,17 @@ protected function isStringAndValid(string $item_key): void $field_to_check = !$item_key ? $this->field_name : $item_key; $regex = '#[a-zA-Z0-9]#'; if (!isset($this->data_source[$field_to_check])) { - $this->messageItem + $this->message_error_builder ->type('string.unknown') ->message("`$field_to_check` is not valid") ->label($field_to_check); - $this->addError($this->messageItem); + $this->addError($this->message_error_builder); } else if (!preg_match($regex, $this->data_source[$field_to_check]) || strlen(trim($this->field_value)) == 0) { - $this->messageItem + $this->message_error_builder ->type('string.unknown') ->message("`$field_to_check` should be a string") ->label($field_to_check); - $this->addError($this->messageItem); + $this->addError($this->message_error_builder); } } - - protected function classProvider(): string - { - return 'string'; - } }