1: <?php
2:
3: namespace PHPixie\HTTP\Messages\Message\Request\ServerRequest;
4:
5: use PHPixie\HTTP\Messages;
6:
7: 8: 9:
10: class SAPI extends \PHPixie\HTTP\Messages\Message\Request\ServerRequest
11: {
12: 13: 14:
15: protected $messages;
16:
17: 18: 19:
20: protected $fileParams;
21:
22: 23: 24: 25: 26: 27: 28: 29: 30: 31:
32: public function __construct($messages, $server, $get, $post, $cookies, $files, $attributes = array())
33: {
34: $this->messages = $messages;
35:
36: $this->serverParams = $server;
37: $this->queryParams = $get;
38: $this->parsedBody = $post;
39: $this->cookieParams = $cookies;
40: $this->fileParams = $files;
41: $this->attributes = $attributes;
42:
43: $this->method = $server['REQUEST_METHOD'];
44: }
45:
46: 47: 48:
49: protected function requireProtocolVersion()
50: {
51: if($this->protocolVersion === null) {
52: $this->protocolVersion = substr($this->serverParams['SERVER_PROTOCOL'], 5);
53: }
54: }
55:
56: 57: 58:
59: protected function requireHeaders()
60: {
61: if($this->processedHeaders) {
62: return;
63: }
64:
65: $headers = array();
66:
67: foreach($this->serverParams as $key => $value) {
68: if($key === 'HTTP_COOKIE') {
69: continue;
70: }
71:
72: if (substr($key, 0, 5) === 'HTTP_') {
73: $headerName = substr($key, 5);
74: $headerName = $this->normalizeHeaderName($headerName);
75: $headers[$headerName] = array($value);
76:
77: }elseif(substr($key, 0, 8) === 'CONTENT_') {
78: $headerName = substr($key, 8);
79: $headerName = 'Content-' . $this->normalizeHeaderName($headerName);
80: $headers[$headerName] = array($value);
81: }
82: }
83:
84: $this->headers = $headers;
85: $this->populateHeaderNames();
86:
87: $this->processedHeaders = true;
88: }
89:
90: 91: 92:
93: protected function requireUploadedFiles()
94: {
95: if($this->uploadedFiles !== null) {
96: return;
97: }
98:
99: $uploadedFiles = array();
100:
101: foreach($this->fileParams as $name => $data) {
102: if(is_array($data['error'])) {
103: $count = count($data['error']);
104: $keys = array_keys($data);
105: $values = array();
106:
107: for($i=0; $i<$count; $i++) {
108: $normalized = array();
109: foreach($keys as $key) {
110: $normalized[$key] = $data[$key][$i];
111: }
112: $values[] = $this->messages->sapiUploadedFile($normalized);
113: }
114:
115: $uploadedFiles[$name] = $values;
116: }else{
117: $uploadedFiles[$name] = $this->messages->sapiUploadedFile($data);
118: }
119: }
120:
121: $this->uploadedFiles = $uploadedFiles;
122: }
123:
124: 125: 126: 127:
128: protected function normalizeHeaderName($header)
129: {
130: $header = strtolower($header);
131: $header = str_replace('_', ' ', $header);
132: $header = ucwords($header);
133: $header = str_replace(' ', '-', $header);
134: return $header;
135: }
136:
137: 138: 139:
140: protected function requireBody()
141: {
142: if($this->body === null) {
143: $this->body = $this->messages->stream('php://input');
144: }
145: }
146:
147: 148: 149:
150: protected function requireUri()
151: {
152: if($this->uri === null) {
153: $this->uri = $this->messages->sapiUri($this->serverParams);
154: }
155: }
156: }