Completed
Push — master ( 1cffcd...4414b3 )
by Elf
02:56
created

ApiResponse::mergeData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace ElfSundae\Laravel\Api\Http;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Http\JsonResponse;
7
8
class ApiResponse extends JsonResponse
9
{
10
    /**
11
     * The api result code.
12
     *
13
     * @var int
14
     */
15
    protected $code;
16
17
    /**
18
     * Create an ApiResponse instance.
19
     *
20
     * @param  mixed  $data
21
     * @param  int|null  $code
22
     * @param  array  $headers
23
     * @param  int  $options
24
     */
25
    public function __construct($data = null, $code = null, $headers = [], $options = 0)
26
    {
27
        $this->code = is_null($code) ? static::successCode() : (int) $code;
28
29
        parent::__construct($data, 200, $headers, $options);
30
    }
31
32
    /**
33
     * Get the code key.
34
     *
35
     * @return string
36
     */
37
    public static function codeKey()
38
    {
39
        static $codeKey = null;
40
41
        if (is_null($codeKey)) {
42
            $codeKey = config('api.response.key.code', 'code');
43
        }
44
45
        return $codeKey;
46
    }
47
48
    /**
49
     * Get the message key.
50
     *
51
     * @return string
52
     */
53
    public static function messageKey()
54
    {
55
        static $messageKey = null;
56
57
        if (is_null($messageKey)) {
58
            $messageKey = config('api.response.key.message', 'msg');
59
        }
60
61
        return $messageKey;
62
    }
63
64
    /**
65
     * Get the success code.
66
     *
67
     * @return int
68
     */
69
    public static function successCode()
70
    {
71
        static $successCode = null;
72
73
        if (is_null($successCode)) {
74
            $successCode = (int) config('api.response.code.success', 1);
75
        }
76
77
        return $successCode;
78
    }
79
80
    /**
81
     * Sets the data to be sent as JSON.
82
     *
83
     * @param  mixed  $data
84
     * @return $this
85
     */
86
    public function setData($data = null)
87
    {
88
        if ($data instanceof Model) {
0 ignored issues
show
Bug introduced by
The class Illuminate\Database\Eloquent\Model does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
89
            $data = [
90
                snake_case(class_basename($data)) => $this->convertObjectToArray($data),
91
            ];
92
        } elseif (is_object($data)) {
93
            $data = $this->convertObjectToArray($data);
94
        } elseif (is_null($data)) {
95
            $data = [];
96
        } elseif (is_string($data)) {
97
            $data = [static::messageKey() => $data];
98
        } elseif (! is_array($data)) {
99
            $data = [static::messageKey() => json_encode($data)];
100
        }
101
102
        if (! array_key_exists(static::codeKey(), $data)) {
103
            $data[static::codeKey()] = $this->getCode();
104
        }
105
106
        return parent::setData($data);
107
    }
108
109
    /**
110
     * Convert an object to array.
111
     *
112
     * @param  mixed  $object
113
     * @return array
114
     */
115
    protected function convertObjectToArray($object)
116
    {
117
        if (method_exists($object, 'toArray')) {
118
            return $object->toArray();
119
        }
120
121
        return json_decode(json_encode($object, true), true);
122
    }
123
124
    /**
125
     * Merge new data into the current data.
126
     *
127
     * @param  array  ...$data
128
     * @return $this
129
     */
130
    public function mergeData(array ...$data)
131
    {
132
        return $this->setData(array_replace($this->getData(true), ...$data));
133
    }
134
135
    /**
136
     * Remove empty elements from the current data.
137
     *
138
     * @return $this
139
     */
140
    public function removeEmpty()
141
    {
142
        return $this->setData(array_filter($this->getData(true)));
143
    }
144
145
    /**
146
     * Get the api result code.
147
     *
148
     * @return int
149
     */
150
    public function getCode()
151
    {
152
        return $this->code;
153
    }
154
155
    /**
156
     * Set the api result code.
157
     *
158
     * @param  int  $code
159
     * @return $this
160
     */
161
    public function setCode($code)
162
    {
163
        $this->code = (int) $code;
164
165
        return $this->mergeData([static::codeKey() => $this->code]);
166
    }
167
168
    /**
169
     * Set the api result code.
170
     *
171
     * @param  int  $code
172
     * @return $this
173
     */
174
    public function code($code)
175
    {
176
        return $this->setCode($code);
177
    }
178
179
    /**
180
     * Get the api result message.
181
     *
182
     * @return string
183
     */
184
    public function getMessage()
185
    {
186
        return array_get($this->getData(true), static::messageKey());
187
    }
188
189
    /**
190
     * Set the api result message.
191
     *
192
     * @param  string  $message
193
     * @return $this
194
     */
195
    public function setMessage($message)
196
    {
197
        return $this->mergeData([static::messageKey() => (string) $message]);
198
    }
199
200
    /**
201
     * Set the api result message.
202
     *
203
     * @param  string  $message
204
     * @return $this
205
     */
206
    public function message($message)
207
    {
208
        return $this->setMessage($message);
209
    }
210
211
    /**
212
     * Set the response status code.
213
     *
214
     * @param  int  $code
215
     * @param  mixed  $text
216
     * @return $this
217
     */
218
    public function statusCode($code, $text = null)
219
    {
220
        return $this->setStatusCode($code, $text);
221
    }
222
}
223