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

Containerable   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 0
dl 0
loc 50
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 5 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)
30
    {
31
        $this->attributes = $attributes;
32
        return $this;
33
    }
34
35
    public function toArray(): array
36
    {
37
        return $this->attributes;
38
    }
39
40
    public function toJson(int $options = 0): string
41
    {
42
        return json_encode($this->toArray(), $options);
43
    }
44
45
    public function __toString(): string
46
    {
47
        return static::class . $this->toJson();
48
    }
49
50
    public function jsonSerialize()
51
    {
52
        return $this->toArray();
53
    }
54
}