This repository was archived by the owner on Feb 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRequestParser.php
More file actions
118 lines (99 loc) · 2.87 KB
/
RequestParser.php
File metadata and controls
118 lines (99 loc) · 2.87 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
114
115
116
117
118
<?php
require_once 'Request.php';
/**
* Request parser
*
* @version 0.2
* @author Thiago Delgado Pinto
*/
class RequestParser {
/** Return a new {@link Request} object with the request content. */
static function createObject() {
return new Request(
self::method(),
self::contentType(),
self::uri(),
self::parameters()
);
}
/** Return the server addreess */
static function serverAddress() {
return self::val( $_SERVER, 'SERVER_ADDR' );
}
/** Return true if the server address is 127.0.0.1 */
static function isLocalServer() {
return '127.0.0.1' === self::serverAddress();
}
/** Return request's URI. */
static function uri() {
return self::val( $_SERVER, 'REQUEST_URI' );
}
/** Return request's path info. */
static function pathInfo() {
return self::val( $_SERVER, 'PATH_INFO' );
}
/** Return request's HTTP method. */
static function method() {
return self::val( $_SERVER, 'REQUEST_METHOD' );
}
/** Return request's content type. */
static function contentType() {
// 1st try: It is necessary to config .htaccess for getting CONTENT_TYPE
$value = self::val( $_SERVER, 'CONTENT_TYPE' );
// 2nd try: Getting all headers
if ( ! isset( $value ) ) {
$accept = self::val( getallheaders(), 'Accept' );
if ( isset( $accept ) ) {
$content = explode( ',', $accept );
if ( count( $content ) > 0 ) {
$value = $content[ 0 ];
}
}
}
return $value;
}
/** Return request's query string. */
static function queryString() {
return self::val( $_SERVER, 'QUERY_STRING' );
}
/** Return request's query array */
static function queryArray() {
if ( isset( $_SERVER[ 'QUERY_STRING' ] ) ) {
$params = array();
parse_str( $_SERVER[ 'QUERY_STRING' ], $params );
return $params;
}
return null;
}
/** Return an array with the request parameters */
static function parameters() {
$params = self::queryArray();
if ( ! isset( $params ) ) { $params = array(); }
// PUT/POST incoming stream
$content = file_get_contents( 'php://input' );
$fields = array();
switch ( self::contentType() ) {
case Request::CONTENT_HTML: $fields = json_decode( $content ); break;
case Request::CONTENT_JSON: parse_str( $content, $fields ); break;
default: return $params;
}
// Add content from $fields to $params
foreach ( $fields as $key => $value ) {
$params[ $key ] = $value;
}
return $params;
}
/** Return a timestamp of the start of the request */
static function time() {
return self::val( $_SERVER, 'REQUEST_TIME' );
}
/** Return a timestamp of the start of the request, with microsecond precision */
static function timeFloat() {
return self::val( $_SERVER, 'REQUEST_TIME_FLOAT' );
}
/** Return the value for the given array key or {@code null} if not found. */
private static function val( array $array, $key ) {
return isset( $array[ $key ] ) ? $array[ $key ] : null;
}
}
?>