1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Demv\JSend; |
4
|
|
|
|
5
|
|
|
use Seld\JsonLint\JsonParser; |
6
|
|
|
use Seld\JsonLint\ParsingException; |
7
|
|
|
use function Dgame\Ensurance\ensure; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class JSend |
11
|
|
|
* @package Demv\JSend |
12
|
|
|
*/ |
13
|
|
|
final class JSend |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @param array $response |
17
|
|
|
* |
18
|
|
|
* @return JSendResponseInterface |
19
|
|
|
*/ |
20
|
|
|
public static function interpret(array $response): JSendResponseInterface |
21
|
|
|
{ |
22
|
|
|
ensure($response)->isArray()->hasKey(StatusInterface::KEY)->orThrow('Key "status" is required'); |
23
|
|
|
|
24
|
|
|
$status = Status::instance($response[StatusInterface::KEY]); |
25
|
|
|
if ($status->isSuccess() || $status->isFail()) { |
26
|
|
|
ensure($response)->isArray()->hasKey('data')->orThrow('Key "data" is required'); |
27
|
|
|
|
28
|
|
|
return new JSendResponse($status, $response['data']); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
return new JSendErrorResponse($status, $response); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param string $json |
36
|
|
|
* |
37
|
|
|
* @return JSendResponseInterface |
38
|
|
|
* @throws ParsingException |
39
|
|
|
*/ |
40
|
|
|
public static function decode(string $json): JSendResponseInterface |
41
|
|
|
{ |
42
|
|
|
$parser = new JsonParser(); |
43
|
|
|
$result = $parser->parse($json, JsonParser::PARSE_TO_ASSOC | JsonParser::DETECT_KEY_CONFLICTS); |
44
|
|
|
|
45
|
|
|
return self::interpret($result); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param array $response |
50
|
|
|
* |
51
|
|
|
* @return string |
52
|
|
|
*/ |
53
|
|
|
public static function encode(array $response): string |
54
|
|
|
{ |
55
|
|
|
ensure($response)->isNotEmpty()->orThrow('Empty response cannot be converted to valid JSend-JSON'); |
56
|
|
|
ensure($response)->isArray()->hasKey(StatusInterface::KEY)->orThrow('Key "status" is required'); |
57
|
|
|
$status = Status::instance($response[StatusInterface::KEY]); |
58
|
|
|
if ($status->isError()) { |
59
|
|
|
ensure($response)->isArray()->hasKey('message')->orThrow('Need a descriptive error-message'); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return json_encode($response); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param array $response |
67
|
|
|
* |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
|
|
public static function success(array $response): string |
71
|
|
|
{ |
72
|
|
|
$response[StatusInterface::KEY] = StatusInterface::STATUS_SUCCESS; |
73
|
|
|
|
74
|
|
|
return self::encode($response); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param array $response |
79
|
|
|
* |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
|
|
public static function fail(array $response): string |
83
|
|
|
{ |
84
|
|
|
$response[StatusInterface::KEY] = StatusInterface::STATUS_FAIL; |
85
|
|
|
|
86
|
|
|
return self::encode($response); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param array $response |
91
|
|
|
* |
92
|
|
|
* @return string |
93
|
|
|
*/ |
94
|
|
|
public static function error(array $response): string |
95
|
|
|
{ |
96
|
|
|
$response[StatusInterface::KEY] = StatusInterface::STATUS_ERROR; |
97
|
|
|
|
98
|
|
|
return self::encode($response); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param JSendResponseInterface $response |
103
|
|
|
* |
104
|
|
|
* @return int |
105
|
|
|
*/ |
106
|
|
|
public static function getDefaultHttpStatusCode(JSendResponseInterface $response): int |
107
|
|
|
{ |
108
|
|
|
if ($response->getStatus()->isError()) { |
109
|
|
|
return $response->getError()->getCode() ?? 500; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return 200; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|