Source of file Request.php
Size: 8,567 Bytes - Last Modified: 2016-01-10T08:00:01-05:00
../src/Request.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
Covered by 2 test(s):
70
Covered by 2 test(s):
71
Covered by 2 test(s):
72
Covered by 2 test(s):
73
Covered by 2 test(s):
74
Covered by 2 test(s):
75
Covered by 2 test(s):
76
Covered by 2 test(s):
7778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
Covered by 8 test(s):
114
Covered by 8 test(s):
115
Covered by 8 test(s):
116
Covered by 8 test(s):
117
Covered by 8 test(s):
118119120
Covered by 8 test(s):
121
Covered by 6 test(s):
122
Covered by 4 test(s):
123124
Covered by 3 test(s):
125
Covered by 3 test(s):
126
Covered by 3 test(s):
127
Covered by 8 test(s):
128129
Covered by 8 test(s):
130
Covered by 2 test(s):
131
Covered by 2 test(s):
132133
Covered by 8 test(s):
134135136137138139140141142143144145146
Covered by 3 test(s):
147
Covered by 3 test(s):
148149150151152153154155156157158159
Covered by 4 test(s):
160161
Covered by 4 test(s):
162
Covered by 4 test(s):
163
Covered by 2 test(s):
164
Covered by 2 test(s):
165
Covered by 2 test(s):
166167
Covered by 4 test(s):
168
Covered by 4 test(s):
169170171
Covered by 4 test(s):
172
Covered by 4 test(s):
173
Covered by 4 test(s):
174
Covered by 4 test(s):
175
Covered by 4 test(s):
176
Covered by 4 test(s):
177
Covered by 2 test(s):
178
Covered by 2 test(s):
179
Covered by 2 test(s):
180
Covered by 2 test(s):
181
Covered by 1 test(s):
182
Covered by 1 test(s):
183
Covered by 1 test(s):
184
Covered by 1 test(s):
185186187
Covered by 1 test(s):
188
Covered by 1 test(s):
189
Covered by 1 test(s):
190
Covered by 1 test(s):
191192
Covered by 4 test(s):
193194195196197198199200201202203
Covered by 4 test(s):
204205
Covered by 4 test(s):
206207208209210211212213214215
Covered by 1 test(s):
216217
Covered by 1 test(s):
218
Covered by 1 test(s):
219
Covered by 1 test(s):
220
Covered by 1 test(s):
221
Covered by 1 test(s):
222223
Covered by 1 test(s):
224225226227228229230231232233234
Covered by 4 test(s):
235
Covered by 1 test(s):
236237
Covered by 3 test(s):
238239240241242243244245246247248
Covered by 2 test(s):
249250
Covered by 2 test(s):
251252253254255256257258259260261
Covered by 1 test(s):
262
Covered by 1 test(s):
263264265
Covered by 1 test(s):
266267268269270271272273274275
Covered by 1 test(s):
276277278279280281282283284285
Covered by 7 test(s):
286287288289290291292293294295296
Covered by 4 test(s):
297
Covered by 1 test(s):
298299300
Covered by 4 test(s):
301302303304305306307308309310
Covered by 1 test(s):
311312313314315316317318319320
Covered by 5 test(s):
321
Covered by 5 test(s):
322
Covered by 5 test(s):
323
Covered by 2 test(s):
324
Covered by 2 test(s):
325
Covered by 2 test(s):
326
Covered by 2 test(s):
327328
Covered by 5 test(s):
329
Covered by 2 test(s):
330331332
Covered by 3 test(s):
333334335336337338339340341342343344345
Covered by 8 test(s):
346
Covered by 8 test(s):
347
Covered by 8 test(s):
348
Covered by 8 test(s):
349
Covered by 8 test(s):
350
Covered by 8 test(s):
351
Covered by 8 test(s):
352353
Covered by 8 test(s):
354
Covered by 1 test(s):
355356357
Covered by 7 test(s):
358359360
| <?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'; /** * Holder for the request method. * * @var string */ protected $method = 'get'; /** * 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'])); $this->setMethod($_SERVER); } /** * Setter for the $method property. * * @param string $method The request method. * * @return MvcList\Request Returns $this, for object-chaining. */ public function setMethod($method) { if (is_array($method)) { $method = $method['REQUEST_METHOD']; } $this->method = $method; return $this; } /** * Getter for the method property. * * @return string The request method. */ public function getMethod() { return $this->method; } /** * * 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]; } } |