1: <?php
2:
3: namespace PHPixie\HTTP;
4:
5: use PHPixie\HTTP\Data\Headers;
6: use Psr\Http\Message\ServerRequestInterface;
7: use Psr\Http\Message\UriInterface;
8:
9: /**
10: * HTTP request representation
11: */
12: class Request
13: {
14: /**
15: * @var ServerRequestInterface
16: */
17: protected $serverRequest;
18:
19: /**
20: * @var array
21: */
22: protected $dataMap = array();
23:
24: /**
25: * @var array
26: */
27: protected $dataMethods = array(
28: 'query' => 'getQueryParams',
29: 'data' => 'getParsedBody',
30: 'attributes' => 'getAttributes',
31: 'uploads' => 'getUploadedFiles',
32: );
33:
34: /**
35: * @var \PHPixie\Slice\Data
36: */
37: protected $server;
38:
39: /**
40: * @var Headers
41: */
42: protected $headers;
43:
44: /**
45: * Constructor
46: * @param Builder $builder
47: * @param ServerRequestInterface $serverRequest
48: */
49: public function __construct($builder, $serverRequest)
50: {
51: $this->builder = $builder;
52: $this->serverRequest = $serverRequest;
53: }
54:
55: /**
56: * Query parameters (e.g. $_GET)
57: * @return \PHPixie\Slice\Type\ArrayData
58: */
59: public function query()
60: {
61: return $this->getData('query');
62: }
63:
64: /**
65: * Body parameters (e.g. $_POST)
66: * @return \PHPixie\Slice\Type\ArrayData
67: */
68: public function data()
69: {
70: return $this->getData('data');
71: }
72:
73: /**
74: * Additional attributes (e.g. routing data)
75: * @return \PHPixie\Slice\Type\ArrayData
76: */
77: public function attributes()
78: {
79: return $this->getData('attributes');
80: }
81:
82: /**
83: * Uploads data (e.g. $_FILES)
84: * @return \PHPixie\Slice\Type\ArrayData
85: */
86: public function uploads()
87: {
88: return $this->getData('uploads');
89: }
90:
91: /**
92: * Server data (e.g. $_SERVER)
93: * @return \PHPixie\HTTP\Data\Server
94: */
95: public function server()
96: {
97: if($this->server === null) {
98: $data = $this->serverRequest->getServerParams();
99: $this->server = $this->builder->serverData($data);
100: }
101:
102: return $this->server;
103: }
104:
105: /**
106: * Headers
107: * @return \PHPixie\HTTP\Data\Headers
108: */
109: public function headers()
110: {
111: if($this->headers === null) {
112: $data = $this->serverRequest->getHeaders();
113: $this->headers = $this->builder->headers($data);
114: }
115:
116: return $this->headers;
117: }
118:
119: /**
120: * Get original PSR-7 ServerRequest
121: * @return ServerRequestInterface
122: */
123: public function serverRequest()
124: {
125: return $this->serverRequest;
126: }
127:
128: /**
129: * Get HTTP method
130: * @return string
131: */
132: public function method()
133: {
134: return $this->serverRequest->getMethod();
135: }
136:
137: /**
138: * PSR-7 URI of the request
139: * @return UriInterface
140: */
141: public function uri()
142: {
143: return $this->serverRequest->getUri();
144: }
145:
146: /**
147: * @param string $type
148: * @return \PHPixie\Slice\Type\ArrayData
149: */
150: protected function getData($type)
151: {
152: if(!array_key_exists($type, $this->dataMap)) {
153: $method = $this->dataMethods[$type];
154: $data = $this->serverRequest->$method();
155: $this->dataMap[$type] = $this->builder->data($data);
156: }
157:
158: return $this->dataMap[$type];
159: }
160: }