/** * @param null $key * @param null $default * @param null $removeKeyPrefix * * @return array|mixed */ public function data($key = NULL, $default = NULL, $removeKeyPrefix = NULL) { $data = parent::data(); if (NULL !== $key && !Kohana_Arr::is_array($key)) { return Kohana_Arr::get($data, $key, $default); } if (NULL === $key) { $key = array_keys($data); } $result = []; foreach ($key as $_key) { $newKey = NULL !== $removeKeyPrefix && strpos($_key, $removeKeyPrefix) === 0 ? substr($_key, strlen($removeKeyPrefix)) : $_key; $result[$newKey] = Kohana_Arr::get($data, $_key, $default); } return $result; }
/** * @param null $data * @param int $httpCode * @param null|array $headers * * @throws HTTP_Exception_Redirect */ static function json($data = NULL, $httpCode = 200, $headers = NULL) { if ($data instanceof HTTP_Exception_Redirect) { throw $data; } $response = Kohana_Response::factory(); try { $response->headers(['cache-control' => 'no-cache, no-store, max-age=0, must-revalidate', 'content-type' => 'application/json; charset=utf-8']); if (Kohana_Arr::is_array($headers)) { $response->headers($headers); } $response->status($httpCode); } catch (Exception $e) { $response->status($httpCode = 500); $data = $e; } if ($data instanceof Exception) { if ($data instanceof HTTP_Exception) { $response->status($httpCode = $data->getCode()); } elseif ($httpCode < 400) { $response->status($httpCode = 500); } $data = Helpers_Arr::exception($data); } if (NULL === $data && $httpCode == 200) { $response->status(204); } elseif (NULL !== $data) { try { $response->body(json_encode($data, JSON_UNESCAPED_UNICODE)); } catch (Exception $e) { $response->body(json_encode(Helpers_Arr::exception($e), JSON_UNESCAPED_UNICODE), 500); } } $response->send_headers(TRUE); exit($response); }
/** * @param null|string|array $key * @param null $default * @param null $delimiter * * @return array|mixed */ protected function requestData($key = NULL, $default = NULL, $delimiter = NULL) { if (NULL === $this->_requestData) { $this->_requestData = Helpers_Arr::merge($this->request->query(), $this->request->post(), $this->_parse_request_body()); } if (Kohana_Arr::is_array($key)) { $result = []; foreach ($key as $idx => $value) { if (!Valid::digit($idx)) { $_key = $idx; $_default = $value; } else { $_key = $value; $_default = $default; } $result[$_key] = Kohana_Arr::path($this->requestData(), $_key, $_default, $delimiter); } } else { $result = NULL === $key ? $this->_requestData : Kohana_Arr::path($this->requestData(), $key, $default, $delimiter); } return $result; }