WHttpRequest.php
3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
/**
* @author Weavora Team <hello@weavora.com>
* @link http://weavora.com
* @copyright Copyright (c) 2011 Weavora LLC
*/
class WHttpRequest extends CHttpRequest
{
private $_restParams = array();
/**
* Default response format
* either 'json' or 'xml'
*/
private $_format = 'json';
private $_formatAttributeName = 'format';
protected $_availableFormats = array('json','xml');
public function getPut($name, $defaultValue = null)
{
if ($this->_restParams === array())
$this->_restParams = array_merge($this->getIsPutRequest() ? $this->getRestParams() : array(), $this->_restParams);
return isset($this->_restParams[$name]) ? $this->_restParams[$name] : $defaultValue;
}
public function getDelete($name, $defaultValue = null)
{
if ($this->_restParams === array())
$this->_restParams = array_merge($this->getIsDeleteRequest() ? $this->getRestParams() : array(), $this->_restParams);
return isset($this->_restParams[$name]) ? $this->_restParams[$name] : $defaultValue;
}
/**
* return all posible params from request
* @return array()
*/
public function getAllRestParams($ignorInlineParams = false)
{
if ($this->_restParams === array())
$this->_restParams = array_merge(($this->getIsDeleteRequest() || $this->getIsPutRequest()) ? $this->getRestParams() : $_REQUEST, $this->_restParams);
if($ignorInlineParams){
$result = array();
foreach ($this->_restParams as $key => $val){
if(!preg_match('|^_|si', $key)){
$result[$key] = $val;
}
}
return $result;
}
return $this->_restParams;
}
public function parseJsonParams(){
if(!isset($_SERVER['CONTENT_TYPE'])){
return $this->_restParams;
}
$contentType = strtok($_SERVER['CONTENT_TYPE'], ';');
if($contentType == 'application/json'){
$requestBody = file_get_contents("php://input");
$this->_restParams = array_merge((array)json_decode($requestBody), $this->_restParams);
}
return $this->_restParams;
}
/**
* Returns the named GET or POST parameter value.
* If the GET or POST parameter does not exist, the second parameter to this method will be returned.
* If both GET and POST contains such a named parameter, the GET parameter takes precedence.
* @param string $name the GET parameter name
* @param mixed $defaultValue the default parameter value if the GET parameter does not exist.
* @return mixed the GET parameter value
* @since 1.0.4
* @see getQuery
* @see getPost
*/
public function getParam($name,$defaultValue=null)
{
if(!isset($_GET[$name]) && isset($_GET['_'.$name])){
$name = '_'.$name;
}
$param = isset($_GET[$name]) ? $_GET[$name] : null;
if(!$param)
$param = isset($_GET[$name]) ? $_GET[$name] : null;
if(!$param)
$param = isset($this->_restParams[$name]) ? $this->_restParams[$name] : null;
return $param ? $param : $defaultValue;
}
public function setFormat($format = null)
{
if ($format && in_array($format, $this->_availableFormats)) {
$this->_format = $format;
}
if (!$this->_format) {
//get format from one of requests type
$format = Yii::app()->request->getParam($this->_formatAttributeName);
$format = (empty($format)) ? Yii::app()->request->getPut($this->_formatAttributeName) : $format;
$format = (empty($format)) ? Yii::app()->request->getDelete($this->_formatAttributeName) : $format;
$this->_format = $format;
}
}
public function getFormat(){
return $this->_format;
}
}