Skip to content

Commit 6b8529d

Browse files
authored
Merge pull request #17 from bim-g/version_emmanuel
ft boolean validation
2 parents 410b77a + 43090f1 commit 6b8529d

File tree

7 files changed

+294
-192
lines changed

7 files changed

+294
-192
lines changed

class/VBoolean.php

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
namespace Wepesi\app;
3+
/*
4+
* To change this license header, choose License Headers in Project Properties.
5+
* To change this template file, choose Tools | Templates
6+
* and open the template in the editor.
7+
*/
8+
9+
/**
10+
* Description of String
11+
*
12+
* @author Domeshow
13+
*/
14+
class VBoolean {
15+
private $string_value;
16+
private $string_item;
17+
private $source_data;
18+
private $_errors;
19+
/**
20+
*
21+
* @param array $source
22+
* @param string $string_item
23+
* @param string $stringValue
24+
*/
25+
function __construct(array $source,string $string_item=null) {
26+
$this->string_item=$string_item;
27+
$this->source_data=$source;
28+
$this->check_key=$this->checkExist();
29+
if($this->check_key){
30+
$this->string_value=$source[$string_item];
31+
};
32+
}
33+
34+
/**
35+
*
36+
* @param string $itemKey
37+
* @return boolean
38+
*/
39+
private function checkExist(string $itemKey=null){
40+
$item_to_check=$itemKey?$itemKey:$this->string_item;
41+
$val = $this->source_data[$item_to_check];
42+
43+
$regex = "/^(true|false)$/";
44+
if (!isset($this->source_data[$item_to_check])) {
45+
$message = [
46+
"type"=> "any.unknow",
47+
"message" => "`{$item_to_check}` is unknow",
48+
"label" => $item_to_check,
49+
];
50+
$this->addError($message);
51+
return false;
52+
}else if(!preg_match($regex, is_bool($val)?($val?'true':'false'):$val)){
53+
$message=[
54+
"type" => "boolean.unknow",
55+
"message" => "`{$item_to_check}` shoud be a boolean",
56+
"label" => $item_to_check,
57+
];
58+
$this->addError($message);
59+
return false;
60+
}
61+
return true;
62+
}
63+
64+
function required(){
65+
$required_value= is_bool($this->string_value);
66+
if (empty($required_value)) {
67+
$message = [
68+
"type"=> "any.required",
69+
"message" => "`{$this->string_item}` is required",
70+
"label" => $this->string_item,
71+
];
72+
$this->addError($message);
73+
}
74+
return $this;
75+
}
76+
77+
/**
78+
*
79+
* @param array $value
80+
* @return type
81+
*/
82+
private function addError(array $value){
83+
return $this->_errors[]=$value;
84+
}/**
85+
*
86+
* @return type
87+
*/
88+
function check(){
89+
return $this->_errors;
90+
}
91+
}

0 commit comments

Comments
 (0)