Skip to content

Commit 150ebb1

Browse files
Update UltimateValidator.php
1 parent 333ef70 commit 150ebb1

File tree

1 file changed

+61
-34
lines changed

1 file changed

+61
-34
lines changed

src/UltimateValidator.php

Lines changed: 61 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,13 @@ public function submit(array $data, ?bool $allowedType = false, callable $before
129129
//operator function checker
130130
$this->operator = $this->operatorMethod($keyPair);
131131

132-
if($this->operator){
132+
if($this->operator === 'error'){
133+
$this->error = false;
134+
$this->message[$keyPair['variable']] = sprintf("Comparison Operator error for this variable `%s`", $keyPair['variable']);
135+
break;
136+
}
137+
138+
elseif(!is_null($this->operator) && $this->operator){
133139
if(!in_array($keyPair['variable'], array_keys($this->message))){
134140
$this->message[$keyPair['variable']] = $message;
135141
}
@@ -213,28 +219,28 @@ public function echoJson(int $response, $message = null)
213219
*/
214220
private function createFlags(array $flag)
215221
{
216-
switch($flag['filter']){
217-
case ($flag['filter'] === 'email' || $flag['filter'] === 'e'):
222+
switch($flag['data_type']){
223+
case ($flag['data_type'] === 'email' || $flag['data_type'] === 'e'):
218224
$type = filter_input($this->type, $flag['variable'], FILTER_VALIDATE_EMAIL);
219225
break;
220226

221-
case ($flag['filter'] === 'int' || $flag['filter'] === 'i'):
227+
case ($flag['data_type'] === 'int' || $flag['data_type'] === 'i'):
222228
$type = filter_input($this->type, $flag['variable'], FILTER_VALIDATE_INT);
223229
break;
224230

225-
case ($flag['filter'] === 'float' || $flag['filter'] === 'f'):
231+
case ($flag['data_type'] === 'float' || $flag['data_type'] === 'f'):
226232
$type = filter_input($this->type, $flag['variable'], FILTER_VALIDATE_FLOAT);
227233
break;
228234

229-
case ($flag['filter'] === 'url' || $flag['filter'] === 'u'):
235+
case ($flag['data_type'] === 'url' || $flag['data_type'] === 'u'):
230236
$type = filter_input($this->type, $flag['variable'], FILTER_VALIDATE_URL);
231237
break;
232238

233-
case ($flag['filter'] === 'array' || $flag['filter'] === 'a'):
239+
case ($flag['data_type'] === 'array' || $flag['data_type'] === 'a'):
234240
$type = isset($this->param[$flag['variable']]) && is_array($this->param[$flag['variable']]) ? true : false;
235241
break;
236242

237-
case ($flag['filter'] === 'bool' || $flag['filter'] === 'b'):
243+
case ($flag['data_type'] === 'bool' || $flag['data_type'] === 'b'):
238244
$type = filter_input($this->type, $flag['variable'], FILTER_VALIDATE_BOOLEAN);
239245
break;
240246

@@ -255,10 +261,13 @@ private function createFlags(array $flag)
255261
private function operatorMethod(?array $flag = null)
256262
{
257263
$this->operator = null;
258-
if(isset($flag['operator']) && !empty($flag['operator']) ){
259-
$this->operator = $this->createOperator($flag);
264+
//comparison operator command
265+
if(isset($flag['operator']) && !empty($flag['operator'])){
266+
$this->operator = 'error';
267+
//value check command
268+
if(isset($flag['value']))
269+
$this->operator = $this->createOperator($flag);
260270
}
261-
262271
return $this->operator;
263272
}
264273

@@ -269,65 +278,78 @@ private function operatorMethod(?array $flag = null)
269278
*/
270279
private function createOperator($flag)
271280
{
281+
$this->operator = false;
282+
$flagOperator = $flag['operator'];
283+
$flagValue = $flag['value'];
272284

273285
//equal to operator
274-
if(count(explode("==", $flag['operator'])) === 2)
286+
if(substr_count($flagOperator, "=") === 2)
287+
{
288+
$dataString = $this->param[$flag['variable']];
289+
if($dataString == $flagValue){
290+
$this->operator = true;
291+
}
292+
}
293+
294+
//strictly equal to operator
295+
elseif(substr_count($flagOperator, "=") === 3)
275296
{
276297
$dataString = $this->param[$flag['variable']];
277-
$operatorValue = explode("==", $flag['operator'])[1];
278-
$this->operator = false;
279-
if($dataString == $operatorValue){
298+
if($dataString === $flagValue){
280299
$this->operator = true;
281300
}
282301
}
283302

284303
//not equal to operator
285-
elseif(count(explode("!=", $flag['operator'])) === 2)
304+
elseif(substr_count($flagOperator, "!=") === 1)
286305
{
287306
$dataString = $this->param[$flag['variable']];
288-
$operatorValue = explode("!=", $flag['operator'])[1];
289-
$this->operator = false;
290-
if($dataString != $operatorValue){
307+
if($dataString != $flagValue){
291308
$this->operator = true;
292309
}
293310
}
294311

295-
//greater than operator
296-
elseif(count(explode(">", $flag['operator'])) === 2)
312+
//strictly not equal to operator
313+
elseif(substr_count($flagOperator, "!==") === 1)
297314
{
298315
$dataString = $this->param[$flag['variable']];
299-
$operatorValue = explode(">", $flag['operator'])[1];
300-
$this->operator = false;
316+
if($dataString !== $flagValue){
317+
$this->operator = true;
318+
}
319+
}
301320

302-
//string
321+
//greater than operator
322+
elseif(substr_count($flagOperator, ">") === 1)
323+
{
324+
$dataString = $this->param[$flag['variable']];
325+
//string data type
303326
if((float) $dataString == 0){
304-
if(strlen($dataString) > $operatorValue){
327+
if(strlen($dataString) > $flagValue){
305328
$this->operator = true;
306329
}
307330
}else{
308-
if((float) $dataString > $operatorValue){
331+
if((float) $dataString > $flagValue){
309332
$this->operator = true;
310333
}
311334
}
312335
}
313336

314337
//less than operator
315-
elseif(count(explode("<", $flag['operator'])) === 2)
338+
elseif(substr_count($flagOperator, "<") === 1)
316339
{
317340
$dataString = $this->param[$flag['variable']];
318-
$operatorValue = explode("<", $flag['operator'])[1];
319-
$this->operator = false;
341+
//for string data type
320342
if((float) $dataString == 0){
321-
if(strlen($dataString) < $operatorValue){
343+
if(strlen($dataString) < $flagValue){
322344
$this->operator = true;
323345
}
324346
}else{
325-
if((float) $dataString < $operatorValue){
347+
if((float) $dataString < $flagValue){
326348
$this->operator = true;
327349
}
328350
}
329351
}
330-
352+
331353
return $this->operator;
332354
}
333355

@@ -344,21 +366,26 @@ private function getKeyPairs(string $key)
344366
//explode indicator
345367
$find_occur = substr_count($key, ':');
346368

347-
if(count($flags) > 3 || $find_occur > 2 || !isset($flags[1])){
369+
if(count($flags) > 4 || $find_occur > 3 || !isset($flags[1])){
348370
return "exception";
349371
}
350372

351-
$flags['filter'] = $flags[0];
373+
$flags['data_type'] = $flags[0];
352374
$flags['variable'] = $flags[1];
353375

354376
//create operator
355377
if(isset($flags[2])){
356378
$flags['operator'] = $flags[2];
357379
}
380+
if(isset($flags[3])){
381+
$flags['value'] = $flags[3];
382+
}
358383

359384
unset($flags[0]);
360385
unset($flags[1]);
361386
unset($flags[2]);
387+
unset($flags[3]);
388+
362389
return $flags;
363390
}
364391

0 commit comments

Comments
 (0)