1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tomaj\NetteApi\Params; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
|
7
|
|
|
class InputParam implements ParamInterface |
8
|
|
|
{ |
9
|
|
|
const TYPE_POST = 'POST'; |
10
|
|
|
const TYPE_GET = 'GET'; |
11
|
|
|
const TYPE_FILE = 'FILE'; |
12
|
|
|
|
13
|
|
|
const OPTIONAL = false; |
14
|
|
|
const REQUIRED = true; |
15
|
|
|
|
16
|
|
|
private $type; |
17
|
|
|
|
18
|
|
|
private $key; |
19
|
|
|
|
20
|
|
|
private $required; |
21
|
|
|
|
22
|
|
|
private $availableValues; |
23
|
|
|
|
24
|
|
|
private $multi; |
25
|
|
|
|
26
|
|
|
public function __construct($type, $key, $required = self::OPTIONAL, $availableValues = null, $multi = false) |
27
|
|
|
{ |
28
|
|
|
$this->type = $type; |
29
|
|
|
$this->key = $key; |
30
|
|
|
$this->required = $required; |
31
|
|
|
$this->availableValues = $availableValues; |
32
|
|
|
$this->multi = $multi; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @return string |
37
|
|
|
*/ |
38
|
|
|
public function getType() |
39
|
|
|
{ |
40
|
|
|
return $this->type; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return string |
45
|
|
|
*/ |
46
|
|
|
public function getKey() |
47
|
|
|
{ |
48
|
|
|
return $this->key; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return boolean |
53
|
|
|
*/ |
54
|
|
|
public function isRequired() |
55
|
|
|
{ |
56
|
|
|
return $this->required; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function getAvailableValues() |
60
|
|
|
{ |
61
|
|
|
return $this->availableValues; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return bool |
66
|
|
|
*/ |
67
|
|
|
public function isMulti() |
68
|
|
|
{ |
69
|
|
|
return $this->multi; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function isValid() |
73
|
|
|
{ |
74
|
|
|
$value = $this->getValue(); |
75
|
|
|
if ($this->availableValues != null) { |
76
|
|
|
if (is_array($this->availableValues)) { |
77
|
|
|
return in_array($value, $this->availableValues); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if ($this->required) { |
82
|
|
|
if ($value == null || $value = '') { |
83
|
|
|
return false; |
84
|
|
|
} |
85
|
|
|
if (is_string($this->availableValues)) { |
86
|
|
|
return $value == $this->availableValues; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
return true; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function getValue() |
|
|
|
|
93
|
|
|
{ |
94
|
|
|
if ($this->type == self::TYPE_GET) { |
95
|
|
|
if (!filter_has_var(INPUT_GET, $this->key) && isset($_GET[$this->key])) { |
96
|
|
|
return $_GET[$this->key]; |
97
|
|
|
} |
98
|
|
|
return filter_input(INPUT_GET, $this->key); |
99
|
|
|
} |
100
|
|
|
if ($this->type == self::TYPE_POST) { |
101
|
|
|
if (!filter_has_var(INPUT_POST, $this->key) && isset($_POST[$this->key])) { |
102
|
|
|
return $_POST[$this->key]; |
103
|
|
|
} |
104
|
|
|
return filter_input(INPUT_POST, $this->key); |
105
|
|
|
} |
106
|
|
|
if ($this->type == self::TYPE_FILE) { |
107
|
|
|
if (isset($_FILES[$this->key])) { |
108
|
|
|
return $_FILES[$this->key]; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
throw new Exception("Invalid type"); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: