Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
33.33% |
4 / 12 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
ResponseJson | |
33.33% |
4 / 12 |
|
50.00% |
1 / 2 |
3.19 | |
0.00% |
0 / 1 |
success | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
error | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace PeServer\App\Models; |
6 | |
7 | use stdClass; |
8 | use PeServer\Core\Throws\ArgumentNullException; |
9 | |
10 | /** |
11 | * API/AJAX の共通応答データ。 |
12 | */ |
13 | class ResponseJson |
14 | { |
15 | #region variable |
16 | |
17 | /** |
18 | * 正常系データ。 |
19 | * |
20 | * @var mixed |
21 | */ |
22 | public mixed $data; |
23 | /** |
24 | * 異常系データ。 |
25 | * |
26 | * @var array{message:string,code:string,info:mixed}|null |
27 | */ |
28 | public ?array $error = null; |
29 | |
30 | #endregion |
31 | |
32 | #region function |
33 | |
34 | /** |
35 | * 正常データ生成。 |
36 | * |
37 | * @param mixed $data |
38 | * @return ResponseJson |
39 | */ |
40 | public static function success(mixed $data): ResponseJson |
41 | { |
42 | ArgumentNullException::throwIfNull($data, '$data'); |
43 | |
44 | $result = new ResponseJson(); |
45 | $result->data = $data; |
46 | |
47 | return $result; |
48 | } |
49 | |
50 | /** |
51 | * 異常データ生成。 |
52 | * |
53 | * @param string $message |
54 | * @param string $code |
55 | * @param mixed $info |
56 | * @return ResponseJson |
57 | */ |
58 | public static function error(string $message, string $code, mixed $info): ResponseJson |
59 | { |
60 | $result = new ResponseJson(); |
61 | $result->data = new stdClass(); |
62 | $result->error = [ |
63 | 'message' => $message, |
64 | 'code' => $code, |
65 | 'info' => $info, |
66 | ]; |
67 | |
68 | return $result; |
69 | } |
70 | |
71 | #endregion |
72 | } |