From 89802e5031b8b404fdbe653a0f0ee5b9f0791bd3 Mon Sep 17 00:00:00 2001 From: bim-g Date: Wed, 29 Dec 2021 22:45:07 +0200 Subject: [PATCH 1/3] [FEAT][TRA] add tranlate class module --- .idea/php.xml | 7 ++++++- .idea/wepesi_validation.iml | 7 ++++++- composer.json | 4 +++- index.php | 1 + lang/en/language.php | 4 ++++ lang/fr/language.php | 10 +++++++++ .../configs/wepesi_validation.properties | 0 nbproject/project.properties | 20 ------------------ nbproject/project.xml | 10 --------- shared/global.php | 5 +++++ src/Escape.php | 21 +++++++++++++++++++ src/VString.php | 14 ++++++------- test/index.php | 4 ++-- 13 files changed, 65 insertions(+), 42 deletions(-) create mode 100644 lang/en/language.php create mode 100644 lang/fr/language.php delete mode 100644 nbproject/configs/wepesi_validation.properties delete mode 100644 nbproject/project.properties delete mode 100644 nbproject/project.xml create mode 100644 shared/global.php create mode 100644 src/Escape.php diff --git a/.idea/php.xml b/.idea/php.xml index f6159c8..3b1bcf5 100644 --- a/.idea/php.xml +++ b/.idea/php.xml @@ -1,4 +1,9 @@ - + + + + + + \ No newline at end of file diff --git a/.idea/wepesi_validation.iml b/.idea/wepesi_validation.iml index c956989..1ee89a6 100644 --- a/.idea/wepesi_validation.iml +++ b/.idea/wepesi_validation.iml @@ -1,7 +1,12 @@ - + + + + + + diff --git a/composer.json b/composer.json index 938fc09..609da5c 100644 --- a/composer.json +++ b/composer.json @@ -18,5 +18,7 @@ "Wepesi\\app\\":"src/" } }, - "require": {} + "require": { + "php": ">=7.4" + } } diff --git a/index.php b/index.php index 160ad47..4ae3eb2 100644 --- a/index.php +++ b/index.php @@ -1,4 +1,5 @@ "hello" +]; \ No newline at end of file diff --git a/lang/fr/language.php b/lang/fr/language.php new file mode 100644 index 0000000..e89a9fd --- /dev/null +++ b/lang/fr/language.php @@ -0,0 +1,10 @@ +"`%s` doit avoir un minimum de `%s` caracteres", + "`%s` should have maximum of `%s` caracters"=>"`%s` doit avoir un maximum de `%s` caracteres", + "`%s` this should be an email"=>"`%s` doit etre un email", + "`%s` this should be a link(url)"=>"`%s` doit avoir un lien(url) ", + "`%s` should match %s"=>"`%s` doit correpondre a `%s`", + "`%s` is required"=>"`%s` est obligatoire", + "`%s` should be a string"=>"`%s` doit est une chaine de caractere", +]; \ No newline at end of file diff --git a/nbproject/configs/wepesi_validation.properties b/nbproject/configs/wepesi_validation.properties deleted file mode 100644 index e69de29..0000000 diff --git a/nbproject/project.properties b/nbproject/project.properties deleted file mode 100644 index f83c2b3..0000000 --- a/nbproject/project.properties +++ /dev/null @@ -1,20 +0,0 @@ -auxiliary.org-netbeans-modules-css-prep.less_2e_compiler_2e_options= -auxiliary.org-netbeans-modules-css-prep.less_2e_enabled=false -auxiliary.org-netbeans-modules-css-prep.less_2e_mappings=/less:/css -auxiliary.org-netbeans-modules-css-prep.sass_2e_compiler_2e_options= -auxiliary.org-netbeans-modules-css-prep.sass_2e_enabled=false -auxiliary.org-netbeans-modules-css-prep.sass_2e_mappings=/scss:/css -auxiliary.org-netbeans-modules-javascript2-requirejs.enabled=true -auxiliary.org-netbeans-modules-web-clientproject-api.js_2e_libs_2e_folder=js/libs -browser.reload.on.save=true -code.analysis.excludes= -ignore.path= -include.path=\ - ${php.global.include.path} -php.version=PHP_73 -source.encoding=UTF-8 -src.dir=. -tags.asp=false -tags.short=false -testing.providers= -web.root=. diff --git a/nbproject/project.xml b/nbproject/project.xml deleted file mode 100644 index af5763e..0000000 --- a/nbproject/project.xml +++ /dev/null @@ -1,10 +0,0 @@ - - - org.netbeans.modules.php.project - - - wepesi_validation - - - - diff --git a/shared/global.php b/shared/global.php new file mode 100644 index 0000000..ef606ef --- /dev/null +++ b/shared/global.php @@ -0,0 +1,5 @@ +0){ + $key_value=!isset($language[$message])?null:$language[$message]; + $message_key=$key_value!=null?vsprintf($key_value,$data):vsprintf($message,$data); + } + return $message_key; + } +} \ No newline at end of file diff --git a/src/VString.php b/src/VString.php index bf0730f..ec0d62c 100644 --- a/src/VString.php +++ b/src/VString.php @@ -43,7 +43,7 @@ function min(int $rule_values=0){ if (strlen($this->string_value) < $min) { $message=[ "type"=>"string.min", - "message"=> "`{$this->string_item}` should have minimum of `{$min}` caracters", + "message"=> i18n::translate("`%s` should have minimum of `%s` caracters",[$this->string_item,$min]), "label"=>$this->string_item, "limit"=>$min ]; @@ -62,7 +62,7 @@ function max(int $rule_values=1){ if (strlen($this->string_value) > $max) { $message = [ "type" => "string.max", - "message" => "`{$this->string_item}` should have maximum of `{$max}` caracters", + "message" => i18n::translate("`%s` should have maximum of `%s` caracters",[$this->string_item,$max]), "label" => $this->string_item, "limit" => $max ]; @@ -78,7 +78,7 @@ function email(){ if (!filter_var($this->string_value, FILTER_VALIDATE_EMAIL)) { $message = [ "type" => "string.email", - "message" => "`{$this->string_item}` this should be an email", + "message" => i18n::translate("`%s` this should be an email",[$this->string_item]), "label" => $this->string_item, ]; $this->addError($message); @@ -93,7 +93,7 @@ function url(){ if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i", $this->string_value)) { $message = [ "type" => "string.url", - "message" => "`{$this->string_item}` this shoudl be a link(url)", + "message" => i18n::translate("`{$this->string_item}` this should be a link(url)"), "label" => $this->string_item, ]; $this->addError($message); @@ -110,7 +110,7 @@ function match(string $key_tomatch){ if (isset($this->source_data[$key_tomatch]) && (strlen($this->string_value)!= strlen($this->source_data[$key_tomatch])) && ($this->string_value!=$this->source_data[$key_tomatch])) { $message = [ "type" => "string.match", - "message" => "`{$this->string_item}` should match {$key_tomatch}", + "message" => i18n::translate("`%s` should match %s",[$this->string_item,$key_tomatch]), "label" => $this->string_item, ]; $this->addError($message); @@ -122,7 +122,7 @@ function required(){ if (empty($required_value)) { $message = [ "type"=> "any.required", - "message" => "`{$this->string_item}` is required", + "message" => i18n::translate("`%s` is required",[$this->string_item]), "label" => $this->string_item, ]; $this->addError($message); @@ -149,7 +149,7 @@ private function checkExist(string $itemKey=null){ }else if(!preg_match($regex,$this->source_data[$item_to_check]) || strlen(trim($this->source_data[$item_to_check]))==0){ $message=[ "type" => "string.unknow", - "message" => "`{$item_to_check}` shoud be a s tring", + "message" => i18n::translate("`%s` should be a string",[$item_to_check]), "label" => $item_to_check, ]; $this->addError($message); diff --git a/test/index.php b/test/index.php index e1b6107..aaa4e86 100644 --- a/test/index.php +++ b/test/index.php @@ -16,7 +16,7 @@ "date_created"=>"2021-05-23" ]; $valid=new Validate($source); -// include "./test/string.php"; + include "./test/string.php"; // include "./test/number.php"; // include "./test/boolean.php"; - include "./test/date.php"; +// include "./test/date.php"; From 5496d99442af107d15c853174e17cc5c602ce45a Mon Sep 17 00:00:00 2001 From: bim-g Date: Sun, 2 Jan 2022 13:45:19 +0200 Subject: [PATCH 2/3] [UPD] update translation for vnumber --- lang/fr/language.php | 12 ++++-- src/VNumber.php | 80 ++++++++++++++++++++++++++---------- src/VString.php | 50 +++++++++++++--------- src/{Escape.php => i18n.php} | 0 test/index.php | 4 +- 5 files changed, 100 insertions(+), 46 deletions(-) rename src/{Escape.php => i18n.php} (100%) diff --git a/lang/fr/language.php b/lang/fr/language.php index e89a9fd..2d8868e 100644 --- a/lang/fr/language.php +++ b/lang/fr/language.php @@ -1,10 +1,16 @@ "`%s` doit avoir un minimum de `%s` caracteres", - "`%s` should have maximum of `%s` caracters"=>"`%s` doit avoir un maximum de `%s` caracteres", + "`%s` should have minimum of `%s` characters"=>"`%s` doit avoir un minimum de `%s` caractèress", + "`%s` should have maximum of `%s` characters"=>"`%s` doit avoir un maximum de `%s` caractèress", "`%s` this should be an email"=>"`%s` doit etre un email", "`%s` this should be a link(url)"=>"`%s` doit avoir un lien(url) ", "`%s` should match %s"=>"`%s` doit correpondre a `%s`", "`%s` is required"=>"`%s` est obligatoire", - "`%s` should be a string"=>"`%s` doit est une chaine de caractere", + "`%s` should be a string"=>"`%s` doit est une chaine de caractères", + "`%s` should be greater than `%s`"=>"`%s` devrait être supérieur à `%s`", + "`%s` should be less than `%s`"=>"`%s` devrait être inférieur à `%s`", + "`%s` should be a positive number"=>"`%s` devrait être un nombre positif", + "`%s` is unknow"=>"`%s` devrait être inférieur à `%s`", + "`%s` should be less than `%s`"=>"`%s` devrait être inférieur à `%s`", + "`%s` should be less than `%s`"=>"`%s` devrait être inférieur à `%s`", ]; \ No newline at end of file diff --git a/src/VNumber.php b/src/VNumber.php index 6c30f66..3839bec 100644 --- a/src/VNumber.php +++ b/src/VNumber.php @@ -16,11 +16,11 @@ class VNumber { //put your code here private $string_value; - private $string_item; - private $source_data; - private $_errors; - private $_min; - private $_max; + private string $string_item; + private array $source_data; + private array $_errors; + private int $_min; + private int $_max; function __construct(array $source,string $string_item) { $this->source_data=$source; @@ -30,11 +30,17 @@ function __construct(array $source,string $string_item) { $this->string_value=$source[$string_item]; } } - function min(int $min_values){ + + /** + * @param int $min_values + * @return $this + */ + function min(int $min_values): VNumber + { if ((int) $this->string_value < $min_values) { $message=[ "type"=>"number.min", - "message"=> "`{$this->string_item}` should be greater than `{$min_values}`", + "message"=> i18n::translate("`%s` should be greater than `%s`",[$this->string_item,$min_values]), "label"=>$this->string_item, "limit"=>$min_values ]; @@ -42,11 +48,17 @@ function min(int $min_values){ } return $this; } - function max(int $min_values){ + + /** + * @param int $min_values + * @return $this + */ + function max(int $min_values): VNumber + { if ((int) $this->string_value > $min_values) { $message=[ "type"=>"number.max", - "message"=> "`{$this->string_item}` should be less than `{$min_values}`", + "message"=> i18n::translate("`%s` should be less than `%s`",[$this->string_item,$min_values]), "label"=>$this->string_item, "limit"=>$min_values ]; @@ -54,11 +66,17 @@ function max(int $min_values){ } return $this; } - function positive(int $min_values){ + + /** + * @param int $min_values + * @return $this + */ + function positive(int $min_values): VNumber + { if ((int) $this->string_value < 0) { $message=[ "type"=>"number.positive", - "message"=> "`{$this->string_item}` should be a positive number", + "message"=> i18n::translate("`%s` should be a positive number",[$this->string_item]), "label"=>$this->string_item, "limit"=>1 ]; @@ -66,27 +84,37 @@ function positive(int $min_values){ } return $this; } - function required(){ + + /** + * @return $this + */ + function required(): VNumber + { $required_value= trim($this->string_value); if (empty($required_value)) { $message = [ "type"=> "any.required", - "message" => "`{$this->string_item}` is required", + "message" => i18n::translate("`%s` is required",[$this->string_item]), "label" => $this->string_item, ]; $this->addError($message); } return $this; } -// - private function checkExist(string $itemKey=null){ - $item_to_check=$itemKey?$itemKey:$this->string_item; + + /** + * @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.unknow", - "message" => "`{$item_to_check}` is unknow", + "message" => i18n::translate("`%s` is unknow",[$item_to_check]), "label" => $item_to_check, ]; $this->addError($message); @@ -94,7 +122,7 @@ private function checkExist(string $itemKey=null){ }else if (preg_match($regex_string,trim($this->source_data[$item_to_check])) || !is_integer($this->source_data[$item_to_check])) { $message = [ "type"=> "number.unknow", - "message" => "`{$item_to_check}` should be a number", + "message" => i18n::translate("`%s` should be a number",[$item_to_check]), "label" => $item_to_check, ]; $this->addError($message); @@ -102,10 +130,20 @@ private function checkExist(string $itemKey=null){ } return $status_key_exist; } - private function addError(array $value){ - return $this->_errors[]=$value; + + /** + * @param array $value + */ + private function addError(array $value): void + { + $this->_errors[] = $value; } - function check(){ + + /** + * @return array + */ + function check(): array + { return $this->_errors; } } diff --git a/src/VString.php b/src/VString.php index ec0d62c..22337c9 100644 --- a/src/VString.php +++ b/src/VString.php @@ -13,16 +13,16 @@ */ class VString { private $string_value; - private $string_item; - private $source_data; - private $_errors; - private $_min; - private $_max; + private ?string $string_item; + private array $source_data=[]; + private array $_errors=[]; + private int $_min=0; + private int $_max=0; + /** - * + * * @param array $source - * @param string $string_item - * @param string $stringValue + * @param string|null $string_item */ function __construct(array $source,string $string_item=null) { $this->string_item=$string_item; @@ -38,12 +38,13 @@ function __construct(array $source,string $string_item=null) { * @param int $rule_values * @return $this */ - function min(int $rule_values=0){ + function min(int $rule_values=0): VString + { $min=is_integer($rule_values)? ((int)$rule_values>0?(int)$rule_values:0):0; if (strlen($this->string_value) < $min) { $message=[ "type"=>"string.min", - "message"=> i18n::translate("`%s` should have minimum of `%s` caracters",[$this->string_item,$min]), + "message"=> i18n::translate("`%s` should have minimum of `%s` characters",[$this->string_item,$min]), "label"=>$this->string_item, "limit"=>$min ]; @@ -56,13 +57,14 @@ function min(int $rule_values=0){ * @param int $rule_values * @return $this */ - function max(int $rule_values=1){ + function max(int $rule_values=1): VString + { $max = is_integer($rule_values) ? ((int)$rule_values > 0 ? (int)$rule_values : 0):0; $this->_max=$max; if (strlen($this->string_value) > $max) { $message = [ "type" => "string.max", - "message" => i18n::translate("`%s` should have maximum of `%s` caracters",[$this->string_item,$max]), + "message" => i18n::translate("`%s` should have maximum of `%s` characters",[$this->string_item,$max]), "label" => $this->string_item, "limit" => $max ]; @@ -74,7 +76,8 @@ function max(int $rule_values=1){ * * @return $this */ - function email(){ + function email(): VString + { if (!filter_var($this->string_value, FILTER_VALIDATE_EMAIL)) { $message = [ "type" => "string.email", @@ -89,7 +92,8 @@ function email(){ * * @return $this */ - function url(){ + function url(): VString + { if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i", $this->string_value)) { $message = [ "type" => "string.url", @@ -117,7 +121,12 @@ function match(string $key_tomatch){ } return $this; } - function required(){ + + /** + * @return $this + */ + function required(): VString + { $required_value= trim($this->string_value); if (empty($required_value)) { $message = [ @@ -141,7 +150,7 @@ private function checkExist(string $itemKey=null){ if (!isset($this->source_data[$item_to_check])) { $message = [ "type"=> "any.unknow", - "message" => "`{$item_to_check}` is unknow", + "message" => i18n::translate("`%s` is unknow",[$item_to_check]), "label" => $item_to_check, ]; $this->addError($message); @@ -159,10 +168,11 @@ private function checkExist(string $itemKey=null){ }/** * * @param array $value - * @return type - */ - private function addError(array $value){ - return $this->_errors[]=$value; + * @return void + */ + private function addError(array $value): void + { + $this->_errors[] = $value; }/** * * @return type diff --git a/src/Escape.php b/src/i18n.php similarity index 100% rename from src/Escape.php rename to src/i18n.php diff --git a/test/index.php b/test/index.php index aaa4e86..88d6042 100644 --- a/test/index.php +++ b/test/index.php @@ -16,7 +16,7 @@ "date_created"=>"2021-05-23" ]; $valid=new Validate($source); - include "./test/string.php"; -// include "./test/number.php"; +// include "./test/string.php"; + include "./test/number.php"; // include "./test/boolean.php"; // include "./test/date.php"; From 97e85ad7052371b02e2759e58b69a12cb334ff2e Mon Sep 17 00:00:00 2001 From: bim-g Date: Sun, 2 Jan 2022 14:47:10 +0200 Subject: [PATCH 3/3] [UPD]translate date,[FIX] make namestace start with uppercase --- .idea/php.xml | 5 +++ .idea/wepesi_validation.iml | 2 +- composer.json | 2 +- lang/fr/language.php | 11 ++++--- src/VBoolean.php | 2 +- src/VDate.php | 61 +++++++++++++++++++------------------ src/VNumber.php | 6 ++-- src/VString.php | 2 +- src/Validate.php | 2 +- src/i18n.php | 2 +- test/date.php | 2 +- test/index.php | 6 ++-- test/number.php | 4 +-- 13 files changed, 58 insertions(+), 49 deletions(-) diff --git a/.idea/php.xml b/.idea/php.xml index 3b1bcf5..9a0a9fd 100644 --- a/.idea/php.xml +++ b/.idea/php.xml @@ -6,4 +6,9 @@ + + + + + \ No newline at end of file diff --git a/.idea/wepesi_validation.iml b/.idea/wepesi_validation.iml index 1ee89a6..40fe797 100644 --- a/.idea/wepesi_validation.iml +++ b/.idea/wepesi_validation.iml @@ -3,7 +3,7 @@ - + diff --git a/composer.json b/composer.json index 609da5c..7991ca0 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,7 @@ ], "autoload": { "psr-4": { - "Wepesi\\app\\":"src/" + "Wepesi\\App\\":"src/" } }, "require": { diff --git a/lang/fr/language.php b/lang/fr/language.php index 2d8868e..6872663 100644 --- a/lang/fr/language.php +++ b/lang/fr/language.php @@ -7,10 +7,11 @@ "`%s` should match %s"=>"`%s` doit correpondre a `%s`", "`%s` is required"=>"`%s` est obligatoire", "`%s` should be a string"=>"`%s` doit est une chaine de caractères", - "`%s` should be greater than `%s`"=>"`%s` devrait être supérieur à `%s`", - "`%s` should be less than `%s`"=>"`%s` devrait être inférieur à `%s`", + "`%s` should be greater than `%s`"=>"`%s` devrait être supérieur à `%s`", + "`%s` should be less than `%s`"=>"`%s` devrait être inférieur à `%s`", "`%s` should be a positive number"=>"`%s` devrait être un nombre positif", - "`%s` is unknow"=>"`%s` devrait être inférieur à `%s`", - "`%s` should be less than `%s`"=>"`%s` devrait être inférieur à `%s`", - "`%s` should be less than `%s`"=>"`%s` devrait être inférieur à `%s`", + "`%s` should be a number"=>"`%s` devrait être un nombre.", + "`%s` is unknow"=>"`%s` n'est pas definit", + "`%s` should be a date."=>"`%s` devrait être une date.", + "`%s` should be greater than now"=>"`%s` devrait être plus grand que maintenant", ]; \ No newline at end of file diff --git a/src/VBoolean.php b/src/VBoolean.php index 9a62c58..270abdd 100644 --- a/src/VBoolean.php +++ b/src/VBoolean.php @@ -1,6 +1,6 @@ date_value=$source[$string_item]; @@ -29,14 +27,15 @@ function __construct(array $source,string $string_item=null) { /** * @return $this */ - function now(){ + function now(): VDate + { $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->string_item}` should be greater than now", + "message"=> i18n::translate("`%s` should be greater than now",[$this->string_item]), "label"=>$this->string_item, "limit"=>$min_date ]; @@ -50,15 +49,15 @@ function now(){ * @return $this * while trying to get day validation use this module */ - function today(string $times=null){ - $regeg="#+[0-9]h:[0-9]min:[0-9]sec#"; + function today(string $times=null): VDate + { $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->string_item}` should be greater than today ", + "message"=> i18n::translate("`%s` should be greater than today ",[$this->string_item]), "label"=>$this->string_item, "limit"=>$min_date ]; @@ -66,26 +65,28 @@ function today(string $times=null){ } return $this; } + /** - * @param string $rule_values + * @param string|null $rule_values * @return $this * get the min date control from the given date */ - function min(string $rule_values=null){ + function min(string $rule_values=null): VDate + { /** * $regex= "#[a-zA-Z]#"; * $time= preg_match($regex,$rule_values); * $con=!$time?$time:(int)$time; * in case the parameters are integers */ - $rule_values=isset($rule_values)?$rule_values: "now"; + $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->string_item}` should greater than `{$min_date}`", + "message"=> i18n::translate("`%s` should be greater than `%s`",[$this->string_item,$min_date]), "label"=>$this->string_item, "limit"=>$min_date ]; @@ -99,16 +100,16 @@ function min(string $rule_values=null){ * @return $this * while try to check maximum date of a defined period use this module */ - function max(string $rule_values=null){ - $rule_values=isset($rule_values)?$rule_values: "now"; - + function max(string $rule_values=null): VDate + { + $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->string_item}` should be less than `{$max_date}`", + "message" => i18n::translate("`%s` should be less than `%s`",[$this->string_item,$max_date]), "label" => $this->string_item, "limit" => $max_date ]; @@ -120,12 +121,13 @@ function max(string $rule_values=null){ * @return $this * call this module is the input is requied and should not be null or empty */ - function required(){ + function required(): VDate + { $required_value= trim($this->date_value); if (empty($required_value) || strlen($required_value)==0) { $message = [ "type"=> "any.required", - "message" => "`{$this->string_item}` {$this->lang->required}", + "message" => i18n::translate("`%s` is required",[$this->string_item]), "label" => $this->string_item, ]; $this->addError($message); @@ -133,29 +135,30 @@ function required(){ return $this; } // private methode - private function checkExist(string $itemKey=null){ + 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.unknow", - "message" => "`{$item_to_check}` {$this->lang->unknow}", + "message" => i18n::translate("`%s` is unknow",[$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.unknow", - "message" => "`{$item_to_check}` {$this->lang->string_unknow}", + "message" => i18n::translate("`%s` should be a date.",[$item_to_check]), "label" => $item_to_check, ]; $this->addError($message); } - return true; } - private function addError(array $value){ - return $this->_errors[]=$value; + private function addError(array $value): void + { + $this->_errors[] = $value; } function check(){ return $this->_errors; diff --git a/src/VNumber.php b/src/VNumber.php index 3839bec..d7f1aa7 100644 --- a/src/VNumber.php +++ b/src/VNumber.php @@ -6,7 +6,7 @@ * and open the template in the editor. */ -namespace Wepesi\app; +namespace Wepesi\App; /** * Description of VNumber @@ -40,7 +40,7 @@ function min(int $min_values): VNumber if ((int) $this->string_value < $min_values) { $message=[ "type"=>"number.min", - "message"=> i18n::translate("`%s` should be greater than `%s`",[$this->string_item,$min_values]), + "message"=> i18n::translate("`%s` should be greater than `%s`",[$this->string_item,$min_values]), "label"=>$this->string_item, "limit"=>$min_values ]; @@ -58,7 +58,7 @@ function max(int $min_values): VNumber if ((int) $this->string_value > $min_values) { $message=[ "type"=>"number.max", - "message"=> i18n::translate("`%s` should be less than `%s`",[$this->string_item,$min_values]), + "message"=> i18n::translate("`%s` should be less than `%s`",[$this->string_item,$min_values]), "label"=>$this->string_item, "limit"=>$min_values ]; diff --git a/src/VString.php b/src/VString.php index 22337c9..594499c 100644 --- a/src/VString.php +++ b/src/VString.php @@ -1,5 +1,5 @@ $valid->date("birth_day")->min("-18years")->required()->check(), - "date_created"=>$valid->date("birth_day")->now()->required()->check() + "date_created"=>$valid->date("birth_day")->now()->max("100years")->required()->check() ]; $valid->check($source,$schema); diff --git a/test/index.php b/test/index.php index 88d6042..9c51fa1 100644 --- a/test/index.php +++ b/test/index.php @@ -1,5 +1,5 @@ "", @@ -17,6 +17,6 @@ ]; $valid=new Validate($source); // include "./test/string.php"; - include "./test/number.php"; +// include "./test/number.php"; // include "./test/boolean.php"; -// include "./test/date.php"; + include "./test/date.php"; diff --git a/test/number.php b/test/number.php index f264145..4137f99 100644 --- a/test/number.php +++ b/test/number.php @@ -1,7 +1,7 @@ $valid->number("age")->min(18)->max(50)->required()->check() ]; - $valid->check($source,$check); + $valid->check($source,$schema); var_dump($valid->passed()); var_dump($valid->errors()); \ No newline at end of file