Completed
Push — version-4 ( a26cca...5903bf )
by
unknown
02:38
created

Notification::attribute()   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 2
dl 0
loc 4
rs 10
1
<?php
2
3
namespace Fenos\Notifynder\Builder;
4
5
use JsonSerializable;
6
use Illuminate\Contracts\Support\Arrayable;
7
use Illuminate\Contracts\Support\Jsonable;
8
use Illuminate\Support\Arr;
9
10
class Notification implements Arrayable, Jsonable, JsonSerializable
11
{
12
    protected $attributes = [];
13
14
    protected $requiredFields = [
15
        'from_id',
16
        'to_id',
17
        'category_id',
18
    ];
19
20
    public function __construct()
21
    {
22
        $customRequired = notifynder_config()->getAdditionalRequiredFields();
23
        $this->requiredFields = array_merge($this->requiredFields, $customRequired);
24
    }
25
26
    public function attributes()
27
    {
28
        return $this->attributes;
29
    }
30
31
    public function attribute($key, $default = null)
32
    {
33
        return $this->get($key, $default);
34
    }
35
36
    public function has($key)
37
    {
38
        return Arr::has($this->attributes, $key);
39
    }
40
41
    public function get($key, $default = null)
42
    {
43
        return Arr::get($this->attributes, $key, $default);
44
    }
45
46
    public function set($key, $value)
47
    {
48
        Arr::set($this->attributes, $key, $value);
49
    }
50
51
    public function isValid()
52
    {
53
        foreach ($this->requiredFields as $field) {
54
            if (! $this->has($field)) {
55
                return false;
56
            }
57
        }
58
59
        return true;
60
    }
61
62
    public function __get($key)
63
    {
64
        return $this->get($key);
65
    }
66
67
    public function __set($key, $value)
68
    {
69
        $this->set($key, $value);
70
    }
71
72
    public function toJson($options = 0)
73
    {
74
        return json_encode($this->jsonSerialize(), $options);
75
    }
76
77
    public function jsonSerialize()
78
    {
79
        return $this->toArray();
80
    }
81
82
    public function toArray()
83
    {
84
        return array_map(function ($value) {
85
            return $value instanceof Arrayable ? $value->toArray() : $value;
86
        }, $this->attributes());
87
    }
88
89
    public function __toString()
90
    {
91
        return $this->toJson();
92
    }
93
}
94