1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package presentation |
4
|
|
|
* @subpackage requests |
5
|
|
|
* @author marius orcsik <[email protected]> |
6
|
|
|
* @date 2011.08.11 |
7
|
|
|
*/ |
8
|
|
|
namespace vsc\presentation\requests; |
9
|
|
|
|
10
|
|
|
class RawHttpRequest extends RwHttpRequest { |
11
|
|
|
protected $aRawVars = []; |
12
|
|
|
protected $sRawInput; |
13
|
|
|
|
14
|
1 |
|
public function __construct() { |
15
|
1 |
|
parent::__construct(); |
16
|
|
|
|
17
|
1 |
|
if (isset($_SERVER) && !$this->isGet()) { |
18
|
1 |
|
$this->constructRawVars(); |
19
|
|
|
} |
20
|
1 |
|
} |
21
|
|
|
|
22
|
|
|
// this seems quite unsafe |
23
|
1 |
|
public function setRawVars($aVars) { |
24
|
1 |
|
if (is_array($aVars)) { |
25
|
1 |
|
if (is_array($this->aRawVars)) { |
26
|
|
|
$this->aRawVars = array_merge($aVars, $this->aRawVars); |
27
|
|
|
} else { |
28
|
1 |
|
$this->aRawVars = $aVars; |
29
|
|
|
} |
30
|
|
|
} |
31
|
1 |
|
} |
32
|
|
|
|
33
|
3 |
|
public function getRawVars() { |
34
|
3 |
|
if (!empty($this->aRawVars)) { |
35
|
|
|
return $this->aRawVars; |
36
|
|
|
} |
37
|
3 |
|
$sContentType = $this->getContentType(); |
38
|
|
|
|
39
|
3 |
|
$vars = array(); |
40
|
3 |
|
switch ($sContentType) { |
41
|
3 |
|
case 'application/x-www-form-urlencoded': |
42
|
|
|
parse_str($this->sRawInput, $vars); |
43
|
|
|
break; |
44
|
3 |
|
case 'application/json': |
45
|
1 |
|
$vars = json_decode($this->sRawInput, true); |
46
|
1 |
|
break; |
47
|
2 |
|
case 'application/xml': |
48
|
|
|
default: |
49
|
2 |
|
break; |
50
|
|
|
} |
51
|
3 |
|
if (!empty ($vars)) { |
52
|
1 |
|
$this->aRawVars = $vars; |
|
|
|
|
53
|
|
|
} |
54
|
3 |
|
return $this->aRawVars; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param string $sVarName |
59
|
|
|
* @return mixed |
60
|
|
|
*/ |
61
|
1 |
|
public function getRawVar($sVarName) { |
62
|
1 |
|
$aRawVars = $this->getRawVars(); |
63
|
1 |
|
if (is_array($aRawVars) && array_key_exists($sVarName, $aRawVars)) { |
64
|
1 |
|
return self::getDecodedVar($aRawVars[$sVarName]); |
65
|
|
|
} else { |
66
|
|
|
return null; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
1 |
|
public function getVars() { |
71
|
1 |
|
$aRawVars = $this->getRawVars(); |
72
|
1 |
|
$aParentVars = parent::getVars(); |
73
|
1 |
|
if (!is_array($aRawVars)) { |
74
|
|
|
return parent::getVars(); |
75
|
|
|
} else { |
76
|
1 |
|
if (is_array($aParentVars)) { |
77
|
1 |
|
return array_merge($aRawVars, $aParentVars); |
78
|
|
|
} |
79
|
|
|
return $aRawVars; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
2 |
|
public function hasVar($sVarName) { |
84
|
|
|
return ( |
85
|
2 |
|
$this->hasRawVar($sVarName) || |
86
|
2 |
|
parent::hasVar($sVarName) |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
|
90
|
1 |
|
public function hasRawVar($sVarName) { |
91
|
1 |
|
$aRawVars = $this->getRawVars(); |
92
|
1 |
|
return (is_array($aRawVars) && array_key_exists($sVarName, $aRawVars)); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param string $sVarName |
97
|
|
|
* @return mixed |
98
|
|
|
*/ |
99
|
1 |
|
public function getVar($sVarName) { |
100
|
1 |
|
$mValue = parent::getVar($sVarName); |
101
|
1 |
|
if (!$mValue) { |
102
|
1 |
|
$mValue = $this->getRawVar($sVarName); |
103
|
|
|
} |
104
|
1 |
|
return $mValue; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return string |
109
|
|
|
*/ |
110
|
|
|
protected function getRawInput() { |
111
|
|
|
return file_get_contents('php://input'); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @todo this has to be moved in the rw url handler |
116
|
|
|
* @param string $sRawInput |
|
|
|
|
117
|
|
|
* @return void |
118
|
|
|
*/ |
119
|
5 |
|
protected function constructRawVars($sRawInput = null) { |
120
|
5 |
|
if (is_null($sRawInput)) { |
121
|
5 |
|
$this->sRawInput = $this->getRawInput(); |
122
|
|
|
} else { |
123
|
3 |
|
$this->sRawInput = $sRawInput; |
124
|
|
|
} |
125
|
5 |
|
$this->aRawVars = null; |
|
|
|
|
126
|
5 |
|
} |
127
|
|
|
} |
128
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..