1: <?php
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: class Mage_Api2_Model_Request extends Zend_Controller_Request_Http
35: {
36: 37: 38:
39: const REQUEST_CHARSET = 'utf-8';
40:
41: 42: 43:
44: const QUERY_PARAM_REQ_ATTRS = 'attrs';
45: const QUERY_PARAM_PAGE_NUM = 'page';
46: const QUERY_PARAM_PAGE_SIZE = 'limit';
47: const QUERY_PARAM_ORDER_FIELD = 'order';
48: const QUERY_PARAM_ORDER_DIR = 'dir';
49: const QUERY_PARAM_FILTER = 'filter';
50:
51:
52: 53: 54: 55: 56:
57: protected $_interpreter;
58:
59: 60: 61: 62: 63:
64: protected $_bodyParams;
65:
66: 67: 68: 69: 70: 71: 72: 73: 74:
75: public function __construct($uri = null)
76: {
77: parent::__construct($uri ? $uri : null);
78: }
79:
80: 81: 82: 83: 84:
85: protected function _getInterpreter()
86: {
87: if (null === $this->_interpreter) {
88: $this->_interpreter = Mage_Api2_Model_Request_Interpreter::factory($this->getContentType());
89: }
90: return $this->_interpreter;
91: }
92:
93: 94: 95: 96: 97:
98: public function getAcceptTypes()
99: {
100: $qualityToTypes = array();
101: $orderedTypes = array();
102:
103: foreach (preg_split('/,\s*/', $this->getHeader('Accept')) as $definition) {
104: $typeWithQ = explode(';', $definition);
105: $mimeType = trim(array_shift($typeWithQ));
106:
107:
108: if (!preg_match('~^([0-9a-z*+\-]+)(?:/([0-9a-z*+\-\.]+))?$~i', $mimeType)) {
109: continue;
110: }
111: $quality = '1.0';
112:
113: if ($typeWithQ) {
114: $qAndValue = explode('=', $typeWithQ[0]);
115:
116: if (2 == count($qAndValue)) {
117: $quality = $qAndValue[1];
118: }
119: }
120: $qualityToTypes[$quality][$mimeType] = true;
121: }
122: krsort($qualityToTypes);
123:
124: foreach ($qualityToTypes as $typeList) {
125: $orderedTypes += $typeList;
126: }
127: return array_keys($orderedTypes);
128: }
129:
130: 131: 132: 133: 134:
135: public function getApiType()
136: {
137:
138: return isset($this->_params['api_type']) ? $this->_params['api_type'] : null;
139: }
140:
141: 142: 143: 144: 145:
146: public function getBodyParams()
147: {
148: if (null == $this->_bodyParams) {
149: $this->_bodyParams = $this->_getInterpreter()->interpret((string)$this->getRawBody());
150: }
151: return $this->_bodyParams;
152: }
153:
154: 155: 156: 157: 158: 159:
160: public function getContentType()
161: {
162: $headerValue = $this->getHeader('Content-Type');
163:
164: if (!$headerValue) {
165: throw new Mage_Api2_Exception('Content-Type header is empty', Mage_Api2_Model_Server::HTTP_BAD_REQUEST);
166: }
167: if (!preg_match('~^([a-z\d/\-+.]+)(?:; *charset=(.+))?$~Ui', $headerValue, $matches)) {
168: throw new Mage_Api2_Exception('Invalid Content-Type header', Mage_Api2_Model_Server::HTTP_BAD_REQUEST);
169: }
170:
171: if (isset($matches[2]) && self::REQUEST_CHARSET != strtolower($matches[2])) {
172: throw new Mage_Api2_Exception(
173: 'UTF-8 is the only supported charset', Mage_Api2_Model_Server::HTTP_BAD_REQUEST
174: );
175: }
176: return $matches[1];
177: }
178:
179: 180: 181: 182: 183:
184: public function getFilter()
185: {
186: return $this->getQuery(self::QUERY_PARAM_FILTER);
187: }
188:
189: 190: 191: 192: 193:
194: public function getModel()
195: {
196:
197: return isset($this->_params['model']) ? $this->_params['model'] : null;
198: }
199:
200: 201: 202: 203: 204: 205:
206: public function getOperation()
207: {
208: if (!$this->isGet() && !$this->isPost() && !$this->isPut() && !$this->isDelete()) {
209: throw new Mage_Api2_Exception('Invalid request method', Mage_Api2_Model_Server::HTTP_BAD_REQUEST);
210: }
211:
212: $operationByMethod = array(
213: 'GET' => Mage_Api2_Model_Resource::OPERATION_RETRIEVE,
214: 'POST' => Mage_Api2_Model_Resource::OPERATION_CREATE,
215: 'PUT' => Mage_Api2_Model_Resource::OPERATION_UPDATE,
216: 'DELETE' => Mage_Api2_Model_Resource::OPERATION_DELETE
217: );
218:
219: return $operationByMethod[$this->getMethod()];
220: }
221:
222: 223: 224: 225: 226:
227: public function getOrderDirection()
228: {
229: return $this->getQuery(self::QUERY_PARAM_ORDER_DIR);
230: }
231:
232: 233: 234: 235: 236:
237: public function getOrderField()
238: {
239: return $this->getQuery(self::QUERY_PARAM_ORDER_FIELD);
240: }
241:
242: 243: 244: 245: 246:
247: public function getPageNumber()
248: {
249: return $this->getQuery(self::QUERY_PARAM_PAGE_NUM);
250: }
251:
252: 253: 254: 255: 256:
257: public function getPageSize()
258: {
259: return $this->getQuery(self::QUERY_PARAM_PAGE_SIZE);
260: }
261:
262: 263: 264: 265: 266:
267: public function getRequestedAttributes()
268: {
269: $include = $this->getQuery(self::QUERY_PARAM_REQ_ATTRS, array());
270:
271:
272: if (!is_array($include)) {
273: $include = explode(',', $include);
274: }
275: return array_map('trim', $include);
276: }
277:
278: 279: 280: 281: 282:
283: public function getResourceType()
284: {
285:
286: return isset($this->_params['type']) ? $this->_params['type'] : null;
287: }
288:
289: 290: 291: 292: 293:
294: public function getVersion()
295: {
296: return $this->getHeader('Version');
297: }
298:
299: 300: 301: 302: 303:
304: public function getActionType()
305: {
306:
307: return isset($this->_params['action_type']) ? $this->_params['action_type'] : null;
308: }
309:
310: 311: 312: 313: 314: 315:
316: public function isAssocArrayInRequestBody()
317: {
318: $params = $this->getBodyParams();
319: if (count($params)) {
320: $keys = array_keys($params);
321: return !is_numeric($keys[0]);
322: }
323: return false;
324: }
325: }
326: