1: <?php
2: 3: 4: 5: 6: 7: 8: 9:
10:
11: namespace Kotchasan\Http;
12:
13: 14: 15: 16: 17: 18: 19:
20: class AbstractRequest extends AbstractMessage implements \Psr\Http\Message\RequestInterface
21: {
22: 23: 24:
25: protected $method = null;
26: 27: 28:
29: protected $requestTarget;
30: 31: 32:
33: protected $uri;
34:
35: 36: 37: 38: 39: 40: 41: 42:
43: public static function createUriWithGet($uri = 'index.php', $exclude = array())
44: {
45: $query = array();
46: self::map($query, $_GET, $exclude);
47: if (!empty($query)) {
48: $uri .= (strpos($uri, '?') === false ? '?' : '&').http_build_query($query);
49: }
50: return Uri::createFromUri($uri);
51: }
52:
53: 54: 55: 56: 57: 58: 59: 60:
61: public function createUriWithGlobals($uri = 'index.php', $exclude = array())
62: {
63: $query_str = array();
64: self::map($query_str, $_GET, $exclude);
65: self::map($query_str, $_POST, $exclude);
66: if (!empty($query_str)) {
67: $uri .= (strpos($uri, '?') === false ? '?' : '&').http_build_query($query_str);
68: }
69: return Uri::createFromUri($uri);
70: }
71:
72: 73: 74: 75: 76: 77: 78: 79:
80: public static function createUriWithPost($uri = 'index.php', $exclude = array())
81: {
82: $query = array();
83: self::map($query, $_POST, $exclude);
84: if (!empty($query)) {
85: $uri .= (strpos($uri, '?') === false ? '?' : '&').http_build_query($query);
86: }
87: return Uri::createFromUri($uri);
88: }
89:
90: 91: 92: 93: 94: 95:
96: public function getMethod()
97: {
98: if ($this->method === null) {
99: $this->method = isset($_SERVER['REQUEST_METHOD']) ? strtoupper($_SERVER['REQUEST_METHOD']) : 'GET';
100: if ($this->method === 'POST' && isset($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'])) {
101: $this->method = strtoupper($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE']);
102: }
103: }
104: return $this->method;
105: }
106:
107: 108: 109: 110: 111:
112: public function getRequestTarget()
113: {
114: if ($this->requestTarget === null) {
115: $this->requestTarget = $this->uri;
116: }
117: return $this->requestTarget;
118: }
119:
120: 121: 122: 123: 124:
125: public function getUri()
126: {
127: if ($this->uri === null) {
128: $this->uri = Uri::createFromGlobals();
129: }
130: return $this->uri;
131: }
132:
133: 134: 135: 136: 137: 138: 139:
140: public static function map(&$result, $array, $exclude = array())
141: {
142: foreach ($array as $key => $value) {
143: if (!in_array($key, $exclude)) {
144: if (is_array($value)) {
145: foreach ($value as $k => $v) {
146: $result[$key.'['.$k.']'] = $v;
147: }
148: } else {
149: $result[$key] = $value;
150: }
151: }
152: }
153: }
154:
155: 156: 157: 158: 159: 160: 161:
162: public function withMethod($method)
163: {
164: $clone = clone $this;
165: $clone->method = $method;
166: return $clone;
167: }
168:
169: 170: 171: 172: 173: 174: 175:
176: public function withRequestTarget($requestTarget)
177: {
178: $clone = clone $this;
179: $clone->requestTarget = $requestTarget;
180: return $clone;
181: }
182:
183: 184: 185: 186: 187: 188: 189: 190:
191: public function withUri(\Psr\Http\Message\UriInterface $uri, $preserveHost = false)
192: {
193: $clone = clone $this;
194: $clone->uri = $uri;
195: if (!$preserveHost) {
196: if ($uri->getHost() !== '') {
197: $clone->headers['Host'] = $uri->getHost();
198: }
199: } else {
200: if ($this->uri->getHost() !== '' && (!$this->hasHeader('Host') || $this->getHeader('Host') === null)) {
201: $clone->headers['Host'] = $uri->getHost();
202: }
203: }
204: return $clone;
205: }
206: }
207: