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

Containerable::toJson()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 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
}