Passed
Push — ft/dynamic-attributes ( 48373f...169e79 )
by Ben
06:24
created

DynamicAttributes::toJson()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Thinktomorrow\Chief\DynamicAttributes;
5
6
class DynamicAttributes
7
{
8
    /** @var array */
9
    private $values;
10
11
    public function __construct(array $values)
12
    {
13
        $this->values = $values;
14
    }
15
16
    public static function fromRawValue($value): self
17
    {
18
        $value = is_null($value) ? [] : (is_array($value) ? $value : json_decode($value, true));
19
20
        return new static((array) $value);
21
    }
22
23
    public function has(string $key): bool
24
    {
25
        return ($this->get($key, '__NOTFOUND__') !== '__NOTFOUND__');
26
    }
27
28
    public function get(string $key, $default = null)
29
    {
30
        return data_get($this->values, $key, $default);
31
    }
32
33
    public function set(string $key, $value)
34
    {
35
        data_set($this->values, $key, $value);
36
    }
37
38
    public function all()
39
    {
40
        return $this->values;
41
    }
42
43
    public function toJson(): string
44
    {
45
        return json_encode($this->all());
46
    }
47
}
48