Skip to content

Commit

Permalink
Add baseline for Api object
Browse files Browse the repository at this point in the history
  • Loading branch information
g105b committed Dec 17, 2014
1 parent 8a495b5 commit e96126d
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 27 deletions.
6 changes: 5 additions & 1 deletion default.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace = App
production = false
encoding = "UTF-8"
session_cs = false

[request]
api_prefix = "api"
Expand All @@ -14,4 +15,7 @@ index_force = false
[response]
client_minified = false
dev_client_asset_timeout = 600
template_element_attribute = "data-template"
template_element_attribute = "data-template"

[api]
default_version = "v1"
6 changes: 3 additions & 3 deletions src/Core/Path.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

class Path {

const DATABASE = "database";
const DATA = "data";
const PAGE = "page";
const PUBLICFILES = "publicfiles";
const ROOT = "root";
Expand All @@ -36,8 +36,8 @@ class Path {
*/
public static function get($name) {
switch($name) {
case self::DATABASE:
$p = self::get(self::SRC) . "/Database";
case self::DATA:
$p = self::get(self::ROOT) . "/data";
break;

case self::PAGE:
Expand Down
9 changes: 9 additions & 0 deletions src/Response/Headers.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ public static function getAll() {
* @return string The raw HTTP representation of all headers sent
*/
public static function send() {
ResponseCode::send();

$rawAll = "";
foreach (self::$headerArray as $field => $value) {
$raw = self::getRaw($field, $value);
Expand Down Expand Up @@ -104,4 +106,11 @@ public static function getRaw($field, $value) {
return "$field: $value";
}

public static function redirect($uri,
$code = ResponseCode::REDIRECT_TEMPORARY) {
self::add("Location: $uri");
self::code($code);
self::send();
}

}#
53 changes: 30 additions & 23 deletions src/Response/ResponseCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@

class ResponseCode {

private $code;

const TYPE_INFORMATIONAL = 1;
const TYPE_SUCCESS = 2;
const TYPE_REDIRECTION = 3;
const TYPE_CLIENT_ERROR = 4;
const TYPE_SERVER_ERROR = 5;

const REDIRECT_PERMANENT = 301;
const REDIRECT_FOUND = 302;
const REDIRECT_TEMPORARY = 302;
const REDIRECT_SEE_OTHER = 303;

private $typeNameArray = [
1 => "Informational",
2 => "Success",
Expand All @@ -24,7 +28,7 @@ class ResponseCode {
5 => "Server Error",
];

private $descriptionArray = [
private static $descriptionArray = [
// TYPE_INFORMATIONAL
100 => "Continue",
101 => "Switching Protocols",
Expand Down Expand Up @@ -89,45 +93,41 @@ class ResponseCode {
510 => "Not Extended",
511 => "Network Authentication Required",
];
public function __construct() {
$this->code = 200;
}

/**
* @param int $code The new HTTP response code
* Gets the status code associated with the given name.
*
* @return int The new HTTP response code
* @param string $name Text description of status code
*
* @return int Status code for provided name
*/
public function setCode($code) {
if(!is_int($code)) {
throw new \Gt\Core\Exception\InvalidArgumentTypeException(
"Response Code muse be integer value");
}
public static function getByName($name) {
$normalisedName = strtolower($name);
$normalisedName = str_replace(" ", "", $normalisedName);

$this->code = $code;
return $this->code;
}
foreach (self::$descriptionArray as $code => $description) {
$normalisedDescription = strtolower($description);
$normalisedDescription = str_replace(" ", "", $normalisedDescription);

/**
* @return int The current HTTP response code
*/
public function getCode() {
return $this->code;
if($normalisedName === $normalisedDescription) {
return (int)$code;
}
}
}

/**
* @return string The text description for the current HTTP response code
*/
public function getDescription() {
return $this->descriptionArray[$this->code];
return self::$descriptionArray[Headers::$code];
}

/**
* @return array The list of all possible codes and their descriptions in an
* indexed array
*/
public function getAllCodesAndDescriptions() {
return $this->descriptionArray;
return self::$descriptionArray;
}

/**
Expand All @@ -147,4 +147,11 @@ public function getType() {
return (int)substr($this->code, 0, $this->code < 0 ? 2 : 1);
}

/**
* Sends the current HTTP Status Code to the browser.
*/
public function send() {
http_response_code(Headers::code());
}

}#

0 comments on commit e96126d

Please sign in to comment.