Completed
Push — master ( 842a9e...3380bc )
by Clayton
12s
created

Payload::getMessages()   A

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 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
    /**
27
     * Create a copy of the payload with the status.
28
     *
29
     * @param  string  $status
30
     *
31
     * @return \BrightComponents\Common\Payloads\Contracts\PayloadContract
32
     */
33
    public function withStatus($status)
34
    {
35
        $copy = clone $this;
36
        $copy->status = $status;
37
38
        return $copy;
39
    }
40
41
    /**
42
     * Get the status of the payload.
43
     *
44
     * @return string
45
     */
46
    public function getStatus()
47
    {
48
        return $this->status;
49
    }
50
51
    /**
52
     * Create a copy of the payload with input array.
53
     *
54
     * @param  array  $input
55
     *
56
     * @return \BrightComponents\Common\Payloads\Contracts\PayloadContract
57
     */
58
    public function withInput(array $input)
59
    {
60
        $copy = clone $this;
61
        $copy->input = $input;
62
63
        return $copy;
64
    }
65
66
    /**
67
     * Get input array from the payload.
68
     *
69
     * @return array
70
     */
71
    public function getInput()
72
    {
73
        return $this->input;
74
    }
75
76
    /**
77
     * Create a copy of the payload with output array.
78
     *
79
     * @param  array  $output
80
     *
81
     * @return \BrightComponents\Common\Payloads\Contracts\PayloadContract
82
     */
83
    public function withOutput(array $output)
84
    {
85
        $copy = clone $this;
86
        $copy->output = $this->getArrayableItems($output);
87
88
        return $copy;
89
    }
90
91
    /**
92
     * Get output array from the payload.
93
     *
94
     * @return array
95
     */
96
    public function getOutput()
97
    {
98
        return $this->output;
99
    }
100
101
    /**
102
     * Create a copy of the payload with messages array.
103
     *
104
     * @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...
105
     *
106
     * @return \BrightComponents\Common\Payloads\Contracts\PayloadContract
107
     */
108
    public function withMessages(array $messages)
109
    {
110
        $copy = clone $this;
111
        $copy->messages = $messages;
112
113
        return $copy;
114
    }
115
116
    /**
117
     * Get messages array from the payload.
118
     *
119
     * @return array
120
     */
121
    public function getMessages()
122
    {
123
        return $this->messages;
124
    }
125
126
    public function getArrayableItems($input)
127
    {
128
        if (is_array($input)) {
129
            return $input;
130
        } elseif ($input instanceof Collection) {
131
            return $input->all();
132
        } elseif ($input instanceof Arrayable) {
133
            return $input->toArray();
134
        } elseif ($input instanceof Jsonable) {
135
            return json_decode($input->toJson(), true);
136
        } 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...
137
            return $input->jsonSerialize();
138
        } elseif ($input instanceof Traversable) {
139
            return iterator_to_array($input);
140
        }
141
142
        return (array) $input;
143
    }
144
}
145