|
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 Ibrahim |
13 |
| - */ |
14 |
| -class VString { |
15 |
| - private $string_value; |
16 |
| - private $string_item; |
17 |
| - private $source_data; |
18 |
| - private $_errors; |
19 |
| - private $_min; |
20 |
| - private $_max; |
21 |
| - /** |
22 |
| - * |
23 |
| - * @param array $source |
24 |
| - * @param string $string_item |
25 |
| - * @param string $stringValue |
26 |
| - */ |
27 |
| - function __construct(array $source,string $string_item=null) { |
28 |
| - $this->string_item=$string_item; |
29 |
| - $this->source_data=$source; |
30 |
| - $this->_max= $this->_min=0; |
31 |
| - $this->check_key=$this->checkExist(); |
32 |
| - if($this->check_key){ |
33 |
| - $this->string_value=$source[$string_item]; |
34 |
| - }; |
35 |
| - } |
36 |
| - /** |
37 |
| - * |
38 |
| - * @param int $rule_values |
39 |
| - * @return $this |
40 |
| - */ |
41 |
| - function min(int $rule_values=0){ |
42 |
| - $min=is_integer($rule_values)? ((int)$rule_values>0?(int)$rule_values:0):0; |
43 |
| - if (strlen($this->string_value) < $min) { |
44 |
| - $message=[ |
45 |
| - "type"=>"string.min", |
46 |
| - "message"=> "`{$this->string_item}` should have minimum of `{$min}` caracters", |
47 |
| - "label"=>$this->string_item, |
48 |
| - "limit"=>$min |
49 |
| - ]; |
50 |
| - $this->addError($message); |
51 |
| - } |
52 |
| - return $this; |
53 |
| - } |
54 |
| - /** |
55 |
| - * |
56 |
| - * @param int $rule_values |
57 |
| - * @return $this |
58 |
| - */ |
59 |
| - function max(int $rule_values=1){ |
60 |
| - $max = is_integer($rule_values) ? ((int)$rule_values > 0 ? (int)$rule_values : 0):0; |
61 |
| - $this->_max=$max; |
62 |
| - if (strlen($this->string_value) > $max) { |
63 |
| - $message = [ |
64 |
| - "type" => "string.max", |
65 |
| - "message" => "`{$this->string_item}` should have maximum of `{$max}` caracters", |
66 |
| - "label" => $this->string_item, |
67 |
| - "limit" => $max |
68 |
| - ]; |
69 |
| - $this->addError($message); |
70 |
| - } |
71 |
| - return $this; |
72 |
| - } |
73 |
| - /** |
74 |
| - * |
75 |
| - * @return $this |
76 |
| - */ |
77 |
| - function email(){ |
78 |
| - if (!filter_var($this->string_value, FILTER_VALIDATE_EMAIL)) { |
79 |
| - $message = [ |
80 |
| - "type" => "string.email", |
81 |
| - "message" => "`{$this->string_item}` this should be an email", |
82 |
| - "label" => $this->string_item, |
83 |
| - ]; |
84 |
| - $this->addError($message); |
85 |
| - } |
86 |
| - return $this; |
87 |
| - } |
88 |
| - /** |
89 |
| - * |
90 |
| - * @return $this |
91 |
| - */ |
92 |
| - function url(){ |
93 |
| - if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i", $this->string_value)) { |
94 |
| - $message = [ |
95 |
| - "type" => "string.url", |
96 |
| - "message" => "`{$this->string_item}` this shoudl be a link(url)", |
97 |
| - "label" => $this->string_item, |
98 |
| - ]; |
99 |
| - $this->addError($message); |
100 |
| - } |
101 |
| - return $this; |
102 |
| - } |
103 |
| - /** |
104 |
| - * |
105 |
| - * @param string $key_tomatch |
106 |
| - * @return $this |
107 |
| - */ |
108 |
| - function match(string $key_tomatch){ |
109 |
| - $this->checkExist($key_tomatch); |
110 |
| - 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])) { |
111 |
| - $message = [ |
112 |
| - "type" => "string.match", |
113 |
| - "message" => "`{$this->string_item}` should match {$key_tomatch}", |
114 |
| - "label" => $this->string_item, |
115 |
| - ]; |
116 |
| - $this->addError($message); |
117 |
| - } |
118 |
| - return $this; |
119 |
| - } |
120 |
| - function required(){ |
121 |
| - $required_value= trim($this->string_value); |
122 |
| - if (empty($required_value)) { |
123 |
| - $message = [ |
124 |
| - "type"=> "any.required", |
125 |
| - "message" => "`{$this->string_item}` is required", |
126 |
| - "label" => $this->string_item, |
127 |
| - ]; |
128 |
| - $this->addError($message); |
129 |
| - } |
130 |
| - return $this; |
131 |
| - } |
132 |
| - /** |
133 |
| - * |
134 |
| - * @param string $itemKey |
135 |
| - * @return boolean |
136 |
| - */ |
137 |
| - private function checkExist(string $itemKey=null){ |
138 |
| - $item_to_check=$itemKey?$itemKey:$this->string_item; |
139 |
| - $regex="#[a-zA-Z0-9]#"; |
140 |
| - $status_keys_exist=true; |
141 |
| - if (!isset($this->source_data[$item_to_check])) { |
142 |
| - $message = [ |
143 |
| - "type"=> "any.unknow", |
144 |
| - "message" => "`{$item_to_check}` is unknow", |
145 |
| - "label" => $item_to_check, |
146 |
| - ]; |
147 |
| - $this->addError($message); |
148 |
| - return false; |
149 |
| - }else if(!preg_match($regex,$this->source_data[$item_to_check]) || strlen(trim($this->source_data[$item_to_check]))==0){ |
150 |
| - $message=[ |
151 |
| - "type" => "string.unknow", |
152 |
| - "message" => "`{$item_to_check}` shoud be a s tring", |
153 |
| - "label" => $item_to_check, |
154 |
| - ]; |
155 |
| - $this->addError($message); |
156 |
| - return false; |
157 |
| - } |
158 |
| - return $status_keys_exist; |
159 |
| - }/** |
160 |
| - * |
161 |
| - * @param array $value |
162 |
| - * @return type |
163 |
| - */ |
164 |
| - private function addError(array $value){ |
165 |
| - return $this->_errors[]=$value; |
166 |
| - }/** |
167 |
| - * |
168 |
| - * @return type |
169 |
| - */ |
170 |
| - function check(){ |
171 |
| - return $this->_errors; |
172 |
| - } |
173 |
| -} |
| 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 Ibrahim |
| 13 | + */ |
| 14 | +class VString { |
| 15 | + private $string_value; |
| 16 | + private $string_item; |
| 17 | + private $source_data; |
| 18 | + private $_errors; |
| 19 | + private $_min; |
| 20 | + private $_max; |
| 21 | + /** |
| 22 | + * |
| 23 | + * @param array $source |
| 24 | + * @param string $string_item |
| 25 | + * @param string $stringValue |
| 26 | + */ |
| 27 | + function __construct(array $source,string $string_item=null) { |
| 28 | + $this->string_item=$string_item; |
| 29 | + $this->source_data=$source; |
| 30 | + $this->_max= $this->_min=0; |
| 31 | + $this->check_key=$this->checkExist(); |
| 32 | + if($this->check_key){ |
| 33 | + $this->string_value=$source[$string_item]; |
| 34 | + }; |
| 35 | + } |
| 36 | + /** |
| 37 | + * |
| 38 | + * @param int $rule_values |
| 39 | + * @return $this |
| 40 | + */ |
| 41 | + function min(int $rule_values=0){ |
| 42 | + $min=is_integer($rule_values)? ((int)$rule_values>0?(int)$rule_values:0):0; |
| 43 | + if (strlen($this->string_value) < $min) { |
| 44 | + $message=[ |
| 45 | + "type"=>"string.min", |
| 46 | + "message"=> "`{$this->string_item}` should have minimum of `{$min}` caracters", |
| 47 | + "label"=>$this->string_item, |
| 48 | + "limit"=>$min |
| 49 | + ]; |
| 50 | + $this->addError($message); |
| 51 | + } |
| 52 | + return $this; |
| 53 | + } |
| 54 | + /** |
| 55 | + * |
| 56 | + * @param int $rule_values |
| 57 | + * @return $this |
| 58 | + */ |
| 59 | + function max(int $rule_values=1){ |
| 60 | + $max = is_integer($rule_values) ? ((int)$rule_values > 0 ? (int)$rule_values : 0):0; |
| 61 | + $this->_max=$max; |
| 62 | + if (strlen($this->string_value) > $max) { |
| 63 | + $message = [ |
| 64 | + "type" => "string.max", |
| 65 | + "message" => "`{$this->string_item}` should have maximum of `{$max}` caracters", |
| 66 | + "label" => $this->string_item, |
| 67 | + "limit" => $max |
| 68 | + ]; |
| 69 | + $this->addError($message); |
| 70 | + } |
| 71 | + return $this; |
| 72 | + } |
| 73 | + /** |
| 74 | + * |
| 75 | + * @return $this |
| 76 | + */ |
| 77 | + function email(){ |
| 78 | + if (!filter_var($this->string_value, FILTER_VALIDATE_EMAIL)) { |
| 79 | + $message = [ |
| 80 | + "type" => "string.email", |
| 81 | + "message" => "`{$this->string_item}` this should be an email", |
| 82 | + "label" => $this->string_item, |
| 83 | + ]; |
| 84 | + $this->addError($message); |
| 85 | + } |
| 86 | + return $this; |
| 87 | + } |
| 88 | + /** |
| 89 | + * |
| 90 | + * @return $this |
| 91 | + */ |
| 92 | + function url(){ |
| 93 | + if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i", $this->string_value)) { |
| 94 | + $message = [ |
| 95 | + "type" => "string.url", |
| 96 | + "message" => "`{$this->string_item}` this shoudl be a link(url)", |
| 97 | + "label" => $this->string_item, |
| 98 | + ]; |
| 99 | + $this->addError($message); |
| 100 | + } |
| 101 | + return $this; |
| 102 | + } |
| 103 | + /** |
| 104 | + * |
| 105 | + * @param string $key_tomatch |
| 106 | + * @return $this |
| 107 | + */ |
| 108 | + function match(string $key_tomatch){ |
| 109 | + $this->checkExist($key_tomatch); |
| 110 | + 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])) { |
| 111 | + $message = [ |
| 112 | + "type" => "string.match", |
| 113 | + "message" => "`{$this->string_item}` should match {$key_tomatch}", |
| 114 | + "label" => $this->string_item, |
| 115 | + ]; |
| 116 | + $this->addError($message); |
| 117 | + } |
| 118 | + return $this; |
| 119 | + } |
| 120 | + function required(){ |
| 121 | + $required_value= trim($this->string_value); |
| 122 | + if (empty($required_value)) { |
| 123 | + $message = [ |
| 124 | + "type"=> "any.required", |
| 125 | + "message" => "`{$this->string_item}` is required", |
| 126 | + "label" => $this->string_item, |
| 127 | + ]; |
| 128 | + $this->addError($message); |
| 129 | + } |
| 130 | + return $this; |
| 131 | + } |
| 132 | + /** |
| 133 | + * |
| 134 | + * @param string $itemKey |
| 135 | + * @return boolean |
| 136 | + */ |
| 137 | + private function checkExist(string $itemKey=null){ |
| 138 | + $item_to_check=$itemKey?$itemKey:$this->string_item; |
| 139 | + $regex="#[a-zA-Z0-9]#"; |
| 140 | + $status_keys_exist=true; |
| 141 | + if (!isset($this->source_data[$item_to_check])) { |
| 142 | + $message = [ |
| 143 | + "type"=> "any.unknow", |
| 144 | + "message" => "`{$item_to_check}` is unknow", |
| 145 | + "label" => $item_to_check, |
| 146 | + ]; |
| 147 | + $this->addError($message); |
| 148 | + return false; |
| 149 | + }else if(!preg_match($regex,$this->source_data[$item_to_check]) || strlen(trim($this->source_data[$item_to_check]))==0){ |
| 150 | + $message=[ |
| 151 | + "type" => "string.unknow", |
| 152 | + "message" => "`{$item_to_check}` shoud be a s tring", |
| 153 | + "label" => $item_to_check, |
| 154 | + ]; |
| 155 | + $this->addError($message); |
| 156 | + return false; |
| 157 | + } |
| 158 | + return $status_keys_exist; |
| 159 | + }/** |
| 160 | + * |
| 161 | + * @param array $value |
| 162 | + * @return type |
| 163 | + */ |
| 164 | + private function addError(array $value){ |
| 165 | + return $this->_errors[]=$value; |
| 166 | + }/** |
| 167 | + * |
| 168 | + * @return type |
| 169 | + */ |
| 170 | + function check(){ |
| 171 | + return $this->_errors; |
| 172 | + } |
| 173 | +} |
0 commit comments