-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResponse.php
81 lines (76 loc) · 2.79 KB
/
Response.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
require_once('./HttpStatusCode.php');
/**
* Default return structure for HTTP requests.
*/
class Response
{
/**
* Default return structure.
*
* @var array
*/
private static $aResponse = array(
'success' => true,
'response' => array(
'statusCode' => 200,
'statusText' => '',
'message' => '',
'previous' => '',
'data' => array()
),
);
/**
* Returns success for the request.
*
* @param string $cMessage Message of any content.
* @param integer $nStatusCode Requisition status code.
* @param array|string|number|boolean|object $aData Return information, which can be any data type.
* @param string|null $cPrevious Information related to any errors that have occurred.
* @return void
*/
static function success($cMessage = '', $nStatusCode = 200, $aData = array(), $cPrevious = null)
{
static::setResponse($cMessage, $nStatusCode, $aData, $cPrevious);
echo json_encode(static::$aResponse);
die();
}
/**
* Returns an error for the request.
*
* @param string $cMessage Message of any content.
* @param integer $nStatusCode Requisition status code.
* @param array|string|number|boolean|object $aData return information, which can be any data type.
* @param string|null $cPrevious Information related to any errors that have occurred.
* @return void
*/
static function error($cMessage = '', $nStatusCode = 400, $aData = array(), $cPrevious = null)
{
static::setResponse($cMessage, $nStatusCode, $aData, $cPrevious);
echo json_encode(static::$aResponse);
die();
}
/**
* Configures the return structure, as well as the status code and message in the response header.
*
* @param string $cMessage Message of any content.
* @param integer $nStatusCode Requisition status code.
* @param array|string|number|boolean|object $aData return information, which can be any data type.
* @param string|null $cPrevious Information related to any errors that have occurred.
* @return void
*/
private static function setResponse($cMessage, $nStatusCode, $aData, $cPrevious)
{
$bSuccess = $nStatusCode < 400 ? true : false;
$statusText = HttpStatusCode::getMessage($nStatusCode, true);
header("HTTP/1.0 $nStatusCode $statusText");
static::$aResponse['success'] = $bSuccess;
static::$aResponse['response'] = array(
'statusCode' => $nStatusCode,
'statusText' => $statusText,
'message' => mb_convert_encoding($cMessage, 'utf-8', 'iso8859-1'),
'previous' => mb_convert_encoding($cPrevious, 'utf-8', 'iso8859-1'),
'data' => $aData
);
}
}