Completed
Push — version-4 ( 100839...4b96e4 )
by
unknown
02:31
created

Notification::offsetGet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 4
rs 10
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 __toString()
91
    {
92
        return $this->toJson();
93
    }
94
95
    public function offsetExists($offset)
96
    {
97
        return $this->has($offset);
98
    }
99
100
    public function offsetGet($offset)
101
    {
102
        return $this->get($offset);
103
    }
104
105
    public function offsetSet($offset, $value)
106
    {
107
        $this->set($offset, $value);
108
    }
109
110
    public function offsetUnset($offset)
111
    {
112
        Arr::forget($this->attributes, $offset);
113
    }
114
}
115