Issues (33)

src/Response.php (4 issues)

1
<?php
2
declare(strict_types=1);
3
4
namespace Utilities\Router;
5
6
use Utilities\Common\Common;
7
use Utilities\Common\Time;
8
use Utilities\Router\Utils\StatusCode;
9
10
/**
11
 * Response class
12
 *
13
 * @link    https://github.com/utilities-php/router
14
 * @author  Shahrad Elahi (https://github.com/shahradelahi)
15
 * @license https://github.com/utilities-php/router/blob/master/LICENSE (MIT License)
16
 */
17
class Response
18
{
19
20
    /**
21
     * @var int
22
     */
23
    private static int $status_code = -1;
24
25
    /**
26
     * Require given keys in array and send error if not found
27
     *
28
     * @param array $haystack
29
     * @param array $needle
30
     * @return array
31
     */
32
    public static function RequireParams(array $haystack, array $needle): array
33
    {
34
        $result = [];
35
        foreach ($needle as $key) {
36
            if ($haystack[$key] == null) Response::send(StatusCode::BAD_REQUEST, [
37
                'description' => sprintf("Bad Request: the '%s' parameter is required", $key)
38
            ]);
39
            else $result[$key] = $haystack[$key];
40
        }
41
        return $result;
42
    }
43
44
    /**
45
     * Send response.
46
     *
47
     * @param int $statusCode
48
     * @param string|array $body
49
     * @return void
50
     */
51
    public static function send(int $statusCode, string|array $body = []): void
52
    {
53
        static::$status_code = $statusCode;
0 ignored issues
show
Since $status_code is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $status_code to at least protected.
Loading history...
54
        $status = ($statusCode >= 200 && $statusCode < 300);
55
56
        http_response_code($statusCode);
57
        if (!headers_sent()) {
58
            header('Content-type: application/json');
59
        }
60
61
        if (is_array($body)) {
0 ignored issues
show
The condition is_array($body) is always true.
Loading history...
62
            $message['ok'] = $status;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$message was never initialized. Although not strictly required by PHP, it is generally a good practice to add $message = array(); before regardless.
Loading history...
63
            if (!$status) $message['error_code'] = $statusCode;
64
            $message['elapsed_time'] = Time::elapsedTime() . "ms";
65
66
            foreach ($body as $key => $value) {
67
                $message[$key] = $value;
68
            }
69
70
            $body = Common::prettyJson($message);
71
        }
72
73
        echo $body;
74
    }
75
76
    /**
77
     * Check are values are empty (API Response)
78
     *
79
     * @param array $params
80
     * @return void
81
     */
82
    public static function EmptyParams(array $params = []): void
83
    {
84
        foreach ($params as $key => $value) {
85
            if ($value == null) Response::send(StatusCode::BAD_REQUEST, [
86
                'description' => sprintf("Bad Request: the '%s' parameter is empty", $key)
87
            ]);
88
        }
89
    }
90
91
    /**
92
     * This method returns the last status code. On empty, it returns -1.
93
     *
94
     * @return int
95
     */
96
    public static function getStatusCode(): int
97
    {
98
        return static::$status_code;
0 ignored issues
show
Since $status_code is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $status_code to at least protected.
Loading history...
99
    }
100
101
}