Completed
Push — master ( 3380bc...af0439 )
by Clayton
14s
created

Payload::withInput()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace BrightComponents\Common\Payloads;
4
5
use Traversable;
6
use JsonSerializable;
7
use Illuminate\Support\Collection;
8
use Illuminate\Contracts\Support\Jsonable;
9
use Illuminate\Contracts\Support\Arrayable;
10
use BrightComponents\Common\Payloads\Contracts\PayloadContract;
11
12
class Payload implements PayloadContract
13
{
14
    /** @var string */
15
    private $status;
16
17
    /** @var array */
18
    private $input = [];
19
20
    /** @var array */
21
    private $output = [];
22
23
    /** @var array */
24
    private $messages = [];
25
26
    /** @var string */
27
    private $wrapper = '';
28
29
    /**
30
     * Create a copy of the payload with the status.
31
     *
32
     * @param  string  $status
33
     *
34
     * @return \BrightComponents\Common\Payloads\Contracts\PayloadContract
35
     */
36
    public function withStatus($status)
37
    {
38
        $copy = clone $this;
39
        $copy->status = $status;
40
41
        return $copy;
42
    }
43
44
    /**
45
     * Get the status of the payload.
46
     *
47
     * @return string
48
     */
49
    public function getStatus()
50
    {
51
        return $this->status;
52
    }
53
54
    /**
55
     * Create a copy of the payload with input array.
56
     *
57
     * @param  array  $input
58
     *
59
     * @return \BrightComponents\Common\Payloads\Contracts\PayloadContract
60
     */
61
    public function withInput(array $input)
62
    {
63
        $copy = clone $this;
64
        $copy->input = $input;
65
66
        return $copy;
67
    }
68
69
    /**
70
     * Get input array from the payload.
71
     *
72
     * @return array
73
     */
74
    public function getInput()
75
    {
76
        return $this->input;
77
    }
78
79
    /**
80
     * Create a copy of the payload with output array.
81
     *
82
     * @param  mixed  $output
83
     *
84
     * @return \BrightComponents\Common\Payloads\Contracts\PayloadContract
85
     */
86
    public function withOutput($output)
87
    {
88
        $copy = clone $this;
89
        $copy->output = $this->wrapper ? ['data' => $this->getArrayableItems($output)] : $this->getArrayableItems($output);
90
91
        return $copy;
92
    }
93
94
    /**
95
     * Get output array from the payload.
96
     *
97
     * @return array
98
     */
99
    public function getOutput()
100
    {
101
        return $this->output;
102
    }
103
104
    /**
105
     * Create a copy of the payload with messages array.
106
     *
107
     * @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...
108
     *
109
     * @return \BrightComponents\Common\Payloads\Contracts\PayloadContract
110
     */
111
    public function withMessages(array $messages)
112
    {
113
        $copy = clone $this;
114
        $copy->messages = $messages;
115
116
        return $copy;
117
    }
118
119
    /**
120
     * Get messages array from the payload.
121
     *
122
     * @return array
123
     */
124
    public function getMessages()
125
    {
126
        return $this->messages;
127
    }
128
129
    /**
130
     * Set a wrapper for payload output.
131
     *
132
     * @param  string  $wrapper
133
     *
134
     * @return $this
135
     */
136
    public function setWrapper(string $wrapper)
137
    {
138
        $this->wrapper = $wrapper;
139
140
        return $this;
141
    }
142
143
    /**
144
     * Set a wrapper for payload output. Alias for setWrapper.
145
     *
146
     * @param  string  $wrapper
147
     *
148
     * @return $this
149
     */
150
    public function wrap(string $wrapper)
151
    {
152
        return $this->setWrapper($wrapper);
153
    }
154
155
    /**
156
     * Get the arrayable items.
157
     *
158
     * @param  mixed  $input
159
     *
160
     * @return array
161
     */
162
    public function getArrayableItems($input)
163
    {
164
        if (is_array($input)) {
165
            return $input;
166
        } elseif ($input instanceof Collection) {
167
            return $input->all();
168
        } elseif ($input instanceof Arrayable) {
169
            return $input->toArray();
170
        } elseif ($input instanceof Jsonable) {
171
            return json_decode($input->toJson(), true);
172
        } elseif ($input 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...
173
            return $input->jsonSerialize();
174
        } elseif ($input instanceof Traversable) {
175
            return iterator_to_array($input);
176
        }
177
178
        return (array) $input;
179
    }
180
}
181