Completed
Push — version-4 ( 75ffe7...58de13 )
by
unknown
06:48
created

Notification::toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Fenos\Notifynder\Builder;
4
5
use ArrayAccess;
6
use Illuminate\Contracts\Support\Arrayable;
7
use Illuminate\Contracts\Support\Jsonable;
8
use Illuminate\Support\Arr;
9
use JsonSerializable;
10
11
/**
12
 * Class Notification.
13
 */
14
class Notification implements Arrayable, ArrayAccess, Jsonable, JsonSerializable
15
{
16
    /**
17
     * @var array
18
     */
19
    protected $attributes = [];
20
21
    /**
22
     * @var array
23
     */
24
    protected $requiredFields = [
25
        'from_id',
26
        'to_id',
27
        'category_id',
28
    ];
29
30
    /**
31
     * Notification constructor.
32
     */
33
    public function __construct()
34
    {
35
        $customRequired = notifynder_config()->getAdditionalRequiredFields();
36
        $this->requiredFields = array_merge($this->requiredFields, $customRequired);
37
    }
38
39
    /**
40
     * @return array
41
     */
42
    public function attributes()
43
    {
44
        return $this->attributes;
45
    }
46
47
    /**
48
     * @param string $key
49
     * @param null|mixed $default
50
     * @return mixed
51
     */
52
    public function attribute($key, $default = null)
53
    {
54
        return $this->get($key, $default);
55
    }
56
57
    /**
58
     * @param string $key
59
     * @return bool
60
     */
61
    public function has($key)
62
    {
63
        return Arr::has($this->attributes, $key);
64
    }
65
66
    /**
67
     * @param string $key
68
     * @param null|mixed $default
69
     * @return mixed
70
     */
71
    public function get($key, $default = null)
72
    {
73
        return Arr::get($this->attributes, $key, $default);
74
    }
75
76
    /**
77
     * @param string $key
78
     * @param mixed $value
79
     */
80
    public function set($key, $value)
81
    {
82
        Arr::set($this->attributes, $key, $value);
83
    }
84
85
    /**
86
     * @return bool
87
     */
88
    public function isValid()
89
    {
90
        foreach ($this->requiredFields as $field) {
91
            if (! $this->has($field)) {
92
                return false;
93
            }
94
        }
95
96
        return true;
97
    }
98
99
    /**
100
     * @param string $key
101
     * @return mixed
102
     */
103
    public function __get($key)
104
    {
105
        return $this->get($key);
106
    }
107
108
    /**
109
     * @param string $key
110
     * @param mixed $value
111
     */
112
    public function __set($key, $value)
113
    {
114
        $this->set($key, $value);
115
    }
116
117
    /**
118
     * @param int $options
119
     * @return string
120
     */
121
    public function toJson($options = 0)
122
    {
123
        return json_encode($this->jsonSerialize(), $options);
124
    }
125
126
    /**
127
     * @return array
128
     */
129
    public function jsonSerialize()
130
    {
131
        return $this->toArray();
132
    }
133
134
    /**
135
     * @return array
136
     */
137
    public function toArray()
138
    {
139
        return array_map(function ($value) {
140
            return $value instanceof Arrayable ? $value->toArray() : $value;
141
        }, $this->attributes());
142
    }
143
144
    /**
145
     * @return array
146
     */
147
    public function toDbArray()
148
    {
149
        $notification = $this->toArray();
150
        if (array_key_exists('extra', $notification) && is_array($notification['extra'])) {
151
            $notification['extra'] = json_encode($notification['extra']);
152
        }
153
154
        return $notification;
155
    }
156
157
    /**
158
     * @return string
159
     */
160
    public function toString()
161
    {
162
        return $this->toJson();
163
    }
164
165
    /**
166
     * @return string
167
     */
168
    public function __toString()
169
    {
170
        return $this->toString();
171
    }
172
173
    /**
174
     * @param string $offset
175
     * @return bool
176
     */
177
    public function offsetExists($offset)
178
    {
179
        return $this->has($offset);
180
    }
181
182
    /**
183
     * @param string $offset
184
     * @return mixed
185
     */
186
    public function offsetGet($offset)
187
    {
188
        return $this->get($offset);
189
    }
190
191
    /**
192
     * @param string $offset
193
     * @param mixed $value
194
     */
195
    public function offsetSet($offset, $value)
196
    {
197
        $this->set($offset, $value);
198
    }
199
200
    /**
201
     * @param string $offset
202
     */
203
    public function offsetUnset($offset)
204
    {
205
        Arr::forget($this->attributes, $offset);
206
    }
207
}
208