Completed
Push — 3.3 ( 5fced4...d53524 )
by Jerome
22:15 queued 11s
created

engine/classes/Elgg/Request.php (1 issue)

1
<?php
2
3
namespace Elgg;
4
5
use Elgg\Di\PublicContainer;
6
use Elgg\Http\Request as HttpRequest;
7
use Elgg\Router\Route;
8
use Elgg\Validation\ValidationResults;
9
10
/**
11
 * Request container
12
 */
13
class Request {
14
15
	/**
16
	 * @var HttpRequest
17
	 */
18
	private $http_request;
19
20
	/**
21
	 * @var PublicContainer
22
	 */
23
	private $dic;
24
25
	/**
26
	 * @var ValidationResults
27
	 */
28
	private $validation;
29
30
	/**
31
	 * Constructor
32
	 *
33
	 * @param PublicContainer $dic          DI container
34
	 * @param HttpRequest     $http_request Request
35
	 */
36
	public function __construct(PublicContainer $dic, HttpRequest $http_request) {
37
		$this->http_request = $http_request;
38
		$this->dic = $dic;
39
		$this->validation = new ValidationResults();
40
	}
41
42
	/**
43
	 * Access validation results bag
44
	 * @return ValidationResults
45
	 */
46
	public function validation() {
47
		return $this->validation;
48
	}
49
50
	/**
51
	 * Get the name of the route
52
	 *
53
	 * @return string
54
	 */
55
	public function getRoute() {
56
		return $this->getParam('_route');
57
	}
58
59
	/**
60
	 * Get the parameters from the request query and route matches
61
	 *
62
	 * @param bool $filter Sanitize input values
63
	 *
64
	 * @return array
65
	 */
66
	public function getParams($filter = true) {
67
		return $this->http_request->getParams($filter);
68
	}
69
70
	/**
71
	 * Get an element of the params array. If the params array is not an array,
72
	 * the default will always be returned.
73
	 *
74
	 * @param string $key     The key of the value in the params array
75
	 * @param mixed  $default The value to return if missing
76
	 * @param bool   $filter  Sanitize input value
77
	 *
78
	 * @return mixed
79
	 */
80
	public function getParam($key, $default = null, $filter = true) {
81
		return $this->http_request->getParam($key, $default, $filter);
82
	}
83
84
	/**
85
	 * Set request parameter
86
	 * @see set_input()
87
	 *
88
	 * @param string $key   Parameter name
89
	 * @param mixed  $value Value
90
	 *
91
	 * @return void
92
	 */
93
	public function setParam($key, $value = null) {
94
		$this->http_request->setParam($key, $value);
95
	}
96
97
	/**
98
	 * Gets the "entity" key from the params if it holds an Elgg entity
99
	 *
100
	 * @param string $key Input key to pull entity GUID from
101
	 *
102
	 * @return \ElggEntity|null
103
	 */
104
	public function getEntityParam($key = 'guid') {
105
		$guid = $this->http_request->getParam($key);
106
		if ($guid) {
107
			$entity = get_entity($guid);
108
			if ($entity instanceof \ElggEntity) {
1 ignored issue
show
$entity is always a sub-type of ElggEntity.
Loading history...
109
				return $entity;
110
			}
111
		}
112
113
		return null;
114
	}
115
116
	/**
117
	 * Gets the "user" key from the params if it holds an Elgg user
118
	 *
119
	 * @param string $key Input key to pull user GUID from, or "username" for a username.
120
	 *
121
	 * @return \ElggUser|null
122
	 */
123
	public function getUserParam($key = 'user_guid') {
124
		$prop = $this->http_request->getParam($key);
125
		if ($key === 'username') {
126
			$entity = get_user_by_username($prop);
127
128
			return $entity ? : null;
129
		}
130
131
		if ($prop) {
132
			$entity = get_entity($prop);
133
			if ($entity instanceof \ElggUser) {
134
				return $entity;
135
			}
136
		}
137
138
		return null;
139
	}
140
141
	/**
142
	 * Get the DI container
143
	 *
144
	 * @return PublicContainer
145
	 */
146
	public function elgg() {
147
		return $this->dic;
148
	}
149
150
	/**
151
	 * Get URL of the request
152
	 * @return string
153
	 */
154
	public function getURL() {
155
		return $this->http_request->getCurrentURL();
156
	}
157
158
	/**
159
	 * Get relative path of the request
160
	 * @return string
161
	 */
162
	public function getPath() {
163
		return implode('/', $this->http_request->getUrlSegments());
164
	}
165
166
	/**
167
	 * Is the route access with XmlHttpRequest
168
	 * @return bool
169
	 */
170
	public function isXhr() {
171
		return $this->http_request->isXmlHttpRequest();
172
	}
173
174
	/**
175
	 * Get HTTP method of the request
176
	 * @return string
177
	 */
178
	public function getMethod() {
179
		return $this->http_request->getMethod();
180
	}
181
182
	/**
183
	 * Get the HttpRequest for this request
184
	 *
185
	 * @return \Elgg\Http\Request
186
	 */
187
	public function getHttpRequest() {
188
		return $this->http_request;
189
	}
190
}
191