Payload::getOutputWrapper()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace PerfectOblivion\Payload;
4
5
use ArrayAccess;
6
use Traversable;
7
use JsonSerializable;
8
use Illuminate\Contracts\Support\Jsonable;
9
use Illuminate\Contracts\Support\Arrayable;
10
use PerfectOblivion\Payload\Contracts\PayloadContract;
11
12
class Payload implements PayloadContract, ArrayAccess, JsonSerializable, Jsonable, Arrayable
13
{
14
    /** @var int */
15
    private $status;
16
17
    /** @var mixed */
18
    private $output;
19
20
    /** @var array */
21
    private $messages = [];
22
23
    /** @var string|null */
24
    private $outputWrapper = null;
25
26
    /** @var string */
27
    private $messagesWrapper = 'messages';
28
29
    /**
30
     * Initialize a Payload object.
31
     */
32
    public static function instance(): PayloadContract
33
    {
34
        return new static;
35
    }
36
37
    /**
38
     * Set the Payload status.
39
     *
40
     * @param  int  $status
41
     */
42
    public function setStatus(int $status): PayloadContract
43
    {
44
        $instance = clone $this;
45
        $instance->status = $status;
46
47
        return $instance;
48
    }
49
50
    /**
51
     * Get the status of the payload.
52
     */
53
    public function getStatus(): int
54
    {
55
        return $this->status;
56
    }
57
58
    /**
59
     * Set the Payload messages.
60
     *
61
     * @param  array  $output
0 ignored issues
show
Bug introduced by
There is no parameter named $output. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
62
     */
63
    public function setMessages(array $messages): PayloadContract
64
    {
65
        $instance = clone $this;
66
        $instance->messages = $messages;
67
68
        return $instance;
69
    }
70
71
    /**
72
     * Get messages array from the payload.
73
     */
74
    public function getMessages(): array
75
    {
76
        return $this->messages;
77
    }
78
79
    /**
80
     * Set the Payload output.
81
     *
82
     * @param  mixed  $output
83
     * @param  string|null  $wrapper
84
     */
85
    public function setOutput($output, ? string $wrapper = null): PayloadContract
86
    {
87
        if ($wrapper) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $wrapper of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
88
            $this->outputWrapper = $wrapper;
89
        }
90
91
        $instance = clone $this;
92
        $instance->output = $output;
93
94
        return $instance;
95
    }
96
97
    /**
98
     * Get the Payload output.
99
     */
100
    public function getOutput(): array
101
    {
102
        return $this->getArrayableItems($this->output);
103
    }
104
105
    /**
106
     * Get the raw Payload output.
107
     *
108
     * @return mixed
109
     */
110
    public function getRawOutput()
111
    {
112
        return $this->output;
113
    }
114
115
    /**
116
     * Retrieve the Payload output and wrap it.
117
     * Use the outputWrapper if it is set, otherwise use 'data'.
118
     */
119
    public function getwrappedOutput(): array
120
    {
121
        return $this->outputWrapper ? [$this->outputWrapper => $this->output] : ['data' => $this->output];
122
    }
123
124
    /**
125
     * Get the wrapper for the output.
126
     */
127
    public function getOutputWrapper(): string
128
    {
129
        return $this->outputWrapper;
130
    }
131
132
    /**
133
     * Get the wrapper for the messages.
134
     */
135
    public function getMessagesWrapper(): string
136
    {
137
        return $this->messagesWrapper;
138
    }
139
140
    /**
141
     * Return all of the components of the payload in array format.
142
     */
143
    public function all(): array
144
    {
145
        $all = ['output' => $this->getOutput()];
146
        if ($this->messages) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->messages of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
147
            $all = array_merge($all, ['messages' => $this->getMessages()]);
148
        }
149
        if ($this->status) {
150
            $all = array_merge($all, ['status' => $this->getStatus()]);
151
        }
152
153
        return $all;
154
    }
155
156
    /**
157
     * Convert the Payload output to an array.
158
     */
159
    public function getArrayableItems(): array
160
    {
161
        if (is_array($this->output)) {
162
            return $this->output;
163
        } elseif ($this->output instanceof Arrayable) {
164
            return $this->output->toArray();
165
        } elseif ($this->output instanceof Jsonable) {
166
            return json_decode($this->output->toJson(), true);
167
        } elseif ($this->output instanceof JsonSerializable) {
0 ignored issues
show
Bug introduced by
The class JsonSerializable does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
168
            return $this->output->jsonSerialize();
169
        } elseif ($this->output instanceof Traversable) {
170
            return iterator_to_array($this->output);
171
        }
172
173
        return (array) $this->output;
174
    }
175
176
    /**
177
     * Dynamically retrieve attributes on the OutputItem.
178
     *
179
     * @param  string  $key
180
     *
181
     * @return mixed
182
     */
183
    public function __get($key)
184
    {
185
        return $this->output[$key];
186
    }
187
188
    /**
189
     * Convert the Payload instance to JSON.
190
     *
191
     * @param  int  $options
192
     *
193
     * @return string
194
     */
195
    public function toJson($options = 0)
196
    {
197
        return json_encode($this->jsonSerialize(), $options);
198
    }
199
200
    /**
201
     * Convert the Payload into something JSON serializable.
202
     *
203
     * @return array
204
     */
205
    public function jsonSerialize()
206
    {
207
        return $this->toArray();
208
    }
209
210
    /**
211
     * Convert the Payload instance to an array.
212
     *
213
     * @return array
214
     */
215
    public function toArray()
216
    {
217
        $output = $this->outputWrapper || $this->messages ? $this->getWrappedOutput() : $this->getOutput();
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->outputWrapper of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
Bug Best Practice introduced by
The expression $this->messages of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
218
        $messages = $this->messages ? [$this->messagesWrapper => $this->messages] : $this->messages;
219
220
        return $this->messages ? array_merge($output, $messages) : $output;
221
    }
222
223
    /**
224
     * Determine if the given attribute exists.
225
     *
226
     * @param  mixed  $offset
227
     *
228
     * @return bool
229
     */
230
    public function offsetExists($offset)
231
    {
232
        return isset($this->$offset);
233
    }
234
235
    /**
236
     * Get the value for a given offset.
237
     *
238
     * @param  mixed  $offset
239
     *
240
     * @return mixed
241
     */
242
    public function offsetGet($offset)
243
    {
244
        return $this->$offset;
245
    }
246
247
    /**
248
     * Set the value for a given offset.
249
     *
250
     * @param  mixed  $offset
251
     * @param  mixed  $value
252
     */
253
    public function offsetSet($offset, $value)
254
    {
255
        $this->$offset = $value;
256
    }
257
258
    /**
259
     * Unset the value for a given offset.
260
     *
261
     * @param  mixed  $offset
262
     */
263
    public function offsetUnset($offset)
264
    {
265
        unset($this->$offset);
266
    }
267
268
    /**
269
     * Determine if an attribute exists on the Payload.
270
     *
271
     * @param  string  $key
272
     *
273
     * @return bool
274
     */
275
    public function __isset($key)
276
    {
277
        if (! $this->output) {
278
            return false;
279
        }
280
281
        return isset($this->output[$key]);
282
    }
283
284
    /**
285
     * Unset an attribute on the Payload.
286
     *
287
     * @param  string  $key
288
     */
289
    public function __unset($key)
290
    {
291
        if (! $this->output) {
292
            return;
293
        }
294
295
        unset($this->output[$key]);
296
    }
297
298
    /**
299
     * Convert the Payload to its string representation.
300
     *
301
     * @return string
302
     */
303
    public function __toString()
304
    {
305
        return $this->toJson();
306
    }
307
}
308