Total Complexity | 15 |
Total Lines | 86 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
4 | class Payload implements PayloadInterface |
||
5 | { |
||
6 | private $status; |
||
7 | |||
8 | 13 | public function getStatus(): string |
|
9 | { |
||
10 | 13 | return $this->status; |
|
11 | } |
||
12 | |||
13 | 13 | public function setStatus(string $status): PayloadInterface |
|
14 | { |
||
15 | 13 | $this->status = $status; |
|
16 | 13 | return $this; |
|
17 | } |
||
18 | |||
19 | private $data; |
||
20 | |||
21 | 10 | public function getData() |
|
22 | { |
||
23 | 10 | return $this->data; |
|
24 | } |
||
25 | |||
26 | 10 | public function setData($data): PayloadInterface |
|
27 | { |
||
28 | 10 | $this->data = $data; |
|
29 | 10 | return $this; |
|
30 | } |
||
31 | |||
32 | private $message; |
||
33 | |||
34 | 5 | public function getMessage(): ?string |
|
35 | { |
||
36 | 5 | return $this->message; |
|
37 | } |
||
38 | |||
39 | 5 | public function setMessage(string $message): PayloadInterface |
|
40 | { |
||
41 | 5 | $this->message = $message; |
|
42 | 5 | return $this; |
|
43 | } |
||
44 | |||
45 | private $code; |
||
46 | |||
47 | 3 | public function getCode(): ?int |
|
48 | { |
||
49 | 3 | return $this->code; |
|
50 | } |
||
51 | |||
52 | 3 | public function setCode(int $code): PayloadInterface |
|
53 | { |
||
54 | 3 | $this->code = $code; |
|
55 | 3 | return $this; |
|
56 | } |
||
57 | |||
58 | 4 | public function toArray(): array |
|
59 | { |
||
60 | 4 | if ($this->status === Status::SUCCESS || $this->status === Status::FAIL) { |
|
61 | return [ |
||
62 | 2 | 'status' => $this->status, |
|
63 | 2 | 'data' => $this->data |
|
64 | ]; |
||
65 | } |
||
66 | |||
67 | $payload = [ |
||
68 | 2 | 'status' => $this->status, |
|
69 | 2 | 'message' => $this->message |
|
70 | ]; |
||
71 | 2 | if ($this->code !== null) { |
|
72 | 1 | $payload['code'] = $this->code; |
|
73 | } |
||
74 | 2 | if ($this->data !== null) { |
|
75 | 1 | $payload['data'] = $this->data; |
|
76 | } |
||
77 | |||
78 | 2 | return $payload; |
|
79 | } |
||
80 | |||
81 | 4 | public function toString(?int $options = null): string |
|
82 | { |
||
83 | 4 | $options = $options ?? (JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); |
|
84 | 4 | return json_encode($this->toArray(), $options); |
|
85 | } |
||
86 | |||
87 | 4 | public function __toString() |
|
90 | } |
||
91 | } |
||
92 |