Completed
Pull Request — master (#22)
by Sergey
12:08
created

Containerable   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 0
dl 0
loc 49
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __get() 0 4 2
A __set() 0 4 1
A __isset() 0 4 1
A __unset() 0 4 1
A fill() 0 4 1
A toArray() 0 4 1
A toJson() 0 4 1
A __toString() 0 4 1
A jsonSerialize() 0 4 1
1
<?php
2
3
namespace Isswp101\Persimmon\Traits;
4
5
trait Containerable
6
{
7
    protected $attributes = [];
8
9
    public function __get($name)
10
    {
11
        return array_key_exists($name, $this->attributes) ? $this->attributes[$name] : null;
12
    }
13
14
    public function __set($name, $value)
15
    {
16
        $this->attributes[$name] = $value;
17
    }
18
19
    public function __isset($name)
20
    {
21
        return isset($this->attributes[$name]);
22
    }
23
24
    public function __unset($name)
25
    {
26
        unset($this->attributes[$name]);
27
    }
28
29
    public function fill(array $attributes): void
30
    {
31
        $this->attributes = $attributes;
32
    }
33
34
    public function toArray(): array
35
    {
36
        return $this->attributes;
37
    }
38
39
    public function toJson(int $options = 0): string
40
    {
41
        return json_encode($this->toArray(), $options);
42
    }
43
44
    public function __toString(): string
45
    {
46
        return static::class . $this->toJson();
47
    }
48
49
    public function jsonSerialize()
50
    {
51
        return $this->toArray();
52
    }
53
}