Completed
Push — version-4 ( a7057e...e7ecec )
by
unknown
09:30
created

Notification::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 3
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 5
rs 9.4285
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
        return true;
59
    }
60
61
    public function __get($key)
62
    {
63
        return $this->get($key);
64
    }
65
66
    public function __set($key, $value)
67
    {
68
        $this->set($key, $value);
69
    }
70
71
    public function toJson($options = 0)
72
    {
73
        return json_encode($this->jsonSerialize(), $options);
74
    }
75
76
    public function jsonSerialize()
77
    {
78
        return $this->toArray();
79
    }
80
81
    public function toArray()
82
    {
83
        return array_map(function ($value) {
84
            return $value instanceof Arrayable ? $value->toArray() : $value;
85
        }, $this->attributes);
86
    }
87
88
    public function __toString()
89
    {
90
        return $this->toJson();
91
    }
92
}
93