From 9809884f37804e1d71c116fd7f9ed5ded108d07d Mon Sep 17 00:00:00 2001 From: bim-g Date: Fri, 3 Jun 2022 15:48:05 +0200 Subject: [PATCH 01/26] [ENH]App: setup structure app --- .phpunit.result.cache | 2 +- index.php | 2 +- src/Providers/ValidationProvider.php | 11 ++ src/Script/NumberValidation.php | 124 ++++++++++++++++++++++ src/Script/StringValidation.php | 4 + src/Script/VNumber.php | 151 --------------------------- src/Traits/InitTrait.php | 39 +++++++ 7 files changed, 180 insertions(+), 153 deletions(-) create mode 100644 src/Script/NumberValidation.php delete mode 100644 src/Script/VNumber.php create mode 100644 src/Traits/InitTrait.php diff --git a/.phpunit.result.cache b/.phpunit.result.cache index edc4bf1..6f2affb 100644 --- a/.phpunit.result.cache +++ b/.phpunit.result.cache @@ -1 +1 @@ -{"version":1,"defects":{"Test\\Schema\\SCHStringTest::testStringObject":4,"Test\\Schema\\SCHStringTest::testStringObjectIsNotKey":3,"Test\\Schema\\SCHStringTest::testStringEmailKey":3,"Test\\Schema\\SCHStringTest::testStringObjectIsKey":4,"Test\\Schema\\StringSchemaTest::testStringURLKey":4,"Test\\Schema\\StringSchemaTest::testStringMatchKey":4,"Test\\Schema\\StringSchemaTest::testStringMinimumKey":4,"Test\\Schema\\NumberSchemaTest::testStringIsObject":4,"Test\\Schema\\NumberSchemaTest::testRequiredKey":4,"Test\\Schema\\DateSchemaTest::testDateTodayKey":4,"Test\\Schema\\BooleanSchemaTest::testBooleanIsRequireddKey":4,"Test\\Script\\StringValidationTest::testStringIsObject":4,"Test\\Script\\StringValidationTest::testStringObjectException":4,"Test\\Script\\StringValidationTest::testException":3,"Test\\Script\\StringValidationTest::testStringMin":3,"Test\\Script\\StringValidationTest::testStringMaxError":4,"Test\\Script\\StringValidationTest::testStringMaxSuccess":3,"Test\\Script\\StringValidationTest::testStringValidationSourceDataException":4,"Test\\Script\\StringValidationTest::testStringValidationSchemaException":4,"Test\\Script\\StringValidationTest::testStringMinError":4,"Test\\Script\\StringValidationTest::testStringMinSuccess":3,"Test\\Script\\StringValidationTest::testStringEmailError":3,"Test\\Script\\StringValidationTest::testStringURLSuccess":4,"Test\\Script\\StringValidationTest::testStringRequiredSuccess":4,"Test\\Script\\StringValidationTest::testStringRequiredErrors":3,"Test\\Script\\StringValidationTest::testStringRequiredErrorsUnknown":4,"Test\\Script\\StringValidationTest::testStringMatchSuccess":3,"Test\\Script\\StringValidationTest::testStringMatchErrors":3,"Test\\Schema\\StringSchemaTest::testStringIsObject":4,"Test\\Schema\\StringSchemaTest::testStringObjectIsKey":4,"Test\\Schema\\StringSchemaTest::testStringEmailKey":4,"Test\\Schema\\StringSchemaTest::testStringMaximumKey":4,"Test\\Schema\\DateSchemaTest::testStringIsObject":4,"Test\\Schema\\DateSchemaTest::testStringObjectIsKey":4,"Test\\Schema\\DateSchemaTest::testDateNowKey":4,"Test\\Schema\\NumberSchemaTest::testStringObjectIsKey":4,"Test\\Schema\\NumberSchemaTest::testNumberPositiveKey":4,"Test\\Schema\\BooleanSchemaTest::testBooleanIsObject":4,"Test\\Schema\\BooleanSchemaTest::testStringObjectIsKey":4,"Test\\Schema\\BooleanSchemaTest::testBooleanIsValidKey":3,"Test\\ValidationTest::testValidationStringObject":3,"Test\\ValidationTest::testValidationStringErrorObject":3},"times":{"Test\\Schema\\SCHStringTest::testStringObject":0.021,"Test\\Schema\\SCHStringTest::testStringObjectIsKey":0,"Test\\Schema\\SCHStringTest::testStringObjectIsNotKey":0.01,"Test\\Schema\\SCHStringTest::testStringEmailKey":0.001,"Test\\Schema\\SCHStringTest::testStringIsObject":0.008,"Test\\Schema\\StringSchemaTest::testStringIsObject":0.005,"Test\\Schema\\StringSchemaTest::testStringObjectIsKey":0,"Test\\Schema\\StringSchemaTest::testStringEmailKey":0,"Test\\Schema\\StringSchemaTest::testStringURLKey":0,"Test\\Schema\\StringSchemaTest::testStringMatchKey":0,"Test\\Schema\\StringSchemaTest::testStringMinimumKey":0,"Test\\Schema\\StringSchemaTest::testStringMaximumKey":0,"Test\\Schema\\NumberSchemaTest::testStringIsObject":0.005,"Test\\Schema\\NumberSchemaTest::testStringObjectIsKey":0,"Test\\Schema\\NumberSchemaTest::testRequiredKey":0,"Test\\Schema\\NumberSchemaTest::testNumberPositiveKey":0,"Test\\Schema\\DateSchemaTest::testStringIsObject":0.006,"Test\\Schema\\DateSchemaTest::testStringObjectIsKey":0,"Test\\Schema\\DateSchemaTest::testDateNowKey":0,"Test\\Schema\\DateSchemaTest::testDateTodayKey":0,"Test\\Schema\\BooleanSchemaTest::testBooleanIsObject":0.005,"Test\\Schema\\BooleanSchemaTest::testStringObjectIsKey":0,"Test\\Schema\\BooleanSchemaTest::testBooleanIsRequireddKey":0,"Test\\Schema\\BooleanSchemaTest::testBooleanIsValidKey":0,"Test\\Script\\StringValidationTest::testStringIsObject":0,"Test\\Script\\StringValidationTest::testStringObjectException":0.006,"Test\\Script\\StringValidationTest::testException":0.007,"Test\\Script\\StringValidationTest::testStringMin":0.001,"Test\\Script\\StringValidationTest::testStringValidationException":0.006,"Test\\Script\\StringValidationTest::testStringValidationSchemaException":0,"Test\\Script\\StringValidationTest::testStringValidationSourceDataException":0.007,"Test\\Script\\StringValidationTest::testStringMinError":0,"Test\\Script\\StringValidationTest::testStringMinSuccess":0,"Test\\Script\\StringValidationTest::testStringMaxError":0,"Test\\Script\\StringValidationTest::testStringMaxSuccess":0,"Test\\Script\\StringValidationTest::testStringEmailError":0.001,"Test\\Script\\StringValidationTest::testStringEmailSuccess":0,"Test\\Script\\StringValidationTest::testStringUrlError":0.008,"Test\\Script\\StringValidationTest::testStringURLError":0,"Test\\Script\\StringValidationTest::testStringURLSuccess":0.008,"Test\\Script\\StringValidationTest::testStringRequiredSuccess":0.001,"Test\\Script\\StringValidationTest::testStringRequiredErrors":0,"Test\\Script\\StringValidationTest::testStringRequiredErrorsUnknow":0,"Test\\Script\\StringValidationTest::testStringRequiredErrorsUnknown":0,"Test\\Script\\StringValidationTest::testStringMatchErrors":0,"Test\\Script\\StringValidationTest::testStringMatchSuccess":0,"Test\\Script\\StringValidationTest::testStringValidationFieldNotDefinedException":0,"Test\\ValidationTest::testValidationObject":0.016,"Test\\ValidationTest::testValidationStringObject":0.005,"Test\\ValidationTest::testValidationStringErrorObject":0.009}} \ No newline at end of file +{"version":1,"defects":{"Test\\Schema\\SCHStringTest::testStringObject":4,"Test\\Schema\\SCHStringTest::testStringObjectIsNotKey":3,"Test\\Schema\\SCHStringTest::testStringEmailKey":3,"Test\\Schema\\SCHStringTest::testStringObjectIsKey":4,"Test\\Schema\\StringSchemaTest::testStringURLKey":4,"Test\\Schema\\StringSchemaTest::testStringMatchKey":4,"Test\\Schema\\StringSchemaTest::testStringMinimumKey":4,"Test\\Schema\\NumberSchemaTest::testStringIsObject":4,"Test\\Schema\\NumberSchemaTest::testRequiredKey":4,"Test\\Schema\\DateSchemaTest::testDateTodayKey":4,"Test\\Schema\\BooleanSchemaTest::testBooleanIsRequireddKey":4,"Test\\Script\\StringValidationTest::testStringIsObject":4,"Test\\Script\\StringValidationTest::testStringObjectException":4,"Test\\Script\\StringValidationTest::testException":3,"Test\\Script\\StringValidationTest::testStringMin":3,"Test\\Script\\StringValidationTest::testStringMaxError":4,"Test\\Script\\StringValidationTest::testStringMaxSuccess":3,"Test\\Script\\StringValidationTest::testStringValidationSourceDataException":4,"Test\\Script\\StringValidationTest::testStringValidationSchemaException":4,"Test\\Script\\StringValidationTest::testStringMinError":4,"Test\\Script\\StringValidationTest::testStringMinSuccess":3,"Test\\Script\\StringValidationTest::testStringEmailError":3,"Test\\Script\\StringValidationTest::testStringURLSuccess":4,"Test\\Script\\StringValidationTest::testStringRequiredSuccess":4,"Test\\Script\\StringValidationTest::testStringRequiredErrors":3,"Test\\Script\\StringValidationTest::testStringRequiredErrorsUnknown":4,"Test\\Script\\StringValidationTest::testStringMatchSuccess":3,"Test\\Script\\StringValidationTest::testStringMatchErrors":3,"Test\\Schema\\StringSchemaTest::testStringIsObject":4,"Test\\Schema\\StringSchemaTest::testStringObjectIsKey":4,"Test\\Schema\\StringSchemaTest::testStringEmailKey":4,"Test\\Schema\\StringSchemaTest::testStringMaximumKey":4,"Test\\Schema\\DateSchemaTest::testStringIsObject":4,"Test\\Schema\\DateSchemaTest::testStringObjectIsKey":4,"Test\\Schema\\DateSchemaTest::testDateNowKey":4,"Test\\Schema\\NumberSchemaTest::testStringObjectIsKey":4,"Test\\Schema\\NumberSchemaTest::testNumberPositiveKey":4,"Test\\Schema\\BooleanSchemaTest::testBooleanIsObject":4,"Test\\Schema\\BooleanSchemaTest::testStringObjectIsKey":4,"Test\\Schema\\BooleanSchemaTest::testBooleanIsValidKey":3,"Test\\ValidationTest::testValidationStringObject":3,"Test\\ValidationTest::testValidationStringErrorObject":3},"times":{"Test\\Schema\\SCHStringTest::testStringObject":0.021,"Test\\Schema\\SCHStringTest::testStringObjectIsKey":0,"Test\\Schema\\SCHStringTest::testStringObjectIsNotKey":0.01,"Test\\Schema\\SCHStringTest::testStringEmailKey":0.001,"Test\\Schema\\SCHStringTest::testStringIsObject":0.008,"Test\\Schema\\StringSchemaTest::testStringIsObject":0.005,"Test\\Schema\\StringSchemaTest::testStringObjectIsKey":0,"Test\\Schema\\StringSchemaTest::testStringEmailKey":0,"Test\\Schema\\StringSchemaTest::testStringURLKey":0,"Test\\Schema\\StringSchemaTest::testStringMatchKey":0,"Test\\Schema\\StringSchemaTest::testStringMinimumKey":0,"Test\\Schema\\StringSchemaTest::testStringMaximumKey":0,"Test\\Schema\\NumberSchemaTest::testStringIsObject":0.005,"Test\\Schema\\NumberSchemaTest::testStringObjectIsKey":0,"Test\\Schema\\NumberSchemaTest::testRequiredKey":0,"Test\\Schema\\NumberSchemaTest::testNumberPositiveKey":0,"Test\\Schema\\DateSchemaTest::testStringIsObject":0.006,"Test\\Schema\\DateSchemaTest::testStringObjectIsKey":0,"Test\\Schema\\DateSchemaTest::testDateNowKey":0,"Test\\Schema\\DateSchemaTest::testDateTodayKey":0,"Test\\Schema\\BooleanSchemaTest::testBooleanIsObject":0.005,"Test\\Schema\\BooleanSchemaTest::testStringObjectIsKey":0,"Test\\Schema\\BooleanSchemaTest::testBooleanIsRequireddKey":0,"Test\\Schema\\BooleanSchemaTest::testBooleanIsValidKey":0,"Test\\Script\\StringValidationTest::testStringIsObject":0.001,"Test\\Script\\StringValidationTest::testStringObjectException":0.006,"Test\\Script\\StringValidationTest::testException":0.007,"Test\\Script\\StringValidationTest::testStringMin":0.001,"Test\\Script\\StringValidationTest::testStringValidationException":0.006,"Test\\Script\\StringValidationTest::testStringValidationSchemaException":0,"Test\\Script\\StringValidationTest::testStringValidationSourceDataException":0.007,"Test\\Script\\StringValidationTest::testStringMinError":0.001,"Test\\Script\\StringValidationTest::testStringMinSuccess":0,"Test\\Script\\StringValidationTest::testStringMaxError":0,"Test\\Script\\StringValidationTest::testStringMaxSuccess":0,"Test\\Script\\StringValidationTest::testStringEmailError":0.001,"Test\\Script\\StringValidationTest::testStringEmailSuccess":0,"Test\\Script\\StringValidationTest::testStringUrlError":0.008,"Test\\Script\\StringValidationTest::testStringURLError":0,"Test\\Script\\StringValidationTest::testStringURLSuccess":0,"Test\\Script\\StringValidationTest::testStringRequiredSuccess":0,"Test\\Script\\StringValidationTest::testStringRequiredErrors":0,"Test\\Script\\StringValidationTest::testStringRequiredErrorsUnknow":0,"Test\\Script\\StringValidationTest::testStringRequiredErrorsUnknown":0,"Test\\Script\\StringValidationTest::testStringMatchErrors":0,"Test\\Script\\StringValidationTest::testStringMatchSuccess":0,"Test\\Script\\StringValidationTest::testStringValidationFieldNotDefinedException":0,"Test\\ValidationTest::testValidationObject":0.016,"Test\\ValidationTest::testValidationStringObject":0.005,"Test\\ValidationTest::testValidationStringErrorObject":0.262}} \ No newline at end of file diff --git a/index.php b/index.php index 4ae3eb2..b718ee1 100644 --- a/index.php +++ b/index.php @@ -2,4 +2,4 @@ require_once "./vendor/autoload.php"; include('./shared/global.php'); -include('./test/index.php'); \ No newline at end of file +//include('./test/index.php'); \ No newline at end of file diff --git a/src/Providers/ValidationProvider.php b/src/Providers/ValidationProvider.php index b92b090..2bf7be3 100644 --- a/src/Providers/ValidationProvider.php +++ b/src/Providers/ValidationProvider.php @@ -21,6 +21,17 @@ function addError(array $value): void $this->errors[] = $value; } + protected function isRequired(string $source="any",string $fields){ + $required_value = trim($fields); + if (strlen($required_value) == 0) { + $message = [ + 'type' => $source.".required", + 'message' => "`$fields` is required", + 'label' => $fields, + ]; + $this->addError($message); + } + } /** * @return array */ diff --git a/src/Script/NumberValidation.php b/src/Script/NumberValidation.php new file mode 100644 index 0000000..d78a3c0 --- /dev/null +++ b/src/Script/NumberValidation.php @@ -0,0 +1,124 @@ +source_data=$source; + $this->field_item=$string_item; + $this->_max= $this->_min=0; + if($this->isNumber()){ + $this->number_value=$source[$string_item]; + } + } + + /** + * @param $rule + * @return $this + */ + function min($rule): NumberValidation + { + if ((int) $this->number_value < $rule) { + $message=[ + "type"=>"number.min", + "message"=> "`$this->field_item` should be greater than `$rule`", + "label"=>$this->field_item, + "limit"=>$rule + ]; + $this->addError($message); + } + return $this; + } + + /** + * @param $rule + * @return $this + */ + function max($rule): NumberValidation + { + if ((int) $this->number_value > $rule) { + $message=[ + "type"=>"number.max", + "message"=> "`$this->field_item` should be less than `$rule`", + "label"=>$this->field_item, + "limit"=>$rule + ]; + $this->addError($message); + } + return $this; + } + + /** + * @param int $min_values + * @return $this + */ + function positive(): NumberValidation + { + if ((int) $this->number_value < 0) { + $message=[ + "type"=>"number.positive", + "message"=> "`$this->field_item` should be a positive number", + "label"=>$this->field_item, + "limit"=>1 + ]; + $this->addError($message); + } + return $this; + } + + /** + * @return $this + */ +// function required() +// { +// $this->isRequired("number",$this->field_item); +// return $this; +// } + + /** + * @param string|null $itemKey + * @return bool + */ + private function isNumber(): bool + { + $regex_string="#[a-zA-Z]#"; + if (preg_match($regex_string,trim($this->source_data[$this->field_item])) || !is_integer($this->source_data[$this->field_item])) { + $message = [ + "type"=> "number.unknown", + "message" => "`$this->field_item` should be a number", + "label" => $this->field_item, + ]; + $this->addError($message); + return false; + } + return true; + } + /** + * @return array + */ + function check(): array + { + return $this->_errors; + } +} diff --git a/src/Script/StringValidation.php b/src/Script/StringValidation.php index 9ad8dc3..0a8f2df 100644 --- a/src/Script/StringValidation.php +++ b/src/Script/StringValidation.php @@ -23,6 +23,10 @@ class StringValidation extends ValidationProvider { */ function __construct(array $source,array $schema) { $this->initInstance($source,$schema); + $fields=array_keys($schema); + $this->field_item = $fields[0]; + // + $this->source_data = $source; } /** diff --git a/src/Script/VNumber.php b/src/Script/VNumber.php deleted file mode 100644 index 2d8091f..0000000 --- a/src/Script/VNumber.php +++ /dev/null @@ -1,151 +0,0 @@ -source_data=$source; -// $this->string_item=$string_item; -// $this->_max= $this->_min=0; -// $this->i18n= new i18n($lang); -// if($this->checkExist()){ -// $this->string_value=$source[$string_item]; -// } -// } -// -// /** -// * @param int $min_values -// * @return $this -// */ -// function min(int $min_values): SCHNumber -// { -// if ((int) $this->string_value < $min_values) { -// $message=[ -// "type"=>"number.min", -// "message"=> $this->i18n->translate("`%s` should be greater than `%s`",[$this->string_item,$min_values]), -// "label"=>$this->string_item, -// "limit"=>$min_values -// ]; -// $this->addError($message); -// } -// return $this; -// } -// -// /** -// * @param int $min_values -// * @return $this -// */ -// function max(int $min_values): SCHNumber -// { -// if ((int) $this->string_value > $min_values) { -// $message=[ -// "type"=>"number.max", -// "message"=> $this->i18n->translate("`%s` should be less than `%s`",[$this->string_item,$min_values]), -// "label"=>$this->string_item, -// "limit"=>$min_values -// ]; -// $this->addError($message); -// } -// return $this; -// } -// -// /** -// * @param int $min_values -// * @return $this -// */ -// function positive(int $min_values): SCHNumber -// { -// if ((int) $this->string_value < 0) { -// $message=[ -// "type"=>"number.positive", -// "message"=> $this->i18n->translate("`%s` should be a positive number",[$this->string_item]), -// "label"=>$this->string_item, -// "limit"=>1 -// ]; -// $this->addError($message); -// } -// return $this; -// } -// -// /** -// * @return $this -// */ -// function required(): SCHNumber -// { -// $required_value= trim($this->string_value); -// if (empty($required_value)) { -// $message = [ -// "type"=> "any.required", -// "message" => $this->i18n->translate("`%s` is required",[$this->string_item]), -// "label" => $this->string_item, -// ]; -// $this->addError($message); -// } -// return $this; -// } -// -// /** -// * @param string|null $itemKey -// * @return bool -// */ -// private function checkExist(string $itemKey=null): bool -// { -// $item_to_check=!$itemKey?$this->string_item:$itemKey; -// $regex_string="#[a-zA-Z]#"; -// $status_key_exist=true; -// if (!isset($this->source_data[$item_to_check])) { -// $message = [ -// "type"=> "any.unknown", -// "message" => $this->i18n->translate("`%s` is unknown",[$item_to_check]), -// "label" => $item_to_check, -// ]; -// $this->addError($message); -// $status_key_exist=false; -// }else if (preg_match($regex_string,trim($this->source_data[$item_to_check])) || !is_integer($this->source_data[$item_to_check])) { -// $message = [ -// "type"=> "number.unknown", -// "message" => $this->i18n->translate("`%s` should be a number",[$item_to_check]), -// "label" => $item_to_check, -// ]; -// $this->addError($message); -// $status_key_exist=false; -// } -// return $status_key_exist; -// } -// -// /** -// * @param array $value -// */ -// private function addError(array $value): void -// { -// $this->_errors[] = $value; -// } -// -// /** -// * @return array -// */ -// function check(): array -// { -// return $this->_errors; -// } -//} diff --git a/src/Traits/InitTrait.php b/src/Traits/InitTrait.php new file mode 100644 index 0000000..1d9b3b0 --- /dev/null +++ b/src/Traits/InitTrait.php @@ -0,0 +1,39 @@ +extract_data($schema); + } + + /** + * @param array $schema + */ + protected function extract_data(array $schema ){ + $conditions=$schema[$this->field_item]['String']; + foreach ($conditions as $key=>$value){ + call_user_func([$this,$key],$value); + return; + } + } +} \ No newline at end of file From 9e6569c204f27135bf9df3929a10d0ea6516f997 Mon Sep 17 00:00:00 2001 From: bim-g Date: Wed, 7 Dec 2022 16:43:26 +0200 Subject: [PATCH 02/26] [UPD] refactoring schema contract and provider. --- src/Providers/Contracts/Contracts.php | 9 ++++++--- src/Providers/Contracts/SchemaContracts.php | 7 +++++-- src/Providers/SChemaProvider.php | 12 ++++++++++-- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/Providers/Contracts/Contracts.php b/src/Providers/Contracts/Contracts.php index 1ceed04..a1677cd 100644 --- a/src/Providers/Contracts/Contracts.php +++ b/src/Providers/Contracts/Contracts.php @@ -1,11 +1,14 @@ schema[$this->source]["min"]=$rule; return $this; @@ -44,7 +52,7 @@ function required(): SChemaProvider /** * @return array */ - function check(): array + function generate(): array { return $this->schema; } From 72420457d4d8b84308d600c138131d8204308847 Mon Sep 17 00:00:00 2001 From: bim-g Date: Wed, 7 Dec 2022 17:56:01 +0200 Subject: [PATCH 03/26] [UPD] refactoring string schema. --- example/string.php | 19 +++ src/Schema/StringSchema.php | 9 +- .../StringValidator.php} | 110 ++++++------------ 3 files changed, 64 insertions(+), 74 deletions(-) create mode 100644 example/string.php rename src/{Script/StringValidation.php => Validator/StringValidator.php} (55%) diff --git a/example/string.php b/example/string.php new file mode 100644 index 0000000..e96e196 --- /dev/null +++ b/example/string.php @@ -0,0 +1,19 @@ + $schema->string()->email()->min(35)->max(50)->required()->generate(), + "country" => $schema->string()->min(3)->max(40)->generate(), + "password" => $schema->string()->min(3)->max(40)->generate(), + "n_password" => $schema->string()->min(3)->max(40)->match("password")->generate(), +]; + var_dump($rules); +//$valid->check($source, $rules); +//// check if the validation passed or not +//var_dump($valid->passed()); +//var_dump($valid->errors()); \ No newline at end of file diff --git a/src/Schema/StringSchema.php b/src/Schema/StringSchema.php index c0a6eed..981824c 100644 --- a/src/Schema/StringSchema.php +++ b/src/Schema/StringSchema.php @@ -1,4 +1,9 @@ schema[$this->source]["match"]=$key_to_match; return $this; } -} +} \ No newline at end of file diff --git a/src/Script/StringValidation.php b/src/Validator/StringValidator.php similarity index 55% rename from src/Script/StringValidation.php rename to src/Validator/StringValidator.php index 0a8f2df..a9c2b0e 100644 --- a/src/Script/StringValidation.php +++ b/src/Validator/StringValidator.php @@ -1,73 +1,39 @@ initInstance($source,$schema); - $fields=array_keys($schema); - $this->field_item = $fields[0]; - // - $this->source_data = $source; - } - - /** - * @param array $schema + * @param string $item + * @param string $value */ - private function extract_data(array $schema ){ - $conditions=$schema[$this->field_item]['String']; - foreach ($conditions as $key=>$value){ - call_user_func([$this,$key],$value); - return; - } - } - - /** - * @throws Exception - */ - private function initInstance($source, $schema){ - $this->errors=null; - $this->_min=0; - $this->_max=1; - $this->source_data=[]; - $this->field_value=""; - // - if(!is_array($source) || count($source)==0){ - throw new Exception("Your Source Data should not be en empty array"); - } - if(!is_array($schema) || count($schema)==0){ - throw new Exception("Your Schema should not be en empty array"); - } - $fields=array_keys($schema); - $this->field_item=$fields[0]; - // - $this->source_data=$source; - if(!isset($source[$fields[0]])){ - throw new \Exception("field not defined"); - } - $this->field_value=$source[$fields[0]]; - if($this->isString($fields[0])){ - $this->string_value=$this->field_value; - }; - $this->extract_data($schema); + public function __construct(string $item,string $value) { + $this->errors = []; + $this->_min = strlen($value); + $this->_max = strlen($value); + $this->source_data = []; + $this->field_item = $item; + $this->field_value = $value; + parent::__construct(); } /** @@ -75,7 +41,7 @@ private function initInstance($source, $schema){ * @param int $rule * */ - function min($rule):void + public function min(int $rule):void { if (strlen($this->field_value) < $rule) { $message=[ @@ -93,10 +59,10 @@ function min($rule):void * @param int $rule * */ - function max($rule) + public function max(int $rule) { $this->_max=$rule; - if (strlen($this->string_value) > $rule) { + if (strlen($this->field_value) > $rule) { $message = [ "type" => "string.max", "message" => "`$this->field_item` should have maximum of `$rule` characters", @@ -107,10 +73,12 @@ function max($rule) } } - - function email() + /** + * + */ + public function email() { - if (!filter_var($this->string_value, FILTER_VALIDATE_EMAIL)) { + if (!filter_var($this->field_value, FILTER_VALIDATE_EMAIL)) { $message = [ "type" => "string.email", "message" => ("`$this->field_item` should be an email."), @@ -127,9 +95,9 @@ function email() * www.[domain].[extension] * */ - function url() + public function url() { - if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i", $this->string_value)) { + if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i", $this->field_value)) { $message = [ "type" => "string.url", "message" => ("`{$this->field_item}` this should be a link(url)"), @@ -144,10 +112,10 @@ function url() * @param string $key_to_match * */ - function match(string $key_to_match) + public function match(string $key_to_match) { $this->isString($key_to_match); - if (isset($this->source_data[$key_to_match]) && (strlen($this->string_value)!= strlen($this->source_data[$key_to_match])) && ($this->string_value!=$this->source_data[$key_to_match])) { + if (isset($this->source_data[$key_to_match]) && (strlen($this->field_value)!= strlen($this->source_data[$key_to_match])) && ($this->field_value!=$this->source_data[$key_to_match])) { $message = [ "type" => "string.match", "message" => "`$this->field_item` should match `$key_to_match`", @@ -157,7 +125,7 @@ function match(string $key_to_match) } } - function required() + public function required() { $required_value= trim($this->field_value); if (strlen($required_value)==0) { @@ -173,9 +141,9 @@ function required() /** * * @param string $item_key - * @return boolean + * @return void */ - private function isString(string $item_key): bool + protected function isString(string $item_key): void { $field_to_check=!$item_key?$this->field_item:$item_key; $regex="#[a-zA-Z0-9]#"; @@ -185,9 +153,7 @@ private function isString(string $item_key): bool "message" => ("`$field_to_check` should be a string"), "label" => $field_to_check, ]; - $this->addError($message); - return false; + $this->addError($message); } - return true; } } From 4603394e42570b1ec5ee7120157a85e93e3ce9c0 Mon Sep 17 00:00:00 2001 From: bim-g Date: Thu, 8 Dec 2022 21:15:45 +0200 Subject: [PATCH 04/26] [ENH] Add option resolver to app manage undefined key value. --- src/Resolver/Option.php | 102 ++++++++++++++++++++++++++++ src/Resolver/OptionsResolver.php | 110 +++++++++++++++++++++++++++++++ 2 files changed, 212 insertions(+) create mode 100644 src/Resolver/Option.php create mode 100644 src/Resolver/OptionsResolver.php diff --git a/src/Resolver/Option.php b/src/Resolver/Option.php new file mode 100644 index 0000000..e94f567 --- /dev/null +++ b/src/Resolver/Option.php @@ -0,0 +1,102 @@ +hasDefaultValue = false; + $this->name = $name; + } + + /** + * @return string + */ + public function getName(): string + { + return $this->name; + } + + /** + * @return mixed + */ + public function getDefaultValue() + { + return $this->defaultValue; + } + + /** + * @param mixed $defaultValue + * @return Option + */ + public function setDefaultValue($defaultValue): self + { + $this->hasDefaultValue = true; + $this->defaultValue = $defaultValue; + return $this; + } + + /** + * @return bool + */ + public function hasDefaultValue(): bool + { + return $this->hasDefaultValue; + } + + /** + * @param \Closure $closure + * @return $this + */ + public function validator(\Closure $closure): self + { + $this->validator = $closure; + return $this; + } + + /** + * @param $value + * @return bool + */ + public function isValid($value): bool + { + if ($this->validator instanceof \Closure) { + $validator = $this->validator; + return $validator($value); + } + return true; + } +} \ No newline at end of file diff --git a/src/Resolver/OptionsResolver.php b/src/Resolver/OptionsResolver.php new file mode 100644 index 0000000..110bd19 --- /dev/null +++ b/src/Resolver/OptionsResolver.php @@ -0,0 +1,110 @@ +options = new \ArrayObject(); + foreach ($options as $option) { + $this->add($option); + } + } + + public function resolve(array $options): array + { + try { + $checkDiff = $this->checkDiff($options); + if(isset($checkDiff['exception'])){ + return $checkDiff; + } + /** + * @var Option $option + */ + $optionsResolved = []; + foreach ($this->options as $option) { + $optionName = $option->getName(); + if (\array_key_exists($optionName, $options)) { + $value = $options[$optionName]; + if ($option->isValid($value) === false) { + throw new \InvalidArgumentException(sprintf('The option "%s" with value %s is invalid.', $optionName, self::formatValue($value))); + } + $optionsResolved[$optionName] = $value; + continue; + } + + if ($option->hasDefaultValue()) { + $optionsResolved[$optionName] = $option->getDefaultValue(); + continue; + } + + throw new \InvalidArgumentException(sprintf('The required option "%s" is missing.', $optionName)); + } + return $optionsResolved; + } catch (\Exception $ex) { + return $this->exception($ex); + } + } + + /** + * @param Option $option + * @return void + */ + private function add(Option $option): void + { + $this->options->offsetSet($option->getName(), $option); + } + + private function checkDiff(array $options): array + { + try { + $defined = $this->options->getArrayCopy(); + $diff = array_diff_key($options, $defined); + if (count($diff) > 0) { + throw new \InvalidArgumentException(\sprintf( + 'The option(s) "%s" do(es) not exist. Defined options are: "%s".', + implode(', ', array_keys($diff)), + implode('", "', array_keys($defined))) + ); + } + return []; + } catch (\Exception $ex) { + return $this->exception($ex); + } + } + + private static function formatValue($value): string + { + if (is_object($value)) { + return \get_class($value); + } + + if (is_string($value)) { + return '"' . $value . '"'; + } + + if (false === $value) { + return 'false'; + } + + if (true === $value) { + return 'true'; + } + + return \gettype($value); + } +} \ No newline at end of file From 61e57bb13305780c084c88b9ea267e8ed3c5a9ae Mon Sep 17 00:00:00 2001 From: bim-g Date: Thu, 8 Dec 2022 21:16:44 +0200 Subject: [PATCH 05/26] [ENH] Exception as trait to be available everywhere. --- src/Traits/ExceptionTraits.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 src/Traits/ExceptionTraits.php diff --git a/src/Traits/ExceptionTraits.php b/src/Traits/ExceptionTraits.php new file mode 100644 index 0000000..0978774 --- /dev/null +++ b/src/Traits/ExceptionTraits.php @@ -0,0 +1,17 @@ + $ex->getMessage()]; + } +} \ No newline at end of file From f688c6818d642139ed109a3a04849ccb8eb1ab4f Mon Sep 17 00:00:00 2001 From: bim-g Date: Thu, 8 Dec 2022 22:18:29 +0200 Subject: [PATCH 06/26] [ENH] Implement string validation with and example. --- src/Providers/SChemaProvider.php | 10 ++-- src/Providers/ValidationProvider.php | 42 ---------------- src/Providers/ValidatorProvider.php | 52 +++++++++++++++++++ src/Schema/StringSchema.php | 2 +- src/Validator/StringValidator.php | 75 +++++++++++----------------- 5 files changed, 86 insertions(+), 95 deletions(-) delete mode 100644 src/Providers/ValidationProvider.php create mode 100644 src/Providers/ValidatorProvider.php diff --git a/src/Providers/SChemaProvider.php b/src/Providers/SChemaProvider.php index 0821e7d..2dcc985 100644 --- a/src/Providers/SChemaProvider.php +++ b/src/Providers/SChemaProvider.php @@ -8,13 +8,9 @@ use Wepesi\App\Providers\Contracts\SchemaContracts; -/** - * Class SChemaProvider - * @package Wepesi\App\Providers - */ abstract class SChemaProvider implements SchemaContracts { - protected array $schema=[]; + protected array $schema = []; protected string $source; function __construct(string $type) @@ -22,10 +18,10 @@ function __construct(string $type) $this->source = $type; $this->schema[$this->source] = []; } + /** - * @param $rule + * @param int $rule * @return SChemaProvider - * */ function min(int $rule): SChemaProvider { diff --git a/src/Providers/ValidationProvider.php b/src/Providers/ValidationProvider.php deleted file mode 100644 index 2bf7be3..0000000 --- a/src/Providers/ValidationProvider.php +++ /dev/null @@ -1,42 +0,0 @@ -errors[] = $value; - } - - protected function isRequired(string $source="any",string $fields){ - $required_value = trim($fields); - if (strlen($required_value) == 0) { - $message = [ - 'type' => $source.".required", - 'message' => "`$fields` is required", - 'label' => $fields, - ]; - $this->addError($message); - } - } - /** - * @return array - */ - function result(): ?array - { - return $this->errors; - } -} \ No newline at end of file diff --git a/src/Providers/ValidatorProvider.php b/src/Providers/ValidatorProvider.php new file mode 100644 index 0000000..bfbe6e8 --- /dev/null +++ b/src/Providers/ValidatorProvider.php @@ -0,0 +1,52 @@ +errors = []; + } + abstract function min(int $rule); + abstract function max(int $rule); + public function required(){ + $required_value = trim($this->field_value); + if (strlen($required_value) == 0) { + $message = [ + 'type' => $this->class_provider . '.required', + 'message' => "'$this->field_name' is required", + 'label' => $this->field_name, + ]; + $this->addError($message); + } + } + /** + * + * @param array $value + * @return void + */ + function addError(array $value): void + { + $this->errors[] = $value; + } + + /** + * @return array + */ + function result(): array + { + return $this->errors; + } +} \ No newline at end of file diff --git a/src/Schema/StringSchema.php b/src/Schema/StringSchema.php index 981824c..b6bcd8d 100644 --- a/src/Schema/StringSchema.php +++ b/src/Schema/StringSchema.php @@ -22,7 +22,7 @@ abstract class StringSchema extends SChemaProvider { function __construct() { - parent::__construct('String'); + parent::__construct('StringValidator'); } function email(): StringSchema diff --git a/src/Validator/StringValidator.php b/src/Validator/StringValidator.php index a9c2b0e..deb6070 100644 --- a/src/Validator/StringValidator.php +++ b/src/Validator/StringValidator.php @@ -6,7 +6,6 @@ namespace Wepesi\App\Validator; -use Exception; use Wepesi\App\Providers\ValidatorProvider; /** @@ -14,25 +13,19 @@ * * @author Boss Ibrahim Mussa */ -abstract class StringValidator extends ValidatorProvider { - private string $field_value, $field_item; - private array $source_data; - public array $errors; - private int $_min; - private int $_max; - +final class StringValidator extends ValidatorProvider { /** * - * @param string $item - * @param string $value + * @param string $item the item to be validated. + * @param string $value the value of the item + * @param array $data_source the source data from where is going to check it the match key exist and have value. */ - public function __construct(string $item,string $value) { + public function __construct(string $item, string $value, array $data_source=[]) { $this->errors = []; - $this->_min = strlen($value); - $this->_max = strlen($value); - $this->source_data = []; - $this->field_item = $item; + $this->data_source = $data_source; + $this->field_name = $item; $this->field_value = $value; + $this->class_provider = "string"; parent::__construct(); } @@ -46,8 +39,8 @@ public function min(int $rule):void if (strlen($this->field_value) < $rule) { $message=[ "type"=>"string.min", - "message"=> "`$this->field_item` should have minimum of `$rule` characters", - "label"=>$this->field_item, + "message"=> "`$this->field_name` should have minimum of `$rule` characters", + "label"=>$this->field_name, "limit"=>$rule ]; $this->addError($message); @@ -61,12 +54,11 @@ public function min(int $rule):void */ public function max(int $rule) { - $this->_max=$rule; if (strlen($this->field_value) > $rule) { $message = [ "type" => "string.max", - "message" => "`$this->field_item` should have maximum of `$rule` characters", - "label" => $this->field_item, + "message" => "`$this->field_name` should have maximum of `$rule` characters", + "label" => $this->field_name, "limit" => $rule ]; $this->addError($message); @@ -81,8 +73,8 @@ public function email() if (!filter_var($this->field_value, FILTER_VALIDATE_EMAIL)) { $message = [ "type" => "string.email", - "message" => ("`$this->field_item` should be an email."), - "label" => $this->field_item, + "message" => ("`$this->field_name` should be an email."), + "label" => $this->field_name, ]; $this->addError($message); } @@ -100,8 +92,8 @@ public function url() if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i", $this->field_value)) { $message = [ "type" => "string.url", - "message" => ("`{$this->field_item}` this should be a link(url)"), - "label" => $this->field_item, + "message" => ("'$this->field_name' this should be a link(url)"), + "label" => $this->field_name, ]; $this->addError($message); } @@ -114,40 +106,33 @@ public function url() */ public function match(string $key_to_match) { - $this->isString($key_to_match); - if (isset($this->source_data[$key_to_match]) && (strlen($this->field_value)!= strlen($this->source_data[$key_to_match])) && ($this->field_value!=$this->source_data[$key_to_match])) { + $this->isStringAndValid($key_to_match); + 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])) { $message = [ "type" => "string.match", - "message" => "`$this->field_item` should match `$key_to_match`", - "label" => $this->field_item, - ]; - $this->addError($message); - } - } - - public function required() - { - $required_value= trim($this->field_value); - if (strlen($required_value)==0) { - $message = [ - "type"=> "string.required", - "message" => "`$this->field_item` is required", - "label" => $this->field_item, + "message" => "`$this->field_name` should match `$key_to_match`", + "label" => $this->field_name, ]; $this->addError($message); } } - /** * * @param string $item_key * @return void */ - protected function isString(string $item_key): void + protected function isStringAndValid(string $item_key): void { - $field_to_check=!$item_key?$this->field_item:$item_key; + $field_to_check=!$item_key?$this->field_name:$item_key; $regex="#[a-zA-Z0-9]#"; - if(!preg_match($regex,$this->source_data[$field_to_check]) || strlen(trim($this->field_value))==0){ + if(!isset($this->data_source[$field_to_check])){ + $message = [ + 'type' => 'string.unknown', + 'message' => ("`$field_to_check` is not valid"), + 'label' => $field_to_check, + ]; + $this->addError($message); + }else if(!preg_match($regex,$this->data_source[$field_to_check]) || strlen(trim($this->field_value))==0){ $message=[ "type" => "string.unknown", "message" => ("`$field_to_check` should be a string"), From f9c919658bdcc941d01eafda156de7a546b23c59 Mon Sep 17 00:00:00 2001 From: bim-g Date: Thu, 8 Dec 2022 22:20:32 +0200 Subject: [PATCH 07/26] [ENH] Implement contract provider validator. --- src/Providers/Contracts/ValidationContracts.php | 12 ------------ src/Providers/Contracts/ValidatorContracts.php | 14 ++++++++++++++ 2 files changed, 14 insertions(+), 12 deletions(-) delete mode 100644 src/Providers/Contracts/ValidationContracts.php create mode 100644 src/Providers/Contracts/ValidatorContracts.php diff --git a/src/Providers/Contracts/ValidationContracts.php b/src/Providers/Contracts/ValidationContracts.php deleted file mode 100644 index 49cf1a4..0000000 --- a/src/Providers/Contracts/ValidationContracts.php +++ /dev/null @@ -1,12 +0,0 @@ - Date: Thu, 8 Dec 2022 22:27:44 +0200 Subject: [PATCH 08/26] [UPD] Remove useless module file --- shared/global.php | 5 ----- src/Traits/InitTrait.php | 9 ++++++-- test/ValidationTest.php | 45 ---------------------------------------- test/boolean.php | 10 --------- test/date.php | 9 -------- test/index.php | 23 -------------------- test/number.php | 7 ------- test/string.php | 13 ------------ 8 files changed, 7 insertions(+), 114 deletions(-) delete mode 100644 shared/global.php delete mode 100644 test/ValidationTest.php delete mode 100644 test/boolean.php delete mode 100644 test/date.php delete mode 100644 test/index.php delete mode 100644 test/number.php delete mode 100644 test/string.php diff --git a/shared/global.php b/shared/global.php deleted file mode 100644 index ef606ef..0000000 --- a/shared/global.php +++ /dev/null @@ -1,5 +0,0 @@ -field_item]['String']; + $conditions = $schema[$this->field_item]['String']; foreach ($conditions as $key=>$value){ call_user_func([$this,$key],$value); return; diff --git a/test/ValidationTest.php b/test/ValidationTest.php deleted file mode 100644 index 80c75cc..0000000 --- a/test/ValidationTest.php +++ /dev/null @@ -1,45 +0,0 @@ -assertIsObject($validate); - } - function testValidationStringObject(){ - $validate = new Validate(); - $schema=["name"=>$validate->string()->min(3)->max(10)->required()->check()]; - $expected=[ - "name"=>[ - "String"=>[ - "min"=>3, - "max"=>10, - "required"=>true - ] - ] - ]; - $this->assertEquals($schema,$expected); - } - function testValidationStringErrorObject(){ - $validate = new Validate(); - $schema=["name"=>$validate->string()->min(3)->max(10)->required()]; - $expected=[ - "name"=>[ - "string"=>[ - "min"=>3, - "max"=>10, - "required"=>true - ] - ] - ]; - $this->assertIsNotArray($schema["name"]); - $this->assertNotEquals($expected,$schema); - } -} \ No newline at end of file diff --git a/test/boolean.php b/test/boolean.php deleted file mode 100644 index 6764fe5..0000000 --- a/test/boolean.php +++ /dev/null @@ -1,10 +0,0 @@ -$valid->boolean("status")->required()->isValid('TRUE')->check(), - ]; - - $valid->check($source,$schema); -// check if the validation passed or not - var_dump($valid->passed()); - var_dump($valid->errors()); \ No newline at end of file diff --git a/test/date.php b/test/date.php deleted file mode 100644 index 6257c77..0000000 --- a/test/date.php +++ /dev/null @@ -1,9 +0,0 @@ -$valid->date("birth_day")->min("-18years")->required()->check(), - "date_created"=>$valid->date("birth_day")->now()->max("100years")->required()->check() - ]; - $valid->check($source,$schema); - - var_dump($valid->passed()); - var_dump($valid->errors()); \ No newline at end of file diff --git a/test/index.php b/test/index.php deleted file mode 100644 index 5c12383..0000000 --- a/test/index.php +++ /dev/null @@ -1,23 +0,0 @@ - "", -// "age" => 12, -// "country" => "DRC", -// "state" => "North Kivu", -// "password"=>"1234567", -// "n_password"=>123456, -// "city" => "Goma", -// "email"=>"infos@wepesi.com", -// "link"=> "https://github.com/bim-g/wepesi_validation/", -// "status"=> true, -// "birth_day"=>"2021-05-23", -// "date_created"=>"2021-05-23" -// ]; -// $valid=new Validates($source,"fr"); -//// include "./test/string.php"; -// include "./test/number.php"; -//// include "./test/boolean.php"; -//// include "./test/date.php"; -/// diff --git a/test/number.php b/test/number.php deleted file mode 100644 index 4137f99..0000000 --- a/test/number.php +++ /dev/null @@ -1,7 +0,0 @@ -$valid->number("age")->min(18)->max(50)->required()->check() - ]; - $valid->check($source,$schema); -var_dump($valid->passed()); -var_dump($valid->errors()); \ No newline at end of file diff --git a/test/string.php b/test/string.php deleted file mode 100644 index 19bb7b2..0000000 --- a/test/string.php +++ /dev/null @@ -1,13 +0,0 @@ -$valid->string("name")->email()->min(35)->max(50)->required()->check(), - "country"=>$valid->string("country")->min(3)->max(40)->check(), - "password"=>$valid->string("password")->min(3)->max(40)->check(), - "n_password"=>$valid->string("n_password")->min(3)->max(40)->match("password")->check(), - ]; - // var_dump($schema); - $valid->check($source,$schema); -// check if the validation passed or not - var_dump($valid->passed()); - var_dump($valid->errors()); \ No newline at end of file From bfababd619b449a5c6979a0346ba77b7fdf1a3dd Mon Sep 17 00:00:00 2001 From: bim-g Date: Thu, 8 Dec 2022 22:40:04 +0200 Subject: [PATCH 09/26] [UPD] Add schema module to separate valition from schema for better control --- src/Schema.php | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/Schema.php diff --git a/src/Schema.php b/src/Schema.php new file mode 100644 index 0000000..c0c5376 --- /dev/null +++ b/src/Schema.php @@ -0,0 +1,54 @@ + true]; + } + + /** + * @return StringSchema + */ + public function string(): StringSchema + { + return new class extends StringSchema{}; + } + + /** + * @return NumberSchema + */ + public function number(): NumberSchema + { + return new NumberSchema(); + } + + /** + * @return DateSchema + */ + public function date(): DateSchema + { + return new DateSchema(); + } + + /** + * @return BooleanSchema + */ + public function boolean(): BooleanSchema + { + return new BooleanSchema(); + } + +} \ No newline at end of file From ef4e28e5ba0ee60509b04581c56d0e1cb649f276 Mon Sep 17 00:00:00 2001 From: bim-g Date: Thu, 8 Dec 2022 22:40:32 +0200 Subject: [PATCH 10/26] [UPD] string Validation and schema test --- test/Schema/StringSchemaTest.php | 41 +++++++++++++----------- test/SchemaTest.php | 48 ++++++++++++++++++++++++++++ test/Script/StringValidationTest.php | 43 +++++++++++++------------ 3 files changed, 93 insertions(+), 39 deletions(-) create mode 100644 test/SchemaTest.php diff --git a/test/Schema/StringSchemaTest.php b/test/Schema/StringSchemaTest.php index f820d88..6d7a165 100644 --- a/test/Schema/StringSchemaTest.php +++ b/test/Schema/StringSchemaTest.php @@ -1,5 +1,8 @@ assertIsObject($stringSchema); } function testStringObjectIsKey(){ - $stringSchema= new StringSchema(); - $this->assertArrayHasKey("String",$stringSchema->check()); + $stringSchema = new class extends StringSchema{}; + $this->assertArrayHasKey("StringValidator",$stringSchema->generate()); } function testStringEmailKey(){ - $stringSchema = new StringSchema(); - $subset_array=["String"=>["email"=>true]]; - $this->assertEquals($subset_array, $stringSchema->email()->check()); + $stringSchema = new class extends StringSchema{}; + $subset_array = ["StringValidator"=>["email"=>true]]; + $this->assertEquals($subset_array, $stringSchema->email()->generate()); } function testStringURLKey(){ - $stringSchema = new StringSchema(); - $subset_array=["String"=>["url"=>true]]; - $this->assertEquals($subset_array, $stringSchema->url()->check()); + $stringSchema = new class extends StringSchema{}; + $subset_array = ["StringValidator"=>["url"=>true]]; + $this->assertEquals($subset_array, $stringSchema->url()->generate()); } function testStringMatchKey(){ - $stringSchema = new StringSchema(); - $subset_array=["String"=>["match"=>'email']]; - $this->assertEquals($subset_array, $stringSchema->match('email')->check()); + $stringSchema = new class extends StringSchema{}; + $subset_array = ["StringValidator" => ["match"=>'email']]; + $this->assertEquals($subset_array, $stringSchema->match('email')->generate()); } function testStringMinimumKey(){ - $stringSchema = new StringSchema(); - $subset_array=["String"=>["min"=>1]]; - $this->assertEquals($subset_array, $stringSchema->min(1)->check()); + $stringSchema = new class extends StringSchema{}; + $subset_array = ["StringValidator" => ["min" => 1]]; + $this->assertEquals($subset_array, $stringSchema->min(1)->generate()); } function testStringMaximumKey(){ - $stringSchema = new StringSchema(); - $subset_array=["String"=>["max"=>10]]; - $this->assertEquals($subset_array, $stringSchema->max(10)->check()); + $stringSchema = new class extends StringSchema{}; + $subset_array = ["StringValidator" => ["max" => 10]]; + $this->assertEquals($subset_array, $stringSchema->max(10)->generate()); } } \ No newline at end of file diff --git a/test/SchemaTest.php b/test/SchemaTest.php new file mode 100644 index 0000000..c916adb --- /dev/null +++ b/test/SchemaTest.php @@ -0,0 +1,48 @@ +assertIsObject($schema); + } + function testSchemaStringObject(){ + $schema = new Schema(); + $rules=["name"=>$schema->string()->min(3)->max(10)->required()->generate()]; + $expected=[ + "name"=>[ + "StringValidator"=>[ + "min"=>3, + "max"=>10, + "required"=>true + ] + ] + ]; + $this->assertEquals($rules,$expected); + } + function testSchemaStringErrorObject(){ + $schema = new Schema(); + $rules=["name" => $schema->string()->min(3)->max(10)->required()]; + $expected=[ + "name"=>[ + "StringValidator"=>[ + "min"=>3, + "max"=>10, + "required"=>true + ] + ] + ]; + $this->assertIsNotArray($rules["name"]); + $this->assertNotEquals($expected,$rules); + } +} \ No newline at end of file diff --git a/test/Script/StringValidationTest.php b/test/Script/StringValidationTest.php index cc967f4..9441042 100644 --- a/test/Script/StringValidationTest.php +++ b/test/Script/StringValidationTest.php @@ -1,10 +1,13 @@ getMessage(); @@ -31,7 +34,7 @@ function testStringValidationSchemaException(){ $stringValidationSchemaException= Exception::class; try { $source=["name"=>"john Doe"]; - $stringValidationSchemaException = new StringValidation($source, []); + $stringValidationSchemaException = new StringValidator($source, []); }catch (Exception $ex){ $className = get_class($ex); $msg = $ex->getMessage(); @@ -53,7 +56,7 @@ function testStringValidationFieldNotDefinedException(){ "required"=>false ] ]]; - $stringValidationSchemaException = new StringValidation($source, $schema); + $stringValidationSchemaException = new StringValidator($source, $schema); }catch (Exception $ex){ $className = get_class($ex); $msg = $ex->getMessage(); @@ -78,7 +81,7 @@ function testStringIsObject(){ ] ] ]; - $stringValidation= new StringValidation($source,$schema); + $stringValidation= new StringValidator($source,$schema); $this->assertIsObject($stringValidation); } /** @@ -93,7 +96,7 @@ function testStringMinError(){ ] ] ]; - $stringValidation= new StringValidation($source,$schema); + $stringValidation= new StringValidator($source,$schema); $error[]=[ 'type'=>'string.min', 'message'=>'`name` should have minimum of `150` characters', @@ -115,7 +118,7 @@ function testStringMinSuccess(){ ] ] ]; - $stringValidation= new StringValidation($source,$schema); + $stringValidation= new StringValidator($source,$schema); $this->assertEmpty($stringValidation->result()); }/** * @throws Exception @@ -130,7 +133,7 @@ function testStringMaxError(){ ] ] ]; - $stringValidation= new StringValidation($source,$schema); + $stringValidation= new StringValidator($source,$schema); $error[]=[ 'type'=>'string.max', 'message'=>'`description` should have maximum of `150` characters', @@ -152,7 +155,7 @@ function testStringMaxSuccess(){ ] ] ]; - $stringValidation= new StringValidation($source,$schema); + $stringValidation= new StringValidator($source,$schema); $this->assertEmpty($stringValidation->result()); }/** * @throws Exception @@ -166,7 +169,7 @@ function testStringEmailError(){ ] ] ]; - $stringValidation= new StringValidation($source,$schema); + $stringValidation= new StringValidator($source,$schema); $error[]=[ 'type'=>'string.email', 'message'=>'`email` should be an email.', @@ -187,7 +190,7 @@ function testStringEmailSuccess(){ ] ] ]; - $stringValidation= new StringValidation($source,$schema); + $stringValidation= new StringValidator($source,$schema); $this->assertEmpty($stringValidation->result()); } /** @@ -202,7 +205,7 @@ function testStringURLError(){ ] ] ]; - $stringValidation= new StringValidation($source,$schema); + $stringValidation= new StringValidator($source,$schema); $error[]=[ 'type'=>'string.url', 'message'=>'`link` this should be a link(url)', @@ -228,7 +231,7 @@ function testStringURLSuccess(){ ] ] ]; - $stringValidation= new StringValidation($source,$schema); + $stringValidation= new StringValidator($source,$schema); $this->assertEmpty($stringValidation->result()); } @@ -245,7 +248,7 @@ function testStringRequiredErrorsUnknown() ] ] ]; - $stringValidation = new StringValidation($source, $schema); + $stringValidation = new StringValidator($source, $schema); $error=[ 'type' => 'string.unknown', 'message' => "`username` should be a string", @@ -267,7 +270,7 @@ function testStringRequiredErrors() ] ] ]; - $stringValidation = new StringValidation($source, $schema); + $stringValidation = new StringValidator($source, $schema); $error=[ 'type' => 'string.required', 'message' => "`username` is required", @@ -289,7 +292,7 @@ function testStringRequiredSuccess() ] ] ]; - $stringValidation = new StringValidation($source, $schema); + $stringValidation = new StringValidator($source, $schema); $this->assertEmpty($stringValidation->result()); } @@ -309,7 +312,7 @@ function testStringMatchErrors() ] ] ]; - $stringValidation = new StringValidation($source, $schema); + $stringValidation = new StringValidator($source, $schema); $error[]=[ 'type' => 'string.match', 'message' => "`password` should match `retape_password`", @@ -334,7 +337,7 @@ function testStringMatchSuccess() ] ] ]; - $stringValidation = new StringValidation($source, $schema); + $stringValidation = new StringValidator($source, $schema); $this->assertEmpty($stringValidation->result()); } } \ No newline at end of file From 1a8eaa57069dfc1e2dbe01bcda192d3282056950 Mon Sep 17 00:00:00 2001 From: bim-g Date: Thu, 8 Dec 2022 22:41:40 +0200 Subject: [PATCH 11/26] [UPD] refactor validate module to focus only on validation. --- src/Validate.php | 143 ++++++++++++++++++++++------------------------- 1 file changed, 67 insertions(+), 76 deletions(-) diff --git a/src/Validate.php b/src/Validate.php index c2022cf..e3dd6bc 100644 --- a/src/Validate.php +++ b/src/Validate.php @@ -1,115 +1,106 @@ errors=[]; - $this->passed=false; - $this->resource=[]; + $this->errors = []; + $this->passed = false; } - function check(array $source,array $schema){ - $this->errors = []; - $this->check_undefined_Object_key($source, $schema); - foreach ($schema as $item => $response) { - if (isset($source[$item])) { - if ($response) { - foreach ($response as $key => $value) { - $this->addError($value); - } - } - } else { + /** + * @param array $resource data source where the information will be extracted; + * @param array $schema data schema + * @return void + */ + function check(array $resource, array $schema){ + try { + + + $this->errors = []; + $option_resolver = []; + /** + * use of Option resolver to catch all undefined key + * Check for undefined object + */ + foreach ($resource as $item => $response) { + $option_resolver[] = new Option($item); + } + $resolver = new OptionsResolver($option_resolver); + $options = $resolver->resolve($schema); + if (isset($options["exception"])) { $message = [ 'type' => 'object.unknown', - 'message' => "`$item` does not exist", - 'label' => $item, + 'message' => $options["exception"], + 'label' => "label", ]; $this->addError($message); - } - } - if (count($this->errors) == 0) { - $this->passed = true; - } - } - function any(){ + }else{ + foreach ($schema as $item => $rules) { + $key = array_keys($rules)[0]; + if ($key == "any") continue; + $value = $resource[$item]; - } + $class_name = "\Wepesi\App\Validator\\".$key; - private function check_undefined_Object_key($source,$items){ - $diff_array_key = array_diff_key($source, $items); - $source_key = array_keys($diff_array_key); - if (count($source_key) > 0) { - foreach ($source_key as $key) { - $message = [ - 'type' => 'object.undefined', - 'message' => "`$key` is not defined", - 'label' => $key - ]; - $this->addError($message); + $reflexion = new ReflectionClass($class_name); + + $instance = $reflexion->newInstance($item, $value,$resource); + + foreach ($rules[$key] as $method => $params){ + if(method_exists($instance,$method)){ + call_user_func_array([$instance,$method],[$params]); + } + } + $result = $instance->result(); + if( count($result)>0 ){ + $this->errors = array_merge($this->errors,$result); + } + } + if ( count($this->errors) == 0) { + $this->passed = true; + } } + }catch (Exception $ex){ + $message = [ + 'type' => 'object.unknown', + 'message' => $ex, + 'label' => "unknown", + ]; + $this->addError($message); } } private function addError(array $message){ - $this->errors[]=$message; - } - /** - * @return StringSchema - */ - function string(): StringSchema - { - return new StringSchema(); - } - - /** - * @return NumberSchema - */ - function number(): NumberSchema - { - return new NumberSchema(); - } - - /** - * @return DateSchema - */ - function date(): DateSchema - { - return new DateSchema(); - } - - function boolean(): BooleanSchema - { - return new BooleanSchema(); + $this->errors[] = $message; } /** * @return array */ - function errors(): array + public function errors(): array { return $this->errors; } /** * @return bool */ - function passed(): bool + public function passed(): bool { return $this->passed; } From e384ea7785e10ced6b47af40ecd6f21829d5b5c7 Mon Sep 17 00:00:00 2001 From: bim-g Date: Thu, 8 Dec 2022 22:55:41 +0200 Subject: [PATCH 12/26] [UPD] refactor string example --- example/string.php | 25 +++++++++++++++++++------ index.php | 9 ++++++--- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/example/string.php b/example/string.php index e96e196..55b77b9 100644 --- a/example/string.php +++ b/example/string.php @@ -6,14 +6,27 @@ /** * Validate string */ + +use Wepesi\App\Validate; + +$validate = new Validate(); +$source = [ + 'name' => 'ibrahim', + 'country' => "", + 'password' => '1234567', + 'new_password' => 123456, + 'email' => 'infos@wepesi.com', + 'link' => 'https://github.com/bim-g/wepesi_validation/' +]; $rules = [ "name" => $schema->string()->email()->min(35)->max(50)->required()->generate(), - "country" => $schema->string()->min(3)->max(40)->generate(), + "country" => $schema->string()->min(3)->max(40)->required()->generate(), "password" => $schema->string()->min(3)->max(40)->generate(), - "n_password" => $schema->string()->min(3)->max(40)->match("password")->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(), ]; - var_dump($rules); -//$valid->check($source, $rules); +$validate->check($source, $rules); //// check if the validation passed or not -//var_dump($valid->passed()); -//var_dump($valid->errors()); \ No newline at end of file +var_dump($validate->passed()); +var_dump($validate->errors()); \ No newline at end of file diff --git a/index.php b/index.php index b718ee1..35196b1 100644 --- a/index.php +++ b/index.php @@ -1,5 +1,8 @@ Date: Thu, 8 Dec 2022 23:07:21 +0200 Subject: [PATCH 13/26] [UPD] refactor number validation schema and validator module --- example/string.php | 2 + src/Script/NumberValidation.php | 124 ------------------------------ src/Validator/NumberValidator.php | 102 ++++++++++++++++++++++++ 3 files changed, 104 insertions(+), 124 deletions(-) delete mode 100644 src/Script/NumberValidation.php create mode 100644 src/Validator/NumberValidator.php diff --git a/example/string.php b/example/string.php index 55b77b9..3abedcb 100644 --- a/example/string.php +++ b/example/string.php @@ -7,8 +7,10 @@ * Validate string */ +use Wepesi\App\Schema; use Wepesi\App\Validate; +$schema = new Schema(); $validate = new Validate(); $source = [ 'name' => 'ibrahim', diff --git a/src/Script/NumberValidation.php b/src/Script/NumberValidation.php deleted file mode 100644 index d78a3c0..0000000 --- a/src/Script/NumberValidation.php +++ /dev/null @@ -1,124 +0,0 @@ -source_data=$source; - $this->field_item=$string_item; - $this->_max= $this->_min=0; - if($this->isNumber()){ - $this->number_value=$source[$string_item]; - } - } - - /** - * @param $rule - * @return $this - */ - function min($rule): NumberValidation - { - if ((int) $this->number_value < $rule) { - $message=[ - "type"=>"number.min", - "message"=> "`$this->field_item` should be greater than `$rule`", - "label"=>$this->field_item, - "limit"=>$rule - ]; - $this->addError($message); - } - return $this; - } - - /** - * @param $rule - * @return $this - */ - function max($rule): NumberValidation - { - if ((int) $this->number_value > $rule) { - $message=[ - "type"=>"number.max", - "message"=> "`$this->field_item` should be less than `$rule`", - "label"=>$this->field_item, - "limit"=>$rule - ]; - $this->addError($message); - } - return $this; - } - - /** - * @param int $min_values - * @return $this - */ - function positive(): NumberValidation - { - if ((int) $this->number_value < 0) { - $message=[ - "type"=>"number.positive", - "message"=> "`$this->field_item` should be a positive number", - "label"=>$this->field_item, - "limit"=>1 - ]; - $this->addError($message); - } - return $this; - } - - /** - * @return $this - */ -// function required() -// { -// $this->isRequired("number",$this->field_item); -// return $this; -// } - - /** - * @param string|null $itemKey - * @return bool - */ - private function isNumber(): bool - { - $regex_string="#[a-zA-Z]#"; - if (preg_match($regex_string,trim($this->source_data[$this->field_item])) || !is_integer($this->source_data[$this->field_item])) { - $message = [ - "type"=> "number.unknown", - "message" => "`$this->field_item` should be a number", - "label" => $this->field_item, - ]; - $this->addError($message); - return false; - } - return true; - } - /** - * @return array - */ - function check(): array - { - return $this->_errors; - } -} diff --git a/src/Validator/NumberValidator.php b/src/Validator/NumberValidator.php new file mode 100644 index 0000000..e12e2d5 --- /dev/null +++ b/src/Validator/NumberValidator.php @@ -0,0 +1,102 @@ +data_source = $source; + $this->field_name = $string_item; + $this->field_value= $value; + $this->class_provider = "number"; + if($this->isNumber()){ + $this->field_value = $source[$string_item]; + } + parent::__construct(); + } + + /** + * @param int $rule + * @return void + */ + function min(int $rule) + { + if ((int) $this->field_value < $rule) { + $message = [ + "type" => "number.min", + "message" => "`$this->field_name` should be greater than `$rule`", + "label" => $this->field_name, + "limit" => $rule + ]; + $this->addError($message); + } + } + + /** + * @param int $rule + * @return void + */ + function max(int $rule) + { + if ((int) $this->field_value > $rule) { + $message = [ + "type" => "number.max", + "message" => "`$this->field_name` should be less than `$rule`", + "label" => $this->field_name, + "limit" => $rule + ]; + $this->addError($message); + } + } + + /** + * + */ + function positive() + { + if ((int) $this->field_value < 0) { + $message = [ + "type" => "number.positive", + "message" => "`$this->field_name` should be a positive number", + "label" => $this->field_name, + "limit" => 1 + ]; + $this->addError($message); + } + } + + /** + * @return bool + */ + 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])) { + $message = [ + "type" => "number.unknown", + "message" => "`$this->field_name` should be a number", + "label" => $this->field_name, + ]; + $this->addError($message); + return false; + } + return true; + } +} From 9639f75f68d0d96e4725cfea6d5012e82c137a65 Mon Sep 17 00:00:00 2001 From: bim-g Date: Thu, 8 Dec 2022 23:07:48 +0200 Subject: [PATCH 14/26] [UPD] refactor number validation schema and validator example --- example/index.php | 29 +++++++++++++++++++++++++++++ example/number.php | 26 ++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 example/index.php create mode 100644 example/number.php diff --git a/example/index.php b/example/index.php new file mode 100644 index 0000000..9a50c9c --- /dev/null +++ b/example/index.php @@ -0,0 +1,29 @@ + "ibrahim", + "age" => 12, + "country" => "DRC", + "state" => "North Kivu", + "password"=>"1234567", + "new_password"=>123456, + "city" => "Goma", + "email"=>"infos@wepesi.com", + "link"=> "https://github.com/bim-g/wepesi_validation/", + "status"=> true, + "birth_day"=>"2021-05-23", + "date_created"=>"2021-05-23" +]; + +// $valid->check($source,$schema); +// include_once __DIR__."/string.php"; + include __DIR__."/number.php"; +// include __DIR__."/boolean.php"; +// include __DIR__."/date.php"; +/// diff --git a/example/number.php b/example/number.php new file mode 100644 index 0000000..add60b4 --- /dev/null +++ b/example/number.php @@ -0,0 +1,26 @@ + 20, + "length" => 0, + "height" =>"35", + "width" =>"", + "direction" => -7 +]; +$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); + +var_dump($validate->passed()); +var_dump($validate->errors()); \ No newline at end of file From a5c8b844db9da7d1593d736a72093a317c60e857 Mon Sep 17 00:00:00 2001 From: bim-g Date: Thu, 8 Dec 2022 23:20:51 +0200 Subject: [PATCH 15/26] [ENH] refactor number validation schema provider contract and validator example --- src/Schema.php | 2 +- src/Schema/NumberSchema.php | 12 +++++------- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/Schema.php b/src/Schema.php index c0c5376..6e882ba 100644 --- a/src/Schema.php +++ b/src/Schema.php @@ -32,7 +32,7 @@ public function string(): StringSchema */ public function number(): NumberSchema { - return new NumberSchema(); + return new class extends NumberSchema{}; } /** diff --git a/src/Schema/NumberSchema.php b/src/Schema/NumberSchema.php index 9afde33..135c3cf 100644 --- a/src/Schema/NumberSchema.php +++ b/src/Schema/NumberSchema.php @@ -1,9 +1,7 @@ schema[$this->source]['positive']=true; + $this->schema[$this->source]['positive'] = true; return $this; } } From 41f6eb5762f77ba7e6010807faf3117b17494764 Mon Sep 17 00:00:00 2001 From: bim-g Date: Thu, 8 Dec 2022 23:21:40 +0200 Subject: [PATCH 16/26] [UDP] add Number support new structure. --- test/Schema/NumberSchemaTest.php | 23 ++++++++++--------- test/SchemaTest.php | 38 ++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 10 deletions(-) diff --git a/test/Schema/NumberSchemaTest.php b/test/Schema/NumberSchemaTest.php index 09e173c..3f46579 100644 --- a/test/Schema/NumberSchemaTest.php +++ b/test/Schema/NumberSchemaTest.php @@ -1,5 +1,8 @@ assertIsObject($numberSchema); } function testStringObjectIsKey() { - $numberSchema = new NumberSchema(); - $this->assertArrayHasKey('Number', $numberSchema->check()); + $numberSchema = new class extends NumberSchema{}; + $this->assertArrayHasKey("NumberValidator", $numberSchema->generate()); } function testRequiredKey() { - $numberSchema = new NumberSchema(); - $subset_array = ['Number' => ['required' => true]]; - $this->assertEquals($subset_array, $numberSchema->required()->check()); + $numberSchema = new class extends NumberSchema{}; + $subset_array = ["NumberValidator" => ['required' => true]]; + $this->assertEquals($subset_array, $numberSchema->required()->generate()); } function testNumberPositiveKey() { - $numberSchema = new NumberSchema(); - $subset_array = ['Number' => ['positive' => true]]; - $this->assertEquals($subset_array, $numberSchema->positive()->check()); + $numberSchema = new class extends NumberSchema{}; + $subset_array = ["NumberValidator" => ['positive' => true]]; + $this->assertEquals($subset_array, $numberSchema->positive()->generate()); } } \ No newline at end of file diff --git a/test/SchemaTest.php b/test/SchemaTest.php index c916adb..a424238 100644 --- a/test/SchemaTest.php +++ b/test/SchemaTest.php @@ -16,6 +16,10 @@ function testSchemaObject(){ $schema = new Schema(); $this->assertIsObject($schema); } + + /** + * @return void + */ function testSchemaStringObject(){ $schema = new Schema(); $rules=["name"=>$schema->string()->min(3)->max(10)->required()->generate()]; @@ -45,4 +49,38 @@ function testSchemaStringErrorObject(){ $this->assertIsNotArray($rules["name"]); $this->assertNotEquals($expected,$rules); } + /* + * Number Schema Test + */ + function testSchemaNumberObject(){ + $schema = new Schema(); + $rules=["age"=>$schema->number()->min(3)->max(10)->required()->positive()->generate()]; + $expected=[ + "age"=>[ + "NumberValidator"=>[ + "min"=>3, + "max"=>10, + "required"=>true, + "positive" => true, + ] + ] + ]; + $this->assertEquals($rules,$expected); + } + function testSchemaNumberErrorObject(){ + $schema = new Schema(); + $rules = ["age" => $schema->number()->min(5)->max(10)->positive()->required()]; + $expected=[ + "age"=>[ + "NumberValidator"=>[ + "min"=>3, + "max"=>10, + "required"=>true, + "positive"=>false, + ] + ] + ]; + $this->assertIsNotArray($rules["age"]); + $this->assertNotEquals($expected,$rules); + } } \ No newline at end of file From 13d1d924c2307efac776e3405c3c8a159d8f43e0 Mon Sep 17 00:00:00 2001 From: bim-g Date: Fri, 9 Dec 2022 00:49:17 +0200 Subject: [PATCH 17/26] [ENH] Refactor DateValidator et DateSchema --- src/Providers/SChemaProvider.php | 4 +- src/Schema.php | 2 +- src/Schema/DateSchema.php | 30 +++++- src/Script/VDate.php | 170 ------------------------------- src/Validator/DateValidator.php | 140 +++++++++++++++++++++++++ 5 files changed, 169 insertions(+), 177 deletions(-) delete mode 100644 src/Script/VDate.php create mode 100644 src/Validator/DateValidator.php diff --git a/src/Providers/SChemaProvider.php b/src/Providers/SChemaProvider.php index 2dcc985..78efccd 100644 --- a/src/Providers/SChemaProvider.php +++ b/src/Providers/SChemaProvider.php @@ -23,7 +23,7 @@ function __construct(string $type) * @param int $rule * @return SChemaProvider */ - function min(int $rule): SChemaProvider + public function min(int $rule): SChemaProvider { $this->schema[$this->source]["min"]=$rule; return $this; @@ -33,7 +33,7 @@ function min(int $rule): SChemaProvider * @param $rule * @return $this */ - function max($rule): SChemaProvider + public function max($rule): SChemaProvider { $this->schema[$this->source]["max"]=$rule; return $this; diff --git a/src/Schema.php b/src/Schema.php index 6e882ba..14aad78 100644 --- a/src/Schema.php +++ b/src/Schema.php @@ -40,7 +40,7 @@ public function number(): NumberSchema */ public function date(): DateSchema { - return new DateSchema(); + return new class extends DateSchema{}; } /** diff --git a/src/Schema/DateSchema.php b/src/Schema/DateSchema.php index ad296ce..95796e2 100644 --- a/src/Schema/DateSchema.php +++ b/src/Schema/DateSchema.php @@ -1,5 +1,8 @@ schema[$this->source]['min'] = $rule; + return $this; } + /** + * @param string $rule + * @return $this + */ + public function max($rule): DateSchema + { + $this->schema[$this->source]['max'] = $rule; + return $this; + } /** * @return $this */ function now(): DateSchema { - $this->schema[$this->source]["now"]=true; + $this->schema[$this->source]["now"] = true; return $this; } /** diff --git a/src/Script/VDate.php b/src/Script/VDate.php deleted file mode 100644 index d7077b4..0000000 --- a/src/Script/VDate.php +++ /dev/null @@ -1,170 +0,0 @@ -date_value=$source[$string_item]; -// $this->string_item=$string_item; -// $this->source_data=$source; -// $this->i18n= new i18n($lang); -// $this->_max= $this->_min=0; -// $this->checkExist(); -// } -// -// /** -// * @return $this -// */ -// function now(): SCHDate -// { -// $min_date_time=strtotime("now"); -// $min_date=date("d/F/Y",$min_date_time); -// $date_value_time= strtotime($this->date_value); -// if ($date_value_time < $min_date_time) { -// $message=[ -// "type"=>"date.now", -// "message"=> $this->i18n->translate("`%s` should be greater than now",[$this->string_item]), -// "label"=>$this->string_item, -// "limit"=>$min_date -// ]; -// $this->addError($message); -// } -// return $this; -// } -// -// /** -// * @param string|null $times -// * @return $this -// * while trying to get day validation use this module -// */ -// function today(string $times=null): SCHDate -// { -// $min_date_time=strtotime("now {$times}"); -// $min_date=date("d/F/Y",$min_date_time); -// $date_value_time= strtotime($this->date_value); -// if ($date_value_time > $min_date_time) { -// $message=[ -// "type"=>"date.now", -// "message"=> $this->i18n->translate("`%s` should be greater than today ",[$this->string_item]), -// "label"=>$this->string_item, -// "limit"=>$min_date -// ]; -// $this->addError($message); -// } -// return $this; -// } -// -// /** -// * @param string|null $rule_values -// * @return $this -// * get the min date control from the given date -// */ -// function min(string $rule_values=null): SCHDate -// { -// /** -// * $regex= "#[a-zA-Z]#"; -// * $time= preg_match($regex,$rule_values); -// * $con=!$time?$time:(int)$time; -// * in case the parameters are integers -// */ -// $rule_values= $rule_values ?? "now"; -// $min_date_time=strtotime($rule_values); -// $min_date=date("d/F/Y",$min_date_time); -// $date_value_time= strtotime($this->date_value); -// if ($date_value_time > $min_date_time) { -// $message=[ -// "type"=>"date.min", -// "message"=> $this->i18n->translate("`%s` should be greater than `%s`",[$this->string_item,$min_date]), -// "label"=>$this->string_item, -// "limit"=>$min_date -// ]; -// $this->addError($message); -// } -// return $this; -// } -// -// /** -// * @param string|null $rule_values -// * @return $this -// * while try to check maximum date of a defined period use this module -// */ -// function max(string $rule_values=null): SCHDate -// { -// $rule_values= $rule_values ?? "now"; -// $max_date_time=strtotime($rule_values); -// $max_date=date("d/F/Y",$max_date_time); -// $date_value_time= strtotime($this->date_value); -// if ($max_date_time<$date_value_time) { -// $message = [ -// "type" => "date.max", -// "message" => $this->i18n->translate("`%s` should be less than `%s`",[$this->string_item,$max_date]), -// "label" => $this->string_item, -// "limit" => $max_date -// ]; -// $this->addError($message); -// } -// return $this; -// } -// /** -// * @return $this -// * call this module is the input is requied and should not be null or empty -// */ -// function required(): SCHDate -// { -// $required_value= trim($this->date_value); -// if (empty($required_value) || strlen($required_value)==0) { -// $message = [ -// "type"=> "any.required", -// "message" => $this->i18n->translate("`%s` is required",[$this->string_item]), -// "label" => $this->string_item, -// ]; -// $this->addError($message); -// } -// return $this; -// } -//// private methode -// private function checkExist(string $itemKey=null): void -// { -// $item_to_check=$itemKey?$itemKey:$this->string_item; -// $regex="#[a-zA-Z0-9]#"; -// $this->_errors=[]; -// if (!isset($this->source_data[$item_to_check])) { -// $message = [ -// "type"=> "any.unknown", -// "message" => $this->i18n->translate("`%s` is unknown",[$item_to_check]), -// "label" => $item_to_check, -// ]; -// $this->addError($message); -// }else if(!preg_match($regex,$this->source_data[$item_to_check]) || strlen(trim($this->source_data[$item_to_check]))==0){ -// $message=[ -// "type" => "date.unknown", -// "message" => $this->i18n->translate("`%s` should be a date.",[$item_to_check]), -// "label" => $item_to_check, -// ]; -// $this->addError($message); -// } -// } -// private function addError(array $value): void -// { -// $this->_errors[] = $value; -// } -// function check(): array -// { -// return $this->_errors; -// } -//} \ No newline at end of file diff --git a/src/Validator/DateValidator.php b/src/Validator/DateValidator.php new file mode 100644 index 0000000..0011894 --- /dev/null +++ b/src/Validator/DateValidator.php @@ -0,0 +1,140 @@ +field_value = $source[$item]; + $this->field_name = $item; + $this->data_source = $source; + $this->class_provider = "date"; + $this->checkExist(); + parent::__construct(); + } + + /** + * @return void + */ + public function now() + { + $min_date_time = strtotime("now"); + $min_date = date("d/F/Y",$min_date_time); + $date_value_time = strtotime($this->field_value); + if ($date_value_time < $min_date_time) { + $message=[ + "type" => "date.now", + "message" => "`$this->field_name` should be greater than now", + "label" => $this->field_name, + "limit" => $min_date + ]; + $this->addError($message); + } + } + + /** + * @param string|null $times + * @return void + * while trying to get day validation use this module + */ + public function today(string $times=null) + { + $min_date_time = strtotime("now {$times}"); + $min_date = date("d/F/Y",$min_date_time); + $date_value_time = strtotime($this->field_value); + if ($date_value_time > $min_date_time) { + $message=[ + "type" => "date.now", + "message" => "`$this->field_name` should be greater than today ", + "label" => $this->field_name, + "limit" => $min_date + ]; + $this->addError($message); + } + } + + /** + * @param string|null $rule_values + * @return void + * get the min date control from the given date + */ + public function min($rule_values) + { + /** + * $regex= "#[a-zA-Z]#"; + * $time= preg_match($regex,$rule_values); + * $con=!$time?$time:(int)$time; + * in case the parameters are integers + */ + $rule_values = $rule_values ?? "now"; + $min_date_time = strtotime($rule_values); + $min_date = date("d/F/Y",$min_date_time); + $date_value_time = strtotime($this->field_value); + if ($date_value_time > $min_date_time) { + $message=[ + "type" => "date.min", + "message" => "`$this->field_name` should be greater than `$min_date`", + "label" => $this->field_name, + "limit" => $min_date + ]; + $this->addError($message); + } + } + + /** + * @param string|null $rule_values + * @return void + * while try to check maximum date of a defined period use this module + */ + public function max($rule_values) + { + $rule_values = $rule_values ?? "now"; + $max_date_time = strtotime($rule_values); + $max_date = date("d/F/Y",$max_date_time); + $date_value_time = strtotime($this->field_value); + if ($max_date_time < $date_value_time) { + $message = [ + "type" => "date.max", + "message" => "`$this->field_name` should be less than `$max_date`", + "label" => $this->field_name, + "limit" => $max_date + ]; + $this->addError($message); + } + } + + protected function checkExist(string $itemKey=null): void + { + $item_to_check = $itemKey?$itemKey:$this->field_name; + $regex = "#[a-zA-Z0-9]#"; + $this->_errors=[]; + if (!isset($this->data_source[$item_to_check])) { + $message = [ + "type"=> "any.unknown", + "message" => "`$item_to_check` is unknown", + "label" => $item_to_check, + ]; + $this->addError($message); + }else if(!preg_match($regex,$this->data_source[$item_to_check]) || strlen(trim($this->data_source[$item_to_check]))==0){ + $message=[ + "type" => "date.unknown", + "message" => "`$item_to_check` should be a date.", + "label" => $item_to_check, + ]; + $this->addError($message); + } + } +} \ No newline at end of file From df3a66edc529f03834893f67fecd72720f0ae94f Mon Sep 17 00:00:00 2001 From: bim-g Date: Fri, 9 Dec 2022 00:50:22 +0200 Subject: [PATCH 18/26] [UPD] Refactor and add DateValidator from test --- example/index.php | 8 ++------ test/Schema/DateSchemaTest.php | 23 ++++++++++++---------- test/SchemaTest.php | 35 ++++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 16 deletions(-) diff --git a/example/index.php b/example/index.php index 9a50c9c..93f1922 100644 --- a/example/index.php +++ b/example/index.php @@ -4,8 +4,6 @@ * @author Boss Ibrahim Mussa */ -use Wepesi\App\Schema; - $data_source = [ "name" => "ibrahim", "age" => 12, @@ -21,9 +19,7 @@ "date_created"=>"2021-05-23" ]; -// $valid->check($source,$schema); // include_once __DIR__."/string.php"; - include __DIR__."/number.php"; +// include __DIR__."/number.php"; + include __DIR__."/date.php"; // include __DIR__."/boolean.php"; -// include __DIR__."/date.php"; -/// diff --git a/test/Schema/DateSchemaTest.php b/test/Schema/DateSchemaTest.php index 71029fe..a12bba8 100644 --- a/test/Schema/DateSchemaTest.php +++ b/test/Schema/DateSchemaTest.php @@ -1,5 +1,8 @@ assertIsObject($dateSchema); } function testStringObjectIsKey() { - $dateSchema = new DateSchema(); - $this->assertArrayHasKey('Date', $dateSchema->check()); + $dateSchema = new class extends DateSchema{}; + $this->assertArrayHasKey('DateValidator', $dateSchema->generate()); } function testDateNowKey() { - $dateSchema = new DateSchema(); - $subset_array = ['Date' => ['now' => true]]; - $this->assertEquals($subset_array, $dateSchema->now()->check()); + $dateSchema = new class extends DateSchema{}; + $subset_array = ['DateValidator' => ['now' => true]]; + $this->assertEquals($subset_array, $dateSchema->now()->generate()); } function testDateTodayKey() { - $dateSchema = new DateSchema(); - $subset_array = ['Date' => ['today' => true]]; - $this->assertEquals($subset_array, $dateSchema->today()->check()); + $dateSchema = new class extends DateSchema{}; + $subset_array = ['DateValidator' => ['today' => true]]; + $this->assertEquals($subset_array, $dateSchema->today()->generate()); } } \ No newline at end of file diff --git a/test/SchemaTest.php b/test/SchemaTest.php index a424238..d0bfecc 100644 --- a/test/SchemaTest.php +++ b/test/SchemaTest.php @@ -83,4 +83,39 @@ function testSchemaNumberErrorObject(){ $this->assertIsNotArray($rules["age"]); $this->assertNotEquals($expected,$rules); } + /* + * Number Schema Test + */ + function testSchemaDateObject(){ + $schema = new Schema(); + $rules = ["date_creat" => $schema->date()->min("now")->max("2022-12-25")->now()->required()->generate()]; + $expected = [ + "date_creat"=>[ + "DateValidator" => + [ + "min" => "now", + "max" => "2022-12-25", + "required"=> true, + "now" => true, + ] + ] + ]; + $this->assertEquals($rules,$expected); + } + function testSchemaDateErrorObject(){ + $schema = new Schema(); + $rules = ["date_creat" => $schema->date()->min("now")->max("2022-12-25")->required()]; + $expected=[ + "date_creat"=>[ + "DateValidator"=>[ + "min"=>"now", + "max"=>"2022-12-25", + "required"=>true, + "positive"=>false, + ] + ] + ]; + $this->assertIsNotArray($rules["date_creat"]); + $this->assertNotEquals($expected,$rules); + } } \ No newline at end of file From aaa72d6319e76e82b71ee20a7581756e2be549aa Mon Sep 17 00:00:00 2001 From: bim-g Date: Fri, 9 Dec 2022 01:18:25 +0200 Subject: [PATCH 19/26] [UPD] set NumberValidator to final --- src/Validator/NumberValidator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Validator/NumberValidator.php b/src/Validator/NumberValidator.php index e12e2d5..4a3b6fd 100644 --- a/src/Validator/NumberValidator.php +++ b/src/Validator/NumberValidator.php @@ -19,7 +19,7 @@ * * @author Boss Ibrahim Mussa */ -class NumberValidator extends ValidatorProvider { +final class NumberValidator extends ValidatorProvider { function __construct(string $string_item,string $value,array $source) { $this->data_source = $source; From fe0ff59fa1fd7ec829fb0e4f8bec146d775ee2f3 Mon Sep 17 00:00:00 2001 From: bim-g Date: Fri, 9 Dec 2022 01:33:49 +0200 Subject: [PATCH 20/26] [ENH] Refactoring boolean validator and schemaprovider --- example/index.php | 4 +- src/Schema/BooleanSchema.php | 27 ++---- src/Script/VBoolean.php | 128 ----------------------------- src/Validator/BooleanValidator.php | 90 ++++++++++++++++++++ src/Validator/DateValidator.php | 16 ++-- 5 files changed, 109 insertions(+), 156 deletions(-) delete mode 100644 src/Script/VBoolean.php create mode 100644 src/Validator/BooleanValidator.php diff --git a/example/index.php b/example/index.php index 93f1922..88247d1 100644 --- a/example/index.php +++ b/example/index.php @@ -21,5 +21,5 @@ // include_once __DIR__."/string.php"; // include __DIR__."/number.php"; - include __DIR__."/date.php"; -// include __DIR__."/boolean.php"; +// include __DIR__."/date.php"; + include __DIR__."/boolean.php"; diff --git a/src/Schema/BooleanSchema.php b/src/Schema/BooleanSchema.php index f75c2f6..b848960 100644 --- a/src/Schema/BooleanSchema.php +++ b/src/Schema/BooleanSchema.php @@ -1,4 +1,8 @@ source = "Boolean"; - $this->schema[$this->source]=[]; + parent::__construct("BooleanValidator"); } function isValid(): BooleanSchema { - $this->schema[$this->source]['isValid']=true; + $this->schema[$this->source]['isValid'] = true; return $this; } - - function required(): BooleanSchema - { - $this->schema[$this->source]['required'] = true; - - return $this; - } - - function check(): array - { - return $this->schema; - } } diff --git a/src/Script/VBoolean.php b/src/Script/VBoolean.php deleted file mode 100644 index c165e09..0000000 --- a/src/Script/VBoolean.php +++ /dev/null @@ -1,128 +0,0 @@ -string_item = $string_item; -// $this->source_data = $source; -// $this->i18n= new i18n($lang); -// if ($this->checkExist()) { -// $this->string_value = $source[$string_item]; -// }; -// } -// -// /** -// * -// * @param string|null $itemKey -// * @return boolean -// */ -// private function checkExist(string $itemKey = null): bool -// { -// $item_to_check = !$itemKey ? $this->string_item:$itemKey ; -// $val = $this->source_data[$item_to_check]; -// -// $regex = "/^(true|false)$/"; -// if (!isset($this->source_data[$item_to_check])) { -// $message = [ -// "type" => "any.unknown", -// "message" => $this->i18n->translate("`%s` is unknown",[$item_to_check]), -// "label" => $item_to_check, -// ]; -// $this->addError($message); -// return false; -// } else if (!preg_match($regex, is_bool($val) ? ($val ? 'true' : 'false') : $val)) { -// $message = [ -// "type" => "boolean.unknown", -// "message" => $this->i18n->translate("`%s` should be a boolean",[$item_to_check]), -// "label" => $item_to_check, -// ]; -// $this->addError($message); -// return false; -// } -// return true; -// } -// -// function required(): SCHBoolean -// { -// $required_value = is_bool($this->string_value); -// if (empty($required_value)) { -// $message = [ -// "type" => "any.required", -// "message" => $this->i18n->translate("`%s` is required",[$this->string_item]), -// "label" => $this->string_item, -// ]; -// $this->addError($message); -// } -// return $this; -// } -// -// function isValid(string $value): SCHBoolean -// { -// $passed_value = is_bool($value) ? ($value ? 'true' : 'false') : $value; -// $required_value = strtolower($passed_value); -// $check = $required_value == 'true' || $required_value == 'false'; -// if (!($check)) { -// $message = [ -// "type" => "boolean.required", -// "message" => $this->i18n->translate("isValid param must be boolean but you put `%s`",[$required_value]), -// "label" => $this->string_item, -// ]; -// $this->addError($message); -// } else { -// // $sting_value= is_bool($this->string_value); -// $incoming_value = $this->string_value ? 'true' : 'false'; -// -// if ($incoming_value != $required_value) { -// $message = [ -// "type" => "boolean.valid", -// "message" => $this->i18n->translate("`%s` is not validValue required. You must put `%s`",[$incoming_value,$required_value]), -// "label" => $this->string_item, -// ]; -// $this->addError($message); -// } -// } -// return $this; -// } -// -// /** -// * -// * @param array $value -// * @return void -// */ -// private function addError(array $value): void -// { -// $this->_errors[] = $value; -// } -// -// /** -// * @return array -// */ -// function check(): array -// { -// return $this->_errors; -// } -//} diff --git a/src/Validator/BooleanValidator.php b/src/Validator/BooleanValidator.php new file mode 100644 index 0000000..e17962e --- /dev/null +++ b/src/Validator/BooleanValidator.php @@ -0,0 +1,90 @@ +field_name = $string_item; + $this->data_source = $source; + $this->field_value = $source[$this->field_name]; + $this->class_provider = "boolean"; + + if ($this->isBoolean()) { + $this->field_value = $source[$string_item]; + }; + parent::__construct(); + } + + public function min($rule){} + public function max($rule){} + + /** + * + * @param string|null $itemKey + * @return boolean + */ + private function isBoolean(string $itemKey = null): bool + { + $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)) { + $message = [ + "type" => "boolean.unknown", + "message" => "`$item_to_check` should be a boolean", + "label" => $item_to_check, + ]; + $this->addError($message); + return false; + } + return true; + } + + public function isValid(string $value) + { + $passed_value = is_bool($value) ? ($value ? 'true' : 'false') : $value; + $required_value = strtolower($passed_value); + $check = $required_value == 'true' || $required_value == 'false'; + if (!($check)) { + $message = [ + "type" => "boolean.required", + "message" => "isValid param must be boolean but you put `$required_value`", + "label" => $this->field_name, + ]; + $this->addError($message); + } else { + // $sting_value= is_bool($this->string_value); + $incoming_value = $this->field_value ? 'true' : 'false'; + + if ($incoming_value != $required_value) { + $message = [ + "type" => "boolean.valid", + "message" => "`$incoming_value` is not validValue required. You must put `$required_value`", + "label" => $this->field_name, + ]; + $this->addError($message); + } + } + } +} diff --git a/src/Validator/DateValidator.php b/src/Validator/DateValidator.php index 0011894..623429c 100644 --- a/src/Validator/DateValidator.php +++ b/src/Validator/DateValidator.php @@ -67,11 +67,11 @@ public function today(string $times=null) } /** - * @param string|null $rule_values + * @param string|null $rule * @return void * get the min date control from the given date */ - public function min($rule_values) + public function min($rule) { /** * $regex= "#[a-zA-Z]#"; @@ -79,8 +79,8 @@ public function min($rule_values) * $con=!$time?$time:(int)$time; * in case the parameters are integers */ - $rule_values = $rule_values ?? "now"; - $min_date_time = strtotime($rule_values); + $rule = $rule ?? "now"; + $min_date_time = strtotime($rule); $min_date = date("d/F/Y",$min_date_time); $date_value_time = strtotime($this->field_value); if ($date_value_time > $min_date_time) { @@ -95,14 +95,14 @@ public function min($rule_values) } /** - * @param string|null $rule_values + * @param string|null $rule * @return void * while try to check maximum date of a defined period use this module */ - public function max($rule_values) + public function max($rule) { - $rule_values = $rule_values ?? "now"; - $max_date_time = strtotime($rule_values); + $rule = $rule ?? "now"; + $max_date_time = strtotime($rule); $max_date = date("d/F/Y",$max_date_time); $date_value_time = strtotime($this->field_value); if ($max_date_time < $date_value_time) { From 68bef97ad151f74bb78f7041e61d6f7a88bd3521 Mon Sep 17 00:00:00 2001 From: bim-g Date: Fri, 9 Dec 2022 01:37:32 +0200 Subject: [PATCH 21/26] [UPD] Refactoring boolean example to be mre clear --- example/boolean.php | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 example/boolean.php diff --git a/example/boolean.php b/example/boolean.php new file mode 100644 index 0000000..c9c9599 --- /dev/null +++ b/example/boolean.php @@ -0,0 +1,22 @@ + false, + 'activated' => "" +]; +//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(), + ]; + +$validate->check($data_source,$rules); +// check if the validation passed or not + var_dump($validate->passed()); + var_dump($validate->errors()); \ No newline at end of file From cc2175f0746a5499d3a731c7e7cc5993f426b801 Mon Sep 17 00:00:00 2001 From: bim-g Date: Fri, 9 Dec 2022 01:38:12 +0200 Subject: [PATCH 22/26] [FIX] Refactoring BooleanSchematest, call the right class module --- test/Schema/BooleanSchemaTest.php | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/test/Schema/BooleanSchemaTest.php b/test/Schema/BooleanSchemaTest.php index f0b0394..d191340 100644 --- a/test/Schema/BooleanSchemaTest.php +++ b/test/Schema/BooleanSchemaTest.php @@ -1,5 +1,8 @@ assertIsObject($booleanSchema); } function testStringObjectIsKey() { - $booleanSchema = new BooleanSchema(); - $this->assertArrayHasKey('Boolean', $booleanSchema->check()); + $booleanSchema = new class extends BooleanSchema{}; + $this->assertArrayHasKey('BooleanValidator', $booleanSchema->generate()); } function testBooleanIsRequireddKey() { - $booleanSchema = new BooleanSchema(); - $subset_array = ['Boolean' => ['required' => true]]; - $this->assertEquals($subset_array, $booleanSchema->required()->check()); + $booleanSchema = new class extends BooleanSchema{}; + $subset_array = ['BooleanValidator' => ['required' => true]]; + $this->assertEquals($subset_array, $booleanSchema->required()->generate()); } function testBooleanIsValidKey() { - $booleanSchema = new BooleanSchema(); - $subset_array = ['Boolean' => ['isValid' => true]]; - $this->assertEquals($subset_array, $booleanSchema->isValid()->check()); + $booleanSchema = new class extends BooleanSchema{}; + $subset_array = ['BooleanValidator' => ['isValid' => true]]; + $this->assertEquals($subset_array, $booleanSchema->isValid()->generate()); } } \ No newline at end of file From ae3c9032115258b319d9d42e26cdbc59706ac794 Mon Sep 17 00:00:00 2001 From: bim-g Date: Fri, 9 Dec 2022 01:40:06 +0200 Subject: [PATCH 23/26] [FIX] change module extends from schema module class --- src/Schema.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Schema.php b/src/Schema.php index 14aad78..40af89f 100644 --- a/src/Schema.php +++ b/src/Schema.php @@ -48,7 +48,9 @@ public function date(): DateSchema */ public function boolean(): BooleanSchema { - return new BooleanSchema(); + return new class extends BooleanSchema{}; } + //TODO add support to array validation + // TODO add support for file validation } \ No newline at end of file From d862d03bfdb0d95f9c661bd06ac5ca27b16aa746 Mon Sep 17 00:00:00 2001 From: bim-g Date: Fri, 9 Dec 2022 01:40:48 +0200 Subject: [PATCH 24/26] [UPD] remove useless module --- src/Providers/SChemaProvider.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Providers/SChemaProvider.php b/src/Providers/SChemaProvider.php index 78efccd..30216ec 100644 --- a/src/Providers/SChemaProvider.php +++ b/src/Providers/SChemaProvider.php @@ -25,7 +25,7 @@ function __construct(string $type) */ public function min(int $rule): SChemaProvider { - $this->schema[$this->source]["min"]=$rule; + $this->schema[$this->source]["min"] = $rule; return $this; } @@ -35,13 +35,13 @@ public function min(int $rule): SChemaProvider */ public function max($rule): SChemaProvider { - $this->schema[$this->source]["max"]=$rule; + $this->schema[$this->source]["max"] = $rule; return $this; } function required(): SChemaProvider { - $this->schema[$this->source]["required"]=true; + $this->schema[$this->source]["required"] = true; return $this; } From a45f5459bec86c3f882fd1606960dd6558be3095 Mon Sep 17 00:00:00 2001 From: bim-g Date: Fri, 9 Dec 2022 01:41:41 +0200 Subject: [PATCH 25/26] [UPD] setup date to example module for better test --- .idea/wepesi_validation.iml | 2 +- .phpunit.result.cache | 2 +- README.md | 32 ++++++++++++++++++++------------ example/date.php | 20 ++++++++++++++++++++ 4 files changed, 42 insertions(+), 14 deletions(-) create mode 100644 example/date.php diff --git a/.idea/wepesi_validation.iml b/.idea/wepesi_validation.iml index 9c9aef3..14cfa83 100644 --- a/.idea/wepesi_validation.iml +++ b/.idea/wepesi_validation.iml @@ -5,7 +5,7 @@ - + diff --git a/.phpunit.result.cache b/.phpunit.result.cache index 6f2affb..32ef5a9 100644 --- a/.phpunit.result.cache +++ b/.phpunit.result.cache @@ -1 +1 @@ -{"version":1,"defects":{"Test\\Schema\\SCHStringTest::testStringObject":4,"Test\\Schema\\SCHStringTest::testStringObjectIsNotKey":3,"Test\\Schema\\SCHStringTest::testStringEmailKey":3,"Test\\Schema\\SCHStringTest::testStringObjectIsKey":4,"Test\\Schema\\StringSchemaTest::testStringURLKey":4,"Test\\Schema\\StringSchemaTest::testStringMatchKey":4,"Test\\Schema\\StringSchemaTest::testStringMinimumKey":4,"Test\\Schema\\NumberSchemaTest::testStringIsObject":4,"Test\\Schema\\NumberSchemaTest::testRequiredKey":4,"Test\\Schema\\DateSchemaTest::testDateTodayKey":4,"Test\\Schema\\BooleanSchemaTest::testBooleanIsRequireddKey":4,"Test\\Script\\StringValidationTest::testStringIsObject":4,"Test\\Script\\StringValidationTest::testStringObjectException":4,"Test\\Script\\StringValidationTest::testException":3,"Test\\Script\\StringValidationTest::testStringMin":3,"Test\\Script\\StringValidationTest::testStringMaxError":4,"Test\\Script\\StringValidationTest::testStringMaxSuccess":3,"Test\\Script\\StringValidationTest::testStringValidationSourceDataException":4,"Test\\Script\\StringValidationTest::testStringValidationSchemaException":4,"Test\\Script\\StringValidationTest::testStringMinError":4,"Test\\Script\\StringValidationTest::testStringMinSuccess":3,"Test\\Script\\StringValidationTest::testStringEmailError":3,"Test\\Script\\StringValidationTest::testStringURLSuccess":4,"Test\\Script\\StringValidationTest::testStringRequiredSuccess":4,"Test\\Script\\StringValidationTest::testStringRequiredErrors":3,"Test\\Script\\StringValidationTest::testStringRequiredErrorsUnknown":4,"Test\\Script\\StringValidationTest::testStringMatchSuccess":3,"Test\\Script\\StringValidationTest::testStringMatchErrors":3,"Test\\Schema\\StringSchemaTest::testStringIsObject":4,"Test\\Schema\\StringSchemaTest::testStringObjectIsKey":4,"Test\\Schema\\StringSchemaTest::testStringEmailKey":4,"Test\\Schema\\StringSchemaTest::testStringMaximumKey":4,"Test\\Schema\\DateSchemaTest::testStringIsObject":4,"Test\\Schema\\DateSchemaTest::testStringObjectIsKey":4,"Test\\Schema\\DateSchemaTest::testDateNowKey":4,"Test\\Schema\\NumberSchemaTest::testStringObjectIsKey":4,"Test\\Schema\\NumberSchemaTest::testNumberPositiveKey":4,"Test\\Schema\\BooleanSchemaTest::testBooleanIsObject":4,"Test\\Schema\\BooleanSchemaTest::testStringObjectIsKey":4,"Test\\Schema\\BooleanSchemaTest::testBooleanIsValidKey":3,"Test\\ValidationTest::testValidationStringObject":3,"Test\\ValidationTest::testValidationStringErrorObject":3},"times":{"Test\\Schema\\SCHStringTest::testStringObject":0.021,"Test\\Schema\\SCHStringTest::testStringObjectIsKey":0,"Test\\Schema\\SCHStringTest::testStringObjectIsNotKey":0.01,"Test\\Schema\\SCHStringTest::testStringEmailKey":0.001,"Test\\Schema\\SCHStringTest::testStringIsObject":0.008,"Test\\Schema\\StringSchemaTest::testStringIsObject":0.005,"Test\\Schema\\StringSchemaTest::testStringObjectIsKey":0,"Test\\Schema\\StringSchemaTest::testStringEmailKey":0,"Test\\Schema\\StringSchemaTest::testStringURLKey":0,"Test\\Schema\\StringSchemaTest::testStringMatchKey":0,"Test\\Schema\\StringSchemaTest::testStringMinimumKey":0,"Test\\Schema\\StringSchemaTest::testStringMaximumKey":0,"Test\\Schema\\NumberSchemaTest::testStringIsObject":0.005,"Test\\Schema\\NumberSchemaTest::testStringObjectIsKey":0,"Test\\Schema\\NumberSchemaTest::testRequiredKey":0,"Test\\Schema\\NumberSchemaTest::testNumberPositiveKey":0,"Test\\Schema\\DateSchemaTest::testStringIsObject":0.006,"Test\\Schema\\DateSchemaTest::testStringObjectIsKey":0,"Test\\Schema\\DateSchemaTest::testDateNowKey":0,"Test\\Schema\\DateSchemaTest::testDateTodayKey":0,"Test\\Schema\\BooleanSchemaTest::testBooleanIsObject":0.005,"Test\\Schema\\BooleanSchemaTest::testStringObjectIsKey":0,"Test\\Schema\\BooleanSchemaTest::testBooleanIsRequireddKey":0,"Test\\Schema\\BooleanSchemaTest::testBooleanIsValidKey":0,"Test\\Script\\StringValidationTest::testStringIsObject":0.001,"Test\\Script\\StringValidationTest::testStringObjectException":0.006,"Test\\Script\\StringValidationTest::testException":0.007,"Test\\Script\\StringValidationTest::testStringMin":0.001,"Test\\Script\\StringValidationTest::testStringValidationException":0.006,"Test\\Script\\StringValidationTest::testStringValidationSchemaException":0,"Test\\Script\\StringValidationTest::testStringValidationSourceDataException":0.007,"Test\\Script\\StringValidationTest::testStringMinError":0.001,"Test\\Script\\StringValidationTest::testStringMinSuccess":0,"Test\\Script\\StringValidationTest::testStringMaxError":0,"Test\\Script\\StringValidationTest::testStringMaxSuccess":0,"Test\\Script\\StringValidationTest::testStringEmailError":0.001,"Test\\Script\\StringValidationTest::testStringEmailSuccess":0,"Test\\Script\\StringValidationTest::testStringUrlError":0.008,"Test\\Script\\StringValidationTest::testStringURLError":0,"Test\\Script\\StringValidationTest::testStringURLSuccess":0,"Test\\Script\\StringValidationTest::testStringRequiredSuccess":0,"Test\\Script\\StringValidationTest::testStringRequiredErrors":0,"Test\\Script\\StringValidationTest::testStringRequiredErrorsUnknow":0,"Test\\Script\\StringValidationTest::testStringRequiredErrorsUnknown":0,"Test\\Script\\StringValidationTest::testStringMatchErrors":0,"Test\\Script\\StringValidationTest::testStringMatchSuccess":0,"Test\\Script\\StringValidationTest::testStringValidationFieldNotDefinedException":0,"Test\\ValidationTest::testValidationObject":0.016,"Test\\ValidationTest::testValidationStringObject":0.005,"Test\\ValidationTest::testValidationStringErrorObject":0.262}} \ No newline at end of file +{"version":1,"defects":{"Test\\Schema\\SCHStringTest::testStringObject":4,"Test\\Schema\\SCHStringTest::testStringObjectIsNotKey":3,"Test\\Schema\\SCHStringTest::testStringEmailKey":3,"Test\\Schema\\SCHStringTest::testStringObjectIsKey":4,"Test\\Schema\\StringSchemaTest::testStringURLKey":3,"Test\\Schema\\StringSchemaTest::testStringMatchKey":3,"Test\\Schema\\StringSchemaTest::testStringMinimumKey":3,"Test\\Schema\\NumberSchemaTest::testStringIsObject":4,"Test\\Schema\\NumberSchemaTest::testRequiredKey":3,"Test\\Schema\\DateSchemaTest::testDateTodayKey":3,"Test\\Schema\\BooleanSchemaTest::testBooleanIsRequireddKey":3,"Test\\Script\\StringValidationTest::testStringIsObject":4,"Test\\Script\\StringValidationTest::testStringObjectException":4,"Test\\Script\\StringValidationTest::testException":3,"Test\\Script\\StringValidationTest::testStringMin":3,"Test\\Script\\StringValidationTest::testStringMaxError":4,"Test\\Script\\StringValidationTest::testStringMaxSuccess":4,"Test\\Script\\StringValidationTest::testStringValidationSourceDataException":4,"Test\\Script\\StringValidationTest::testStringValidationSchemaException":4,"Test\\Script\\StringValidationTest::testStringMinError":4,"Test\\Script\\StringValidationTest::testStringMinSuccess":4,"Test\\Script\\StringValidationTest::testStringEmailError":4,"Test\\Script\\StringValidationTest::testStringURLSuccess":4,"Test\\Script\\StringValidationTest::testStringRequiredSuccess":4,"Test\\Script\\StringValidationTest::testStringRequiredErrors":4,"Test\\Script\\StringValidationTest::testStringRequiredErrorsUnknown":4,"Test\\Script\\StringValidationTest::testStringMatchSuccess":4,"Test\\Script\\StringValidationTest::testStringMatchErrors":4,"Test\\Schema\\StringSchemaTest::testStringIsObject":4,"Test\\Schema\\StringSchemaTest::testStringObjectIsKey":3,"Test\\Schema\\StringSchemaTest::testStringEmailKey":3,"Test\\Schema\\StringSchemaTest::testStringMaximumKey":3,"Test\\Schema\\DateSchemaTest::testStringIsObject":4,"Test\\Schema\\DateSchemaTest::testStringObjectIsKey":3,"Test\\Schema\\DateSchemaTest::testDateNowKey":3,"Test\\Schema\\NumberSchemaTest::testStringObjectIsKey":3,"Test\\Schema\\NumberSchemaTest::testNumberPositiveKey":3,"Test\\Schema\\BooleanSchemaTest::testBooleanIsObject":4,"Test\\Schema\\BooleanSchemaTest::testStringObjectIsKey":3,"Test\\Schema\\BooleanSchemaTest::testBooleanIsValidKey":3,"Test\\ValidationTest::testValidationStringObject":3,"Test\\ValidationTest::testValidationStringErrorObject":3,"Test\\Script\\StringValidationTest::testStringValidationFieldNotDefinedException":4,"Test\\Script\\StringValidationTest::testStringEmailSuccess":4,"Test\\Script\\StringValidationTest::testStringURLError":4,"Test\\SchemaTest::testSchemaStringObject":3,"Test\\SchemaTest::testSchemaNumberObject":3,"Test\\SchemaTest::testSchemaDateObject":3},"times":{"Test\\Schema\\SCHStringTest::testStringObject":0.021,"Test\\Schema\\SCHStringTest::testStringObjectIsKey":0,"Test\\Schema\\SCHStringTest::testStringObjectIsNotKey":0.01,"Test\\Schema\\SCHStringTest::testStringEmailKey":0.001,"Test\\Schema\\SCHStringTest::testStringIsObject":0.008,"Test\\Schema\\StringSchemaTest::testStringIsObject":0.007,"Test\\Schema\\StringSchemaTest::testStringObjectIsKey":0.001,"Test\\Schema\\StringSchemaTest::testStringEmailKey":0.001,"Test\\Schema\\StringSchemaTest::testStringURLKey":0,"Test\\Schema\\StringSchemaTest::testStringMatchKey":0,"Test\\Schema\\StringSchemaTest::testStringMinimumKey":0,"Test\\Schema\\StringSchemaTest::testStringMaximumKey":0,"Test\\Schema\\NumberSchemaTest::testStringIsObject":0.007,"Test\\Schema\\NumberSchemaTest::testStringObjectIsKey":0,"Test\\Schema\\NumberSchemaTest::testRequiredKey":0.001,"Test\\Schema\\NumberSchemaTest::testNumberPositiveKey":0,"Test\\Schema\\DateSchemaTest::testStringIsObject":0.009,"Test\\Schema\\DateSchemaTest::testStringObjectIsKey":0.008,"Test\\Schema\\DateSchemaTest::testDateNowKey":0.001,"Test\\Schema\\DateSchemaTest::testDateTodayKey":0,"Test\\Schema\\BooleanSchemaTest::testBooleanIsObject":0.007,"Test\\Schema\\BooleanSchemaTest::testStringObjectIsKey":0.001,"Test\\Schema\\BooleanSchemaTest::testBooleanIsRequireddKey":0.001,"Test\\Schema\\BooleanSchemaTest::testBooleanIsValidKey":0,"Test\\Script\\StringValidationTest::testStringIsObject":0.001,"Test\\Script\\StringValidationTest::testStringObjectException":0.006,"Test\\Script\\StringValidationTest::testException":0.007,"Test\\Script\\StringValidationTest::testStringMin":0.001,"Test\\Script\\StringValidationTest::testStringValidationException":0.006,"Test\\Script\\StringValidationTest::testStringValidationSchemaException":0.001,"Test\\Script\\StringValidationTest::testStringValidationSourceDataException":0.076,"Test\\Script\\StringValidationTest::testStringMinError":0.001,"Test\\Script\\StringValidationTest::testStringMinSuccess":0.001,"Test\\Script\\StringValidationTest::testStringMaxError":0.001,"Test\\Script\\StringValidationTest::testStringMaxSuccess":0.001,"Test\\Script\\StringValidationTest::testStringEmailError":0.001,"Test\\Script\\StringValidationTest::testStringEmailSuccess":0.001,"Test\\Script\\StringValidationTest::testStringUrlError":0.008,"Test\\Script\\StringValidationTest::testStringURLError":0.001,"Test\\Script\\StringValidationTest::testStringURLSuccess":0.001,"Test\\Script\\StringValidationTest::testStringRequiredSuccess":0.001,"Test\\Script\\StringValidationTest::testStringRequiredErrors":0.001,"Test\\Script\\StringValidationTest::testStringRequiredErrorsUnknow":0,"Test\\Script\\StringValidationTest::testStringRequiredErrorsUnknown":0.001,"Test\\Script\\StringValidationTest::testStringMatchErrors":0.001,"Test\\Script\\StringValidationTest::testStringMatchSuccess":0.001,"Test\\Script\\StringValidationTest::testStringValidationFieldNotDefinedException":0.001,"Test\\ValidationTest::testValidationObject":0.016,"Test\\ValidationTest::testValidationStringObject":0.005,"Test\\ValidationTest::testValidationStringErrorObject":0.262,"Test\\SchemaTest::testSchemaObject":0.008,"Test\\SchemaTest::testSchemaStringObject":0.002,"Test\\SchemaTest::testSchemaStringErrorObject":0.003,"Test\\SchemaTest::testSchemaNumberObject":0.007,"Test\\SchemaTest::testSchemaNumberErrorObject":0.011,"Test\\SchemaTest::testSchemaDateErrorObject":0.012,"Test\\SchemaTest::testSchemaDateObject":0.007}} \ No newline at end of file diff --git a/README.md b/README.md index ec3f8db..41a66f2 100644 --- a/README.md +++ b/README.md @@ -3,10 +3,14 @@ this module will help to do your own input validation from http request `POST` o # INTEGRATION The integration is the simple thing to do. -First you neeed to create a new instance of `Validate` whitch will be use to do our validation. +First you need to define the rule of the input data, and easy way to do so is by using a schema model which help hundle all of the process, +then create an instance of `Validate` which will help validate data input according to rules already defined. While have the instance of validation, you can access `check` method, with take two parameters, the `source` and `rules`; ```php - $valid=new Validate($source); + $valid = new \Wepesi\App\Validate(); + $schema = new \Wepesi\App\Schema(); + $source = []; + $rules = ["name" => $schema->string()->min(3)->max(5)->generate()]; $valid->check($source,$rules); ``` * `source` @@ -28,24 +32,27 @@ different method are now available according to you need. now you can validate your keys according to a specify type witch are: - string - number - - ... + - date, + - boolean + - file ```php // rules $rules=[ - "email"=>$valid->string("name")->email()->min(9)->max(50)->required()->check(), - "year"=>$valid->number("year")->email()->min(35)->max(60)->required()->check() + "email"=>$schema->string()->email()->min(9)->max(50)->required()->generate(), + "year"=>$schema->number()->email()->min(35)->max(60)->required()->generate() ]; ``` in the example bellow, for the first rule ```php - "email"=>$valid->string("name")->email()->min(9)->max(50)->required()->check() + "email"=>$schema->string()->email()->min(9)->max(50)->required()->generate() // check `email` keys should be a: // - string: type of the value to be check should be a string // - email: that string should be a email // - min:9=> the email should have minimum caracters 9 caracter - // - min:9=> the email should have maximum caracters should exid 50 caracters + // - max:50=> the email should have maximum caracters should exid 50 caracters + // - required=> it will no be empty ``` `STRING` method allow to validation: @@ -65,12 +72,13 @@ In the example bellow, you can see a complete procured on how to validate data-s "link"=>"https://github.com/bim-g/wepesi_validation/", "age"=>1 ]; -$valid=new Validate($source); +$valid = new \Wepesi\App\Validate(); +$schema = new \Wepesi\App\Schema(); $rules=[ - "name"=>$validate->string("name")->required()->min(3)->max(30)->check(), - "email"=>$validate->string("email")->required()->min(3)->max(60)->email()->check(), - "link"=>$validate->string("link")->required()->min(3)->max(60)->url()->check(), - "age"=>$validate->number("age")->required()->positive()->check() + "name"=>$schema->string()->required()->min(3)->max(30)->generate(), + "email"=>$schema->string()->required()->min(3)->max(60)->email()->generate(), + "link"=>$schema->string()->required()->min(3)->max(60)->url()->generate(), + "age"=>$schema->number()->required()->positive()->generate() ]; $valid->check($source,$rules); diff --git a/example/date.php b/example/date.php new file mode 100644 index 0000000..529a86d --- /dev/null +++ b/example/date.php @@ -0,0 +1,20 @@ + '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); + + var_dump($validate->passed()); + var_dump($validate->errors()); \ No newline at end of file From 16e96b206ca58d7d55c1650cd79c038ae9160e11 Mon Sep 17 00:00:00 2001 From: bim-g Date: Fri, 9 Dec 2022 01:45:59 +0200 Subject: [PATCH 26/26] [UPD] remove useless field value on constructor, it will be get from data_source. --- src/Validate.php | 2 +- src/Validator/BooleanValidator.php | 6 +++--- src/Validator/DateValidator.php | 6 +++--- src/Validator/NumberValidator.php | 10 +++++----- src/Validator/StringValidator.php | 9 ++++----- 5 files changed, 16 insertions(+), 17 deletions(-) diff --git a/src/Validate.php b/src/Validate.php index e3dd6bc..c3d57e9 100644 --- a/src/Validate.php +++ b/src/Validate.php @@ -60,7 +60,7 @@ function check(array $resource, array $schema){ $reflexion = new ReflectionClass($class_name); - $instance = $reflexion->newInstance($item, $value,$resource); + $instance = $reflexion->newInstance($item,$resource); foreach ($rules[$key] as $method => $params){ if(method_exists($instance,$method)){ diff --git a/src/Validator/BooleanValidator.php b/src/Validator/BooleanValidator.php index e17962e..b5e4b28 100644 --- a/src/Validator/BooleanValidator.php +++ b/src/Validator/BooleanValidator.php @@ -22,15 +22,15 @@ final class BooleanValidator extends ValidatorProvider { - function __construct(string $string_item,string $value,array $source) + function __construct(string $item, array $source) { - $this->field_name = $string_item; + $this->field_name = $item; $this->data_source = $source; $this->field_value = $source[$this->field_name]; $this->class_provider = "boolean"; if ($this->isBoolean()) { - $this->field_value = $source[$string_item]; + $this->field_value = $source[$item]; }; parent::__construct(); } diff --git a/src/Validator/DateValidator.php b/src/Validator/DateValidator.php index 623429c..035c44e 100644 --- a/src/Validator/DateValidator.php +++ b/src/Validator/DateValidator.php @@ -17,10 +17,10 @@ final class DateValidator extends ValidatorProvider { - public function __construct(string $item,string $value,array $source ) { - $this->field_value = $source[$item]; + public function __construct(string $item,array $data_source ) { $this->field_name = $item; - $this->data_source = $source; + $this->field_value = $data_source[$item]; + $this->data_source = $data_source; $this->class_provider = "date"; $this->checkExist(); parent::__construct(); diff --git a/src/Validator/NumberValidator.php b/src/Validator/NumberValidator.php index 4a3b6fd..f7552a8 100644 --- a/src/Validator/NumberValidator.php +++ b/src/Validator/NumberValidator.php @@ -21,13 +21,13 @@ */ final class NumberValidator extends ValidatorProvider { - function __construct(string $string_item,string $value,array $source) { - $this->data_source = $source; - $this->field_name = $string_item; - $this->field_value= $value; + function __construct(string $item, array $data_source) { + $this->data_source = $data_source; + $this->field_name = $item; + $this->field_value = $data_source[$item]; $this->class_provider = "number"; if($this->isNumber()){ - $this->field_value = $source[$string_item]; + $this->field_value = $data_source[$item]; } parent::__construct(); } diff --git a/src/Validator/StringValidator.php b/src/Validator/StringValidator.php index deb6070..b1a2349 100644 --- a/src/Validator/StringValidator.php +++ b/src/Validator/StringValidator.php @@ -16,15 +16,14 @@ final class StringValidator extends ValidatorProvider { /** * - * @param string $item the item to be validated. - * @param string $value the value of the item - * @param array $data_source the source data from where is going to check it the match key exist and have value. + * @param string $item the item to be validated. + * @param array $data_source the source data from where is going to check it the match key exist and have value. */ - public function __construct(string $item, string $value, array $data_source=[]) { + public function __construct(string $item, array $data_source=[]) { $this->errors = []; $this->data_source = $data_source; $this->field_name = $item; - $this->field_value = $value; + $this->field_value = $data_source[$item]; $this->class_provider = "string"; parent::__construct(); }