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