Response.class.php 2.04 KB
Newer Older
Zohten's avatar
Zohten committed
1
<?php
Zohten's avatar
Zohten committed
2 3 4 5
class Response
{
    protected $code;
    protected $body;
Zohten's avatar
Zohten committed
6

Zohten's avatar
Zohten committed
7 8 9 10 11
    public function __construct($code = 404, $msg = "")
    {
        $this->code = $code;
        $this->body = $msg;
    }
Zohten's avatar
Zohten committed
12

Zohten's avatar
Zohten committed
13 14 15 16
    public static function errorResponse($message = "")
    {
        return new Response(400, $message);
    }
Zohten's avatar
Zohten committed
17

Zohten's avatar
Zohten committed
18 19 20 21
    public static function serverErrorResponse($message = "")
    {
        return new Response(500, $message);
    }
Zohten's avatar
Zohten committed
22

Zohten's avatar
Zohten committed
23 24 25 26
    public static function okResponse($message = "")
    {
        return new Response(200, $message);
    }
Zohten's avatar
Zohten committed
27

Zohten's avatar
Zohten committed
28 29 30 31 32
    public static function createdResponse($message = "")
    {
        return new Response(201, $message);
    }

Zohten's avatar
Zohten committed
33 34 35 36
    public static function notFoundResponse($message = "")
    {
        return new Response(404, $message);
    }
Zohten's avatar
Zohten committed
37

Zohten's avatar
Zohten committed
38 39 40 41
    public static function errorInParametersResponse($message = "")
    {
        return new Response(400, $message);
    }
Zohten's avatar
Zohten committed
42
   
Zohten's avatar
Zohten committed
43 44 45 46
    public static function unauthorizedResponse($message = "")
    {
        return new Response(401, $message);
    }
Zohten's avatar
Zohten committed
47

Zohten's avatar
Zohten committed
48 49 50 51
    public static function interceptEchos()
    {
        ob_start();
    }
Zohten's avatar
Zohten committed
52

Zohten's avatar
Zohten committed
53 54 55 56
    public static function getEchos()
    {
        return ob_get_clean();
    }
Zohten's avatar
Zohten committed
57

Zohten's avatar
Zohten committed
58 59 60 61
    public function send()
    {
        // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin
        header("Access-Control-Allow-Origin: *");
Zohten's avatar
Zohten committed
62

Zohten's avatar
Zohten committed
63
        header("Content-Type: application/json; charset=UTF-8");
Zohten's avatar
Zohten committed
64

Zohten's avatar
Zohten committed
65
        // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Methods
Zohten's avatar
Zohten committed
66
        header("Access-Control-Allow-Methods: GET,POST,PUT,DELETE");
Zohten's avatar
Zohten committed
67

Zohten's avatar
Zohten committed
68
        header("Access-Control-Max-Age: 3600"); // Maximum number of seconds the results can be cached.
Zohten's avatar
Zohten committed
69

Zohten's avatar
Zohten committed
70 71
        // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Headers
        header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
Zohten's avatar
Zohten committed
72

Zohten's avatar
Zohten committed
73 74 75 76
        http_response_code($this->code);
        echo $this->body;
        exit; // do we keep that?
    }
77 78 79 80

    public function getCode(){
        return $this->code;
    }
Zohten's avatar
Zohten committed
81
}