1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sokil\FraudDetector\Processor; |
4
|
|
|
|
5
|
|
|
class VariableProcessor extends AbstractProcessor |
6
|
|
|
{ |
7
|
|
|
const CONDITION_EXISTS = 'exists'; |
8
|
|
|
const CONDITION_EQUALS = '=='; |
9
|
|
|
const CONDITION_NOT_EQUALS = '!='; |
10
|
|
|
const CONDITION_GREATER = '>'; |
11
|
|
|
const CONDITION_LOWER = '<'; |
12
|
|
|
const CONDITION_INSTANCEOF = 'instanceof'; |
13
|
|
|
|
14
|
|
|
private $name; |
15
|
|
|
|
16
|
|
|
private $conditionList; |
17
|
|
|
|
18
|
|
|
private $variableType; |
19
|
|
|
|
20
|
|
|
public function setName($name, $variableType = 'GLOBALS') |
21
|
|
|
{ |
22
|
|
|
$this->name = $name; |
23
|
|
|
|
24
|
|
|
$allowedVariableTypes = array( |
25
|
|
|
'GLOBALS', |
26
|
|
|
'_GET', |
27
|
|
|
'_POST', |
28
|
|
|
'_SERVER', |
29
|
|
|
'_REQUEST', |
30
|
|
|
'_SESSION', |
31
|
|
|
'_COOKIE', |
32
|
|
|
'_FILES', |
33
|
|
|
'_ENV', |
34
|
|
|
); |
35
|
|
|
|
36
|
|
|
if(!in_array($variableType, $allowedVariableTypes)) { |
37
|
|
|
throw new \Exception('Variable type must be one of ' . implode(', ', $allowedVariableTypes)); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$this->variableType = $variableType; |
41
|
|
|
|
42
|
|
|
return $this; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
protected function getValue() |
46
|
|
|
{ |
47
|
|
|
switch($this->variableType) { |
48
|
|
|
case 'GLOBALS' : return $GLOBALS[$this->name]; |
49
|
|
|
case '_GET' : return $_GET[$this->name]; |
50
|
|
|
case '_POST' : return $_POST[$this->name]; |
51
|
|
|
case '_SERVER' : return $_SERVER[$this->name]; |
52
|
|
|
case '_REQUEST' : return $_REQUEST[$this->name]; |
53
|
|
|
case '_SESSION' : return $_SESSION[$this->name]; |
54
|
|
|
case '_COOKIE' : return $_COOKIE[$this->name]; |
55
|
|
|
case '_FILES' : return $_FILES[$this->name]; |
56
|
|
|
case '_ENV' : return $_ENV[$this->name]; |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function isPassed() |
61
|
|
|
{ |
62
|
|
|
foreach($this->conditionList as $condition => $conditionValue) { |
63
|
|
|
switch($condition) { |
64
|
|
|
|
65
|
|
|
case self::CONDITION_EQUALS: |
66
|
|
|
$passed = ($this->getValue() === $conditionValue); |
67
|
|
|
break; |
68
|
|
|
|
69
|
|
|
case self::CONDITION_NOT_EQUALS: |
70
|
|
|
$passed = ($this->getValue() !== $conditionValue); |
71
|
|
|
break; |
72
|
|
|
|
73
|
|
|
case self::CONDITION_GREATER: |
74
|
|
|
$passed = ($this->getValue() > $conditionValue); |
75
|
|
|
break; |
76
|
|
|
|
77
|
|
|
case self::CONDITION_LOWER: |
78
|
|
|
$passed = ($this->getValue() < $conditionValue); |
79
|
|
|
break; |
80
|
|
|
|
81
|
|
|
case self::CONDITION_INSTANCEOF: |
82
|
|
|
$passed = ($this->getValue() instanceof $conditionValue); |
83
|
|
|
break; |
84
|
|
|
|
85
|
|
|
default: |
86
|
|
|
throw new \Exception('Wrong condition ' . $condition . ' specified'); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
if(!$passed) { |
90
|
|
|
return false; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return true; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function equals($value) |
98
|
|
|
{ |
99
|
|
|
$this->conditionList[self::CONDITION_EQUALS] = $value; |
100
|
|
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function notEquals($value) |
104
|
|
|
{ |
105
|
|
|
$this->conditionList[self::CONDITION_NOT_EQUALS] = $value; |
106
|
|
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function greater($value) |
110
|
|
|
{ |
111
|
|
|
$this->conditionList[self::CONDITION_GREATER] = $value; |
112
|
|
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function lower($value) |
116
|
|
|
{ |
117
|
|
|
$this->conditionList[self::CONDITION_LOWER] = $value; |
118
|
|
|
return $this; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function isInstanceOf($value) |
122
|
|
|
{ |
123
|
|
|
$this->conditionList[self::CONDITION_INSTANCEOF] = $value; |
124
|
|
|
return $this; |
125
|
|
|
} |
126
|
|
|
} |