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
|
|
|
class Notification implements Arrayable, ArrayAccess, Jsonable, JsonSerializable |
12
|
|
|
{ |
13
|
|
|
protected $attributes = []; |
14
|
|
|
|
15
|
|
|
protected $requiredFields = [ |
16
|
|
|
'from_id', |
17
|
|
|
'to_id', |
18
|
|
|
'category_id', |
19
|
|
|
]; |
20
|
|
|
|
21
|
|
|
public function __construct() |
22
|
|
|
{ |
23
|
|
|
$customRequired = notifynder_config()->getAdditionalRequiredFields(); |
24
|
|
|
$this->requiredFields = array_merge($this->requiredFields, $customRequired); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function attributes() |
28
|
|
|
{ |
29
|
|
|
return $this->attributes; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function attribute($key, $default = null) |
33
|
|
|
{ |
34
|
|
|
return $this->get($key, $default); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function has($key) |
38
|
|
|
{ |
39
|
|
|
return Arr::has($this->attributes, $key); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function get($key, $default = null) |
43
|
|
|
{ |
44
|
|
|
return Arr::get($this->attributes, $key, $default); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function set($key, $value) |
48
|
|
|
{ |
49
|
|
|
Arr::set($this->attributes, $key, $value); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function isValid() |
53
|
|
|
{ |
54
|
|
|
foreach ($this->requiredFields as $field) { |
55
|
|
|
if (! $this->has($field)) { |
56
|
|
|
return false; |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return true; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function __get($key) |
64
|
|
|
{ |
65
|
|
|
return $this->get($key); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function __set($key, $value) |
69
|
|
|
{ |
70
|
|
|
$this->set($key, $value); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function toJson($options = 0) |
74
|
|
|
{ |
75
|
|
|
return json_encode($this->jsonSerialize(), $options); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function jsonSerialize() |
79
|
|
|
{ |
80
|
|
|
return $this->toArray(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function toArray() |
84
|
|
|
{ |
85
|
|
|
return array_map(function ($value) { |
86
|
|
|
return $value instanceof Arrayable ? $value->toArray() : $value; |
87
|
|
|
}, $this->attributes()); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function toDbArray() |
91
|
|
|
{ |
92
|
|
|
$notification = $this->toArray(); |
93
|
|
|
if(is_array($notification['extra'])) { |
94
|
|
|
$notification['extra'] = json_encode($notification['extra']); |
95
|
|
|
} |
96
|
|
|
return $notification; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function __toString() |
100
|
|
|
{ |
101
|
|
|
return $this->toJson(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function offsetExists($offset) |
105
|
|
|
{ |
106
|
|
|
return $this->has($offset); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function offsetGet($offset) |
110
|
|
|
{ |
111
|
|
|
return $this->get($offset); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function offsetSet($offset, $value) |
115
|
|
|
{ |
116
|
|
|
$this->set($offset, $value); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function offsetUnset($offset) |
120
|
|
|
{ |
121
|
|
|
Arr::forget($this->attributes, $offset); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|