1: <?php
2: namespace PHPixie\HTTP\Messages\Message;
3:
4: use Psr\Http\Message\RequestInterface;
5: use Psr\Http\Message\UriInterface;
6:
7: 8: 9:
10: abstract class Request extends \PHPixie\HTTP\Messages\Message
11: implements RequestInterface
12: {
13: 14: 15:
16: protected static $validMethods = array(
17: 'CONNECT',
18: 'DELETE',
19: 'GET',
20: 'HEAD',
21: 'OPTIONS',
22: 'PATCH',
23: 'POST',
24: 'PUT',
25: 'TRACE',
26: );
27:
28: 29: 30:
31: protected $requestTarget;
32:
33: 34: 35:
36: protected $method;
37:
38: 39: 40:
41: protected $uri;
42:
43: 44: 45:
46: public function getRequestTarget()
47: {
48: if ($this->requestTarget !== null) {
49: return $this->requestTarget;
50: }
51:
52: $this->requireUri();
53:
54: if ($this->uri === null) {
55: return '/';
56: }
57:
58: $target = $this->uri->getPath();
59:
60: $query = $this->uri->getQuery();
61:
62: if ($query !== '') {
63: $target .= '?'.$query;
64: }
65:
66: $this->requestTarget = $target;
67:
68: return $this->requestTarget;
69: }
70:
71: 72: 73:
74: public function withRequestTarget($requestTarget)
75: {
76: if (preg_match('#\s#', $requestTarget)) {
77: throw new \InvalidArgumentException(
78: 'Invalid request target provided; cannot contain whitespace'
79: );
80: }
81:
82: $new = clone $this;
83: $new->requestTarget = $requestTarget;
84: return $new;
85: }
86:
87: 88: 89:
90: public function getMethod()
91: {
92: $this->requireMethod();
93: return $this->method;
94: }
95:
96: 97: 98:
99: public function withMethod($method)
100: {
101: $this->validateMethod($method);
102:
103: $new = clone $this;
104: $new->method = $method;
105: return $new;
106: }
107:
108: 109: 110:
111: public function getUri()
112: {
113: $this->requireUri();
114: return $this->uri;
115: }
116:
117: 118: 119:
120: public function withUri(UriInterface $uri, $preserveHost = false)
121: {
122: $new = clone $this;
123: $new->uri = $uri;
124: if(!$preserveHost && ($host = $uri->getHost()) !== '') {
125: $new->modifyHeader('Host', $host, false, false);
126: }
127:
128: return $new;
129: }
130:
131: 132: 133: 134:
135: protected function validateMethod($method)
136: {
137: $method = strtoupper($method);
138:
139: if (!in_array($method, static::$validMethods, true)) {
140: throw new \InvalidArgumentException("Unsupported HTTP method '$method' provided");
141: }
142: }
143:
144: 145: 146:
147: protected function requireMethod()
148: {
149:
150: }
151:
152: 153: 154:
155: protected function requireUri()
156: {
157:
158: }
159: }