Completed
Push — master ( 0e5f68...33b81c )
by Richard
01:13
created

Response::getResponseData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace RichToms\LaravelJsend\Responses;
4
5
use JsonSerializable;
6
7
class Response implements JsonSerializable
8
{
9
    /**
10
     * @var int
11
     */
12
    protected $statusCode = 200;
13
14
    /**
15
     * @var array|null
16
     */
17
    protected $data = false;
18
19
    /**
20
     * @var string
21
     */
22
    protected $statusText = 'success';
23
24
    /**
25
     * @param  int  $statusCode
26
     * @return $this
27
     */
28
    public function withStatusCode($statusCode)
29
    {
30
        $this->statusCode = $statusCode;
31
32
        return $this;
33
    }
34
35
    /**
36
     * @param  mixed  $data
37
     * @return $this
38
     */
39
    public function withData($data)
40
    {
41
        $this->data = $this->toJSendData($data);
42
43
        return $this;
44
    }
45
46
    /**
47
     * @return bool
48
     */
49
    public function hasData()
50
    {
51
        return $this->data !== false;
52
    }
53
54
    /**
55
     * @return int
56
     */
57
    public function getStatusCode()
58
    {
59
        return $this->statusCode;
60
    }
61
62
    /**
63
     * @return string
64
     */
65
    public function getStatusText()
66
    {
67
        return $this->statusText;
68
    }
69
70
    /**
71
     * @return array
72
     */
73
    protected function getResponseData()
74
    {
75
        return $this->data;
76
    }
77
78
    /**
79
     * @return array
80
     */
81
    public function toArray()
82
    {
83
        $response = [
84
            'status' => $this->getStatusText(),
85
        ];
86
87
        if ($this->hasData()) {
88
            $response['data'] = $this->getResponseData();
89
        }
90
91
        return $response;
92
    }
93
94
    /**
95
     * @return array
96
     */
97
    public function jsonSerialize()
98
    {
99
        return $this->toArray();
100
    }
101
102
    /**
103
     * @param  mixed  $data
104
     * @return mixed
105
     */
106
    public function toJSendData($data)
107
    {
108
        return $data;
109
    }
110
}
111