Completed
Push — master ( 2f25c9...4c0020 )
by
unknown
06:10
created

Notification::__set()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
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);
0 ignored issues
show
Documentation introduced by
$this is of type this<Fenos\Notifynder\Builder\Notification>, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
166
            $notifynderParse = new NotificationParser();
167
168
            return $notifynderParse->parse($notification, $this->category_id);
0 ignored issues
show
Documentation introduced by
The property category_id does not exist on object<Fenos\Notifynder\Builder\Notification>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
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