/** * Respond function to echo json with status and data * * @param boolean $success Success status of the call * @param integer|boolean $status The StatusCode of the call * @param array|boolean $data Extra data * * @return array */ protected static function respond($success, $status = false, $data = false) { Request::contentType('application/json'); $reply = ['success' => $success]; $reply['multiple'] = false; $reply['status'] = []; if ($status !== false) { if (is_array($status)) { $reply['multiple'] = true; $reply['status']['codes'] = []; $reply['status']['hex'] = []; $reply['status']['names'] = []; $reply['status']['messages'] = []; foreach ($status as $stat) { $object = StatusCodeHandler::find($stat); $reply['status']['codes'][] = $object->code(); $reply['status']['hex'][] = $object->hex(); $reply['status']['names'][] = $object->name(); $reply['status']['messages'][] = $object->readable(); } } else { $object = StatusCodeHandler::find($status); $reply['status']['code'] = $object->code(); $reply['status']['hex'] = $object->hex(); $reply['status']['name'] = $object->name(); $reply['status']['message'] = $object->readable(); } } if ($data !== false) { $reply['data'] = $data; } return $reply; }
/** * Registers error routes * * @param string|xTend\Objects\Route * @param mixed $route * @param string|boolean $alias * * @return xTend\Objects\Route */ public static function error($handle, $route = false, $alias = false, $override = false) { //here the handler should be an errorcode //you can either pass an actual handle as the handle //or directly pass a route object as the handle //ignoring the route and alias parameters completely $h; if (is_numeric($handle) && $route !== false) { $h = new Route($handle, $route, $alias); } elseif ($handle instanceof Route) { $h = $handle; } elseif (StatusCodeHandler::find($handle) instanceof StatusCode) { $h = new Route(StatusCodeHandler::find($handle)->code(), $route, $alias); } if ($override === true || !isset($routes[$h->handle()])) { //add route to the error list self::$_error[$h->handle()] = $h; //add to aliases if there is any if ($h->alias() !== false) { self::$_aliases[$h->alias()] = $h; } } elseif (isset($routes[$h->handle()])) { $h = $routes[$h->handle()]; } //return Route object return $h; }
/** * Throws an application error and sets an HTTP code * * @param integer $code * * @return boolean */ public static function throw($code) { header("HTTP/1.0 {$code}"); $error = StatusCodeHandler::find($code); if ($error instanceof StatusCode) { LogHandler::write($error, Request::path() . "\t" . $_SERVER["REMOTE_ADDR"]); return Router::throw($code); } return false; }