Source of file Request.php
Size: 7,855 Bytes - Last Modified: 2015-12-22T09:12:14-05:00
../src/Request.php
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
Covered by 2 test(s):
63
Covered by 2 test(s):
64
Covered by 2 test(s):
65
Covered by 2 test(s):
66
Covered by 2 test(s):
67
Covered by 2 test(s):
68
Covered by 2 test(s):
6970717273747576777879
Covered by 8 test(s):
80
Covered by 8 test(s):
81
Covered by 8 test(s):
82
Covered by 8 test(s):
83
Covered by 8 test(s):
848586
Covered by 8 test(s):
87
Covered by 6 test(s):
88
Covered by 4 test(s):
8990
Covered by 3 test(s):
91
Covered by 3 test(s):
92
Covered by 3 test(s):
93
Covered by 8 test(s):
9495
Covered by 8 test(s):
96
Covered by 2 test(s):
97
Covered by 2 test(s):
9899
Covered by 8 test(s):
100101102103104105106107108109110111112
Covered by 3 test(s):
113
Covered by 3 test(s):
114115116117118119120121122123124125
Covered by 4 test(s):
126127
Covered by 4 test(s):
128
Covered by 4 test(s):
129
Covered by 2 test(s):
130
Covered by 2 test(s):
131
Covered by 2 test(s):
132133
Covered by 4 test(s):
134
Covered by 4 test(s):
135136137
Covered by 4 test(s):
138
Covered by 4 test(s):
139
Covered by 4 test(s):
140
Covered by 4 test(s):
141
Covered by 4 test(s):
142
Covered by 4 test(s):
143
Covered by 2 test(s):
144
Covered by 2 test(s):
145
Covered by 2 test(s):
146
Covered by 2 test(s):
147
Covered by 1 test(s):
148
Covered by 1 test(s):
149
Covered by 1 test(s):
150
Covered by 1 test(s):
151152153
Covered by 1 test(s):
154
Covered by 1 test(s):
155
Covered by 1 test(s):
156
Covered by 1 test(s):
157158
Covered by 4 test(s):
159160161162163164165166167168169
Covered by 4 test(s):
170171
Covered by 4 test(s):
172173174175176177178179180181
Covered by 1 test(s):
182183
Covered by 1 test(s):
184
Covered by 1 test(s):
185
Covered by 1 test(s):
186
Covered by 1 test(s):
187
Covered by 1 test(s):
188189
Covered by 1 test(s):
190191192193194195196197198199200
Covered by 4 test(s):
201
Covered by 1 test(s):
202203
Covered by 3 test(s):
204205206207208209210211212213214
Covered by 2 test(s):
215216
Covered by 2 test(s):
217218219220221222223224225226227
Covered by 1 test(s):
228
Covered by 1 test(s):
229230231
Covered by 1 test(s):
232233234235236237238239240241
Covered by 1 test(s):
242243244245246247248249250251
Covered by 7 test(s):
252253254255256257258259260261262
Covered by 4 test(s):
263
Covered by 1 test(s):
264265266
Covered by 4 test(s):
267268269270271272273274275276
Covered by 1 test(s):
277278279280281282283284285286
Covered by 5 test(s):
287
Covered by 5 test(s):
288
Covered by 5 test(s):
289
Covered by 2 test(s):
290
Covered by 2 test(s):
291
Covered by 2 test(s):
292
Covered by 2 test(s):
293294
Covered by 5 test(s):
295
Covered by 2 test(s):
296297298
Covered by 3 test(s):
299300301302303304305306307308309310311
Covered by 8 test(s):
312
Covered by 8 test(s):
313
Covered by 8 test(s):
314
Covered by 8 test(s):
315
Covered by 8 test(s):
316
Covered by 8 test(s):
317
Covered by 8 test(s):
318319
Covered by 8 test(s):
320
Covered by 1 test(s):
321322323
Covered by 7 test(s):
324325326
| <?php /** * Base Request * * @category PHP * @package MvcLite * @subpackage Request * @since File available since release 1.0.1 * @author Cory Collier <corycollier@corycollier.com> */ namespace MvcLite; use MvcLite\Traits\Singleton as SingletonTrait; use MvcLite\Traits\FilterChain as FilterChainTrait; /** * Base Request * * @category PHP * @package MvcLite * @subpackage Request * @since Class available since release 1.0.1 * @author Cory Collier <corycollier@corycollier.com> */ class Request extends ObjectAbstract { use SingletonTrait; use FilterChainTrait; /** * Constants */ const ERR_BAD_CONTENT_TYPE = 'Content type [%s] not recognized'; /** * associative array representing the request params * * @var array */ protected $params = []; /** * associative array of the headers sent from the client * * @var array */ protected $headers = []; /** * stores the original request uri * * @var string */ protected $uri; /** * method to start the request up */ public function init() { $this->uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : ''; $this->params = array_merge($this->params, $_COOKIE); $this->params = array_merge($this->params, $_POST); $this->params = array_merge($this->params, $_GET); $this->setHeaders($_SERVER); $this->setParams($this->buildFromString(@$_GET['q'])); } /** * * Method to set the headers. * * @return MvcLite\Request Returns $this, for object-chaining. */ public function setHeaders($headers = []) { // Create the filter chain to transform _SERVER to headers. $filter = $this->getFilterChain([ 'UnderscoreToDash', 'StringtoLower', ]); $filter->addFilter(new Filter\SeparatorToUcwords('-')); // iterate over the $_SERVER superglobal values. foreach ($headers as $key => $value) { if (substr($key, 0, 5) != 'HTTP_') { continue; } $key = $filter->filter($key); $key = strtr($key, ['Http-' => '']); $this->setHeader($key, $value); } if (isset($headers['CONTENT_TYPE'])) { $this->setHeader('Content-Type', $headers['CONTENT_TYPE']); } return $this; } /** * Sets a single header * * @param string $name The name of the header to set. * @param string $value The value of the header. * * @return MvcLite\Request Returns $this, for object-chaining. */ public function setHeader($name, $value) { $this->headers[$name] = $value; return $this; } /** * Build an associative array from a string * * @param string $string * @param string $separator * @return array */ public function buildFromString($string = '', $separator = '/') { global $argv; // create a list of parts by separator $parts = array_filter(explode($separator, $string)); if (!$parts && PHP_SAPI == 'cli') { $parts = $argv; array_shift($parts); } $controller = array_shift($parts); $action = array_shift($parts); $results = [ 'controller' => $controller ? $controller : 'index', 'action' => $action ? $action : 'index', ]; $length = count($parts); $i = 0; while ($i < $length) { if (array_key_exists($i + 1, $parts)) { $key = $parts[$i]; $value = $parts[$i + 1]; if (in_array($key, ['controller', 'action'])) { if ($i <= 2) { $i = $i + 2; } continue; } $results[$key] = $value; } $i = $i + 2; } return $results; } /** * setter for the params property * * @param $params * @return Request $this for object-chaining. */ public function setParams($params = []) { $this->params = array_merge($this->params, (array)$params); return $this; } /** * getter for the params property * * @return array All of the request params (_GET, _POST, and _COOKIE) */ public function getParams() { $params = $this->params; foreach ($params as $key => $value) { if (! $key || ! $value || $key === 'q') { unset($params[$key]); } } return $params; } /** * Gets a single param from the params array * * @param string $param * @return string */ public function getParam($param) { if (array_key_exists($param, $this->params)) { return $this->params[$param]; } } /** * method to set a param manually * * @param string $param * @param string $value * @return Request $this for object-chaining. */ public function setParam($param, $value = '') { $this->params[$param] = $value; return $this; } /** * Determines if the request is post or not * * @return boolean */ public function isPost() { // if there is data in the _post property, return true if (count($_POST)) { return true; } return false; } /** * getter for the headers property * * @return array */ public function getHeaders() { return $this->headers; } /** * method to get the value for a single header * * @param string $header */ public function getHeader($header = '') { return @$this->headers[$header]; } /** * Method to indicate whether the current request is AJAX or not * * @return boolean */ public function isAjax() { // if the request is ajax, don't load the layout if ($this->getHeader('X-Requested-With') == 'XMLHttpRequest') { return true; } return false; } /** * getter for the uri value * * @return string */ public function getUri() { return $this->uri; } /** * Getter for the content type of the request. * * @return string The content type. */ public function getContentType() { $contentType = $this->getHeader('Content-Type'); $contentType = trim(explode(';', $contentType)[0]); if (! $contentType) { $accept = $this->getHeader('Accept'); $parts = explode(',', $accept); $contentType = $parts[0]; } if (! $contentType && PHP_SAPI == 'cli') { return 'text/plain'; } return $contentType ? $contentType : 'text/html'; } /** * Gets a friendly version of the format. * * @param string $contentType The raw content type. * * @return string The machine friendly name for the request type (aka Format). */ public function getFormat($contentType) { $map = [ 'application/json' => 'json', 'application/javascript' => 'json', 'text/html' => 'html', 'text/plain' => 'text', 'text/csv' => 'csv', '*/*' => 'json', ]; if (!array_key_exists($contentType, $map)) { throw new Exception(sprintf(self::ERR_BAD_CONTENT_TYPE, $contentType)); } return $map[$contentType]; } } |