Skip to content

Commit

Permalink
[UPD] update translation for vnumber
Browse files Browse the repository at this point in the history
  • Loading branch information
bim-g committed Jan 2, 2022
1 parent 89802e5 commit 5496d99
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 46 deletions.
12 changes: 9 additions & 3 deletions lang/fr/language.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
<?php
$language=[
"`%s` should have minimum of `%s` caracters"=>"`%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`",
];
80 changes: 59 additions & 21 deletions src/VNumber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -30,82 +30,120 @@ 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
];
$this->addError($message);
}
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
];
$this->addError($message);
}
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
];
$this->addError($message);
}
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);
$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.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);
$status_key_exist=false;
}
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;
}
}
50 changes: 30 additions & 20 deletions src/VString.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
];
Expand All @@ -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
];
Expand All @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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 = [
Expand All @@ -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);
Expand All @@ -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
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions test/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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";

0 comments on commit 5496d99

Please sign in to comment.