|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Demv\JSend; |
|
4
|
|
|
|
|
5
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
6
|
|
|
use Seld\JsonLint\JsonParser; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class ResponseFactory |
|
10
|
|
|
* @package Demv\JSend |
|
11
|
|
|
*/ |
|
12
|
|
|
final class ResponseFactory |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var ResponseFactory |
|
16
|
|
|
*/ |
|
17
|
|
|
private static $instance; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* ResponseFactory constructor. |
|
21
|
|
|
*/ |
|
22
|
|
|
private function __construct() |
|
23
|
|
|
{ |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @return ResponseFactory |
|
28
|
|
|
*/ |
|
29
|
|
|
public static function instance(): self |
|
30
|
|
|
{ |
|
31
|
|
|
if (self::$instance === null) { |
|
32
|
|
|
self::$instance = new self(); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
return self::$instance; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param ResponseInterface $response |
|
40
|
|
|
* |
|
41
|
|
|
* @return JSendResponseInterface |
|
42
|
|
|
* @throws \Seld\JsonLint\ParsingException |
|
43
|
|
|
*/ |
|
44
|
|
|
public function convert(ResponseInterface $response): JSendResponseInterface |
|
45
|
|
|
{ |
|
46
|
|
|
$code = $response->getStatusCode(); |
|
47
|
|
|
$content = $response->getBody()->getContents(); |
|
48
|
|
|
|
|
49
|
|
|
$parser = new JsonParser(); |
|
50
|
|
|
$result = $parser->parse($content, JsonParser::PARSE_TO_ASSOC | JsonParser::DETECT_KEY_CONFLICTS); |
|
51
|
|
|
|
|
52
|
|
|
if ($code >= 200 && $code < 300) { |
|
53
|
|
|
return $this->success($result['data'] ?? null); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
if ($code >= 400) { |
|
57
|
|
|
return $this->error($result, $code); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
return $this->fail($result['data'] ?? null); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param array|null $data |
|
65
|
|
|
* |
|
66
|
|
|
* @return JSendResponseInterface |
|
67
|
|
|
*/ |
|
68
|
|
|
public function success(array $data = null): JSendResponseInterface |
|
69
|
|
|
{ |
|
70
|
|
|
return new JSendResponse(Status::success(), $data); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param array|null $data |
|
75
|
|
|
* |
|
76
|
|
|
* @return JSendResponseInterface |
|
77
|
|
|
*/ |
|
78
|
|
|
public function fail(array $data = null): JSendResponseInterface |
|
79
|
|
|
{ |
|
80
|
|
|
return new JSendResponse(Status::fail(), $data); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param array $response |
|
85
|
|
|
* @param int|null $code |
|
86
|
|
|
* |
|
87
|
|
|
* @return JSendResponseInterface |
|
88
|
|
|
*/ |
|
89
|
|
|
public function error(array $response, int $code = null): JSendResponseInterface |
|
90
|
|
|
{ |
|
91
|
|
|
if ($code !== null || !array_key_exists('code', $response)) { |
|
92
|
|
|
$response['code'] = $code; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
return new JSendErrorResponse(Status::error(), $response); |
|
96
|
|
|
} |
|
97
|
|
|
} |