-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from bim-g/version_emmanuel
ft boolean validation
- Loading branch information
Showing
7 changed files
with
294 additions
and
192 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
<?php | ||
namespace Wepesi\app; | ||
/* | ||
* To change this license header, choose License Headers in Project Properties. | ||
* To change this template file, choose Tools | Templates | ||
* and open the template in the editor. | ||
*/ | ||
|
||
/** | ||
* Description of String | ||
* | ||
* @author Domeshow | ||
*/ | ||
class VBoolean { | ||
private $string_value; | ||
private $string_item; | ||
private $source_data; | ||
private $_errors; | ||
/** | ||
* | ||
* @param array $source | ||
* @param string $string_item | ||
* @param string $stringValue | ||
*/ | ||
function __construct(array $source,string $string_item=null) { | ||
$this->string_item=$string_item; | ||
$this->source_data=$source; | ||
$this->check_key=$this->checkExist(); | ||
if($this->check_key){ | ||
$this->string_value=$source[$string_item]; | ||
}; | ||
} | ||
|
||
/** | ||
* | ||
* @param string $itemKey | ||
* @return boolean | ||
*/ | ||
private function checkExist(string $itemKey=null){ | ||
$item_to_check=$itemKey?$itemKey:$this->string_item; | ||
$val = $this->source_data[$item_to_check]; | ||
|
||
$regex = "/^(true|false)$/"; | ||
if (!isset($this->source_data[$item_to_check])) { | ||
$message = [ | ||
"type"=> "any.unknow", | ||
"message" => "`{$item_to_check}` is unknow", | ||
"label" => $item_to_check, | ||
]; | ||
$this->addError($message); | ||
return false; | ||
}else if(!preg_match($regex, is_bool($val)?($val?'true':'false'):$val)){ | ||
$message=[ | ||
"type" => "boolean.unknow", | ||
"message" => "`{$item_to_check}` shoud be a boolean", | ||
"label" => $item_to_check, | ||
]; | ||
$this->addError($message); | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
function required(){ | ||
$required_value= is_bool($this->string_value); | ||
if (empty($required_value)) { | ||
$message = [ | ||
"type"=> "any.required", | ||
"message" => "`{$this->string_item}` is required", | ||
"label" => $this->string_item, | ||
]; | ||
$this->addError($message); | ||
} | ||
return $this; | ||
} | ||
|
||
/** | ||
* | ||
* @param array $value | ||
* @return type | ||
*/ | ||
private function addError(array $value){ | ||
return $this->_errors[]=$value; | ||
}/** | ||
* | ||
* @return type | ||
*/ | ||
function check(){ | ||
return $this->_errors; | ||
} | ||
} |
Oops, something went wrong.