|
1
|
|
|
<?php declare (strict_types=1); |
|
2
|
|
|
/** |
|
3
|
|
|
* @license MIT |
|
4
|
|
|
* @author Samuel Adeshina <[email protected]> |
|
5
|
|
|
* |
|
6
|
|
|
* This file is part of the EmmetBlue project, please read the license document |
|
7
|
|
|
* available in the root level of the project |
|
8
|
|
|
*/ |
|
9
|
|
|
namespace EmmetBlue\Core; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class DirtChecker |
|
13
|
|
|
* |
|
14
|
|
|
* @author Samuel Adeshina <[email protected]> |
|
15
|
|
|
* |
|
16
|
|
|
* @since v0.0.1 14/12/2016 |
|
17
|
|
|
*/ |
|
18
|
|
|
class DirtChecker |
|
19
|
|
|
{ |
|
20
|
|
|
public $dirtFilterConditions = [">", "<", "="]; |
|
21
|
|
|
|
|
22
|
|
|
public $workingValue; |
|
23
|
|
|
public $conditionValue; |
|
24
|
|
|
public $workingConditions = []; |
|
25
|
|
|
|
|
26
|
|
|
public function __construct(string $value, string $conditionValue, array $conditions = []) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->workingValue = $value; |
|
29
|
|
|
$this->conditionValue = $conditionValue; |
|
30
|
|
|
|
|
31
|
|
|
if (!empty($conditions)){ |
|
32
|
|
|
foreach ($conditions as $value) { |
|
33
|
|
|
if (!in_array($value, $this->dirtFilterConditions)){ |
|
34
|
|
|
throw new \Exception("Invalid Dirt Filter Condition Provided: ".$value); |
|
35
|
|
|
} |
|
36
|
|
|
} |
|
37
|
|
|
$this->workingConditions = $conditions; |
|
38
|
|
|
} |
|
39
|
|
|
else { |
|
40
|
|
|
$this->workingConditions = $this->dirtFilterConditions; |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function isDirty() : bool |
|
45
|
|
|
{ |
|
46
|
|
|
$explodedString = explode(" ", $this->workingValue); |
|
47
|
|
|
foreach ($explodedString as $string){ |
|
48
|
|
|
foreach ($this->workingConditions as $condition){ |
|
49
|
|
|
$switchValue = false; |
|
50
|
|
|
switch ($condition) { |
|
51
|
|
|
case '=': |
|
52
|
|
|
$switchValue = strpos($string, $this->conditionValue) !== false; |
|
53
|
|
|
break; |
|
54
|
|
View Code Duplication |
case '>': |
|
|
|
|
|
|
55
|
|
|
$string = self::turnToNumber($string); |
|
56
|
|
|
$_condition = self::turnToNumber($this->conditionValue); |
|
57
|
|
|
|
|
58
|
|
|
$switchValue = $string > $_condition; |
|
59
|
|
|
break; |
|
60
|
|
View Code Duplication |
case '<': |
|
|
|
|
|
|
61
|
|
|
$string = self::turnToNumber($string); |
|
62
|
|
|
$_condition = self::turnToNumber($this->conditionValue); |
|
63
|
|
|
|
|
64
|
|
|
$switchValue = $string < $_condition; |
|
65
|
|
|
break; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
if (!$switchValue) { |
|
69
|
|
|
return true; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return false; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
private function turnToNumber(string $string) : float |
|
78
|
|
|
{ |
|
79
|
|
|
return (float) filter_var($string, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION | FILTER_FLAG_ALLOW_THOUSAND); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.