Skip to content

Commit

Permalink
Merge pull request #42 from bim-g/v2
Browse files Browse the repository at this point in the history
[ENH] make standard transalion module
  • Loading branch information
bim-g authored Jan 2, 2022
2 parents d212371 + 53f77ae commit e7664f7
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 66 deletions.
21 changes: 0 additions & 21 deletions src/Escape.php

This file was deleted.

14 changes: 8 additions & 6 deletions src/VBoolean.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,18 @@ class VBoolean
private ?string $string_item;
private array $source_data;
private array $_errors=[];
private i18n $i18n;

/**
*
* @param array $source
* @param string|null $string_item
*/
function __construct(array $source, string $string_item = null)
function __construct(array $source, string $string_item = null,string $lang)
{
$this->string_item = $string_item;
$this->source_data = $source;
$this->i18n= new i18n($lang);
if ($this->checkExist()) {
$this->string_value = $source[$string_item];
};
Expand All @@ -47,15 +49,15 @@ private function checkExist(string $itemKey = null): bool
if (!isset($this->source_data[$item_to_check])) {
$message = [
"type" => "any.unknown",
"message" => i18n::translate("`%s` is unknown",[$item_to_check]),
"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" => i18n::translate("`%s` should be a boolean",[$item_to_check]),
"message" => $this->i18n->translate("`%s` should be a boolean",[$item_to_check]),
"label" => $item_to_check,
];
$this->addError($message);
Expand All @@ -70,7 +72,7 @@ function required(): VBoolean
if (empty($required_value)) {
$message = [
"type" => "any.required",
"message" => i18n::translate("`%s` is required",[$this->string_item]),
"message" => $this->i18n->translate("`%s` is required",[$this->string_item]),
"label" => $this->string_item,
];
$this->addError($message);
Expand All @@ -86,7 +88,7 @@ function isValid(string $value): VBoolean
if (!($check)) {
$message = [
"type" => "boolean.required",
"message" => i18n::translate("isValid param must be boolean but you put `%s`",[$required_value]),
"message" => $this->i18n->translate("isValid param must be boolean but you put `%s`",[$required_value]),
"label" => $this->string_item,
];
$this->addError($message);
Expand All @@ -97,7 +99,7 @@ function isValid(string $value): VBoolean
if ($incoming_value != $required_value) {
$message = [
"type" => "boolean.valid",
"message" => i18n::translate("`%s` is not validValue required. You must put `%s`",[$incoming_value,$required_value]),
"message" => $this->i18n->translate("`%s` is not validValue required. You must put `%s`",[$incoming_value,$required_value]),
"label" => $this->string_item,
];
$this->addError($message);
Expand Down
19 changes: 11 additions & 8 deletions src/VDate.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@ class VDate
private int $_min;
private int $_max;
//put your code here
function __construct(array $source,string $string_item=null) {
private i18n $i18n;

function __construct(array $source,string $string_item=null,string $lang="en") {
$this->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();
}
Expand All @@ -35,7 +38,7 @@ function now(): VDate
if ($date_value_time < $min_date_time) {
$message=[
"type"=>"date.now",
"message"=> i18n::translate("`%s` should be greater than now",[$this->string_item]),
"message"=> $this->i18n->translate("`%s` should be greater than now",[$this->string_item]),
"label"=>$this->string_item,
"limit"=>$min_date
];
Expand All @@ -57,7 +60,7 @@ function today(string $times=null): VDate
if ($date_value_time > $min_date_time) {
$message=[
"type"=>"date.now",
"message"=> i18n::translate("`%s` should be greater than today ",[$this->string_item]),
"message"=> $this->i18n->translate("`%s` should be greater than today ",[$this->string_item]),
"label"=>$this->string_item,
"limit"=>$min_date
];
Expand Down Expand Up @@ -86,7 +89,7 @@ function min(string $rule_values=null): VDate
if ($date_value_time > $min_date_time) {
$message=[
"type"=>"date.min",
"message"=> i18n::translate("`%s` should be greater than `%s`",[$this->string_item,$min_date]),
"message"=> $this->i18n->translate("`%s` should be greater than `%s`",[$this->string_item,$min_date]),
"label"=>$this->string_item,
"limit"=>$min_date
];
Expand All @@ -109,7 +112,7 @@ function max(string $rule_values=null): VDate
if ($max_date_time<$date_value_time) {
$message = [
"type" => "date.max",
"message" => i18n::translate("`%s` should be less than `%s`",[$this->string_item,$max_date]),
"message" => $this->i18n->translate("`%s` should be less than `%s`",[$this->string_item,$max_date]),
"label" => $this->string_item,
"limit" => $max_date
];
Expand All @@ -127,7 +130,7 @@ function required(): VDate
if (empty($required_value) || strlen($required_value)==0) {
$message = [
"type"=> "any.required",
"message" => i18n::translate("`%s` is required",[$this->string_item]),
"message" => $this->i18n->translate("`%s` is required",[$this->string_item]),
"label" => $this->string_item,
];
$this->addError($message);
Expand All @@ -143,14 +146,14 @@ private function checkExist(string $itemKey=null): void
if (!isset($this->source_data[$item_to_check])) {
$message = [
"type"=> "any.unknown",
"message" => i18n::translate("`%s` is unknown",[$item_to_check]),
"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" => i18n::translate("`%s` should be a date.",[$item_to_check]),
"message" => $this->i18n->translate("`%s` should be a date.",[$item_to_check]),
"label" => $item_to_check,
];
$this->addError($message);
Expand Down
18 changes: 10 additions & 8 deletions src/VNumber.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ class VNumber {
private array $_errors;
private int $_min;
private int $_max;

function __construct(array $source,string $string_item) {
private i18n $i18n;

function __construct(array $source,string $string_item,string $lang="en") {
$this->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];
}
Expand All @@ -40,7 +42,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"=> $this->i18n->translate("`%s` should be greater than `%s`",[$this->string_item,$min_values]),
"label"=>$this->string_item,
"limit"=>$min_values
];
Expand All @@ -58,7 +60,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"=> $this->i18n->translate("`%s` should be less than `%s`",[$this->string_item,$min_values]),
"label"=>$this->string_item,
"limit"=>$min_values
];
Expand All @@ -76,7 +78,7 @@ function positive(int $min_values): VNumber
if ((int) $this->string_value < 0) {
$message=[
"type"=>"number.positive",
"message"=> i18n::translate("`%s` should be a positive number",[$this->string_item]),
"message"=> $this->i18n->translate("`%s` should be a positive number",[$this->string_item]),
"label"=>$this->string_item,
"limit"=>1
];
Expand All @@ -94,7 +96,7 @@ function required(): VNumber
if (empty($required_value)) {
$message = [
"type"=> "any.required",
"message" => i18n::translate("`%s` is required",[$this->string_item]),
"message" => $this->i18n->translate("`%s` is required",[$this->string_item]),
"label" => $this->string_item,
];
$this->addError($message);
Expand All @@ -114,15 +116,15 @@ private function checkExist(string $itemKey=null): bool
if (!isset($this->source_data[$item_to_check])) {
$message = [
"type"=> "any.unknown",
"message" => i18n::translate("`%s` is unknown",[$item_to_check]),
"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" => i18n::translate("`%s` should be a number",[$item_to_check]),
"message" => $this->i18n->translate("`%s` should be a number",[$item_to_check]),
"label" => $item_to_check,
];
$this->addError($message);
Expand Down
24 changes: 13 additions & 11 deletions src/VString.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,20 @@ class VString {
private array $_errors=[];
private int $_min=0;
private int $_max=0;
private i18n $i18n;

/**
*
* @param array $source
* @param string|null $string_item
* @param $lang
*/
function __construct(array $source,string $string_item=null) {
function __construct(array $source,string $string_item=null,string $lang="en") {
$this->string_item=$string_item;
$this->source_data=$source;
$this->_max= $this->_min=0;
$this->check_key=$this->checkExist();
if($this->check_key){
$this->i18n= new i18n($lang);
if($this->checkExist()){
$this->string_value=$source[$string_item];
};
}
Expand All @@ -44,7 +46,7 @@ function min(int $rule_values=0): VString
if (strlen($this->string_value) < $min) {
$message=[
"type"=>"string.min",
"message"=> i18n::translate("`%s` should have minimum of `%s` characters",[$this->string_item,$min]),
"message"=> $this->i18n->translate("`%s` should have minimum of `%s` characters",[$this->string_item,$min]),
"label"=>$this->string_item,
"limit"=>$min
];
Expand All @@ -64,7 +66,7 @@ function max(int $rule_values=1): VString
if (strlen($this->string_value) > $max) {
$message = [
"type" => "string.max",
"message" => i18n::translate("`%s` should have maximum of `%s` characters",[$this->string_item,$max]),
"message" => $this->i18n->translate("`%s` should have maximum of `%s` characters",[$this->string_item,$max]),
"label" => $this->string_item,
"limit" => $max
];
Expand All @@ -81,7 +83,7 @@ function email(): VString
if (!filter_var($this->string_value, FILTER_VALIDATE_EMAIL)) {
$message = [
"type" => "string.email",
"message" => i18n::translate("`%s` this should be an email",[$this->string_item]),
"message" => $this->i18n->translate("`%s` this should be an email",[$this->string_item]),
"label" => $this->string_item,
];
$this->addError($message);
Expand All @@ -97,7 +99,7 @@ function url(): VString
if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i", $this->string_value)) {
$message = [
"type" => "string.url",
"message" => i18n::translate("`{$this->string_item}` this should be a link(url)"),
"message" => $this->i18n->translate("`{$this->string_item}` this should be a link(url)"),
"label" => $this->string_item,
];
$this->addError($message);
Expand All @@ -114,7 +116,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" => i18n::translate("`%s` should match %s",[$this->string_item,$key_tomatch]),
"message" => $this->i18n->translate("`%s` should match %s",[$this->string_item,$key_tomatch]),
"label" => $this->string_item,
];
$this->addError($message);
Expand All @@ -131,7 +133,7 @@ function required(): VString
if (empty($required_value)) {
$message = [
"type"=> "any.required",
"message" => i18n::translate("`%s` is required",[$this->string_item]),
"message" => $this->i18n->translate("`%s` is required",[$this->string_item]),
"label" => $this->string_item,
];
$this->addError($message);
Expand All @@ -150,15 +152,15 @@ private function checkExist(string $itemKey=null){
if (!isset($this->source_data[$item_to_check])) {
$message = [
"type"=> "any.unknown",
"message" => i18n::translate("`%s` is unknown",[$item_to_check]),
"message" => $this->i18n->translate("`%s` is unknown",[$item_to_check]),
"label" => $item_to_check,
];
$this->addError($message);
return false;
}else if(!preg_match($regex,$this->source_data[$item_to_check]) || strlen(trim($this->source_data[$item_to_check]))==0){
$message=[
"type" => "string.unknown",
"message" => i18n::translate("`%s` should be a string",[$item_to_check]),
"message" => $this->i18n->translate("`%s` should be a string",[$item_to_check]),
"label" => $item_to_check,
];
$this->addError($message);
Expand Down
Loading

0 comments on commit e7664f7

Please sign in to comment.