Test Failed
Push — master ( afd99a...0309cf )
by Ilya
03:13
created

FlashMessage::jsonSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Coderello\Laraflash\FlashMessage;
4
5
use ArrayAccess;
6
use JsonSerializable;
7
use Illuminate\Contracts\Support\Jsonable;
8
use Illuminate\Contracts\Support\Arrayable;
9
use Coderello\Laraflash\Exceptions\InvalidDelayException;
10
use Coderello\Laraflash\Exceptions\InvalidHopsAmountException;
11
12
class FlashMessage implements ArrayAccess, Arrayable, Jsonable, JsonSerializable
13
{
14
    /** @var string|null */
15
    protected $content;
16
17
    /** @var string|null */
18
    protected $title;
19
20
    /** @var string|null */
21
    protected $type;
22
23
    /** @var int|null */
24
    protected $hops = 1;
25
26
    /** @var int|null */
27
    protected $delay = 1;
28
29
    /** @var array */
30
    protected $attributes = [];
31
32
    public function content(?string $content): self
33
    {
34
        if (! is_null($content)) {
35
            $this->content = $content;
36
        } else {
37
            unset($this->content);
38
        }
39
40
        return $this;
41
    }
42
43
    public function title(?string $title): self
44
    {
45
        if (! is_null($title)) {
46
            $this->title = $title;
47
        } else {
48
            unset($this->title);
49
        }
50
51
        return $this;
52
    }
53
54
    public function type(?string $type): self
55
    {
56
        if (! is_null($type)) {
57
            $this->type = $type;
58
        } else {
59
            unset($this->type);
60
        }
61
62
        return $this;
63
    }
64
65
    public function danger(): self
66
    {
67
        $this->type('danger');
68
69
        return $this;
70
    }
71
72
    public function warning(): self
73
    {
74
        $this->type('warning');
75
76
        return $this;
77
    }
78
79
    public function info(): self
80
    {
81
        $this->type('info');
82
83
        return $this;
84
    }
85
86
    public function success(): self
87
    {
88
        $this->type('success');
89
90
        return $this;
91
    }
92
93
    public function hops(int $hops): self
94
    {
95
        if ($hops < 1) {
96
            throw new InvalidHopsAmountException;
97
        }
98
99
        $this->hops = $hops;
100
101
        return $this;
102
    }
103
104
    public function delay(int $delay): self
105
    {
106
        if ($delay < 0) {
107
            throw new InvalidDelayException;
108
        }
109
110
        $this->delay = $delay;
111
112
        return $this;
113
    }
114
115
    public function now(): self
116
    {
117
        $this->delay(0);
118
119
        return $this;
120
    }
121
122
    public function keep(): self
123
    {
124
        $this->hops++;
125
126
        return $this;
127
    }
128
129
    public function attribute(string $key, $value = null): self
130
    {
131
        if (! is_null($value)) {
132
            $this->attributes[$key] = $value;
133
        } else {
134
            unset($this->attributes[$key]);
135
        }
136
137
        return $this;
138
    }
139
140
    public function get(string $key)
141
    {
142
        if (in_array($key, ['title', 'content', 'hops', 'delay', 'type'])) {
143
            return $this->{$key};
144
        }
145
146
        return $this->attributes[$key] ?? null;
147
    }
148
149
    public function toArray()
150
    {
151
        return array_merge(
152
            $this->attributes, [
153
                'content' => $this->content,
154
                'title' => $this->title,
155
                'type' => $this->type,
156
                'hops' => $this->hops,
157
                'delay' => $this->delay,
158
            ]
159
        );
160
    }
161
162
    public function toJson($options = 0)
163
    {
164
        return json_encode($this, $options);
165
    }
166
167
    public function jsonSerialize()
168
    {
169
        return $this->toArray();
170
    }
171
172
    public function offsetExists($offset)
173
    {
174
        return isset($this->attributes[$offset]);
175
    }
176
177
    public function offsetGet($offset)
178
    {
179
        return $this->attributes[$offset];
180
    }
181
182
    public function offsetSet($offset, $value)
183
    {
184
        $this->attribute($offset, $value);
185
    }
186
187
    public function offsetUnset($offset)
188
    {
189
        unset($this->attributes[$offset]);
190
    }
191
192
    public function __get($name)
193
    {
194
        return $this->attributes[$name] ?? null;
195
    }
196
197
    public function __set($name, $value)
198
    {
199
        $this->attribute($name, $value);
200
    }
201
202
    public function __isset($name)
203
    {
204
        return isset($this->attributes[$name]);
205
    }
206
207
    public function __unset($name)
208
    {
209
        unset($this->attributes[$name]);
210
    }
211
}
212