Passed
Push — master ( 5e2b09...a39721 )
by Ben
09:28 queued 02:28
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
3
declare(strict_types=1);
4
5
namespace Thinktomorrow\Chief\DynamicAttributes;
6
7
class DynamicAttributes
8
{
9
    /** @var array */
10
    private $values;
11
12
    public function __construct(array $values)
13
    {
14
        $this->values = $values;
15
    }
16
17
    public static function fromRawValue($value): self
18
    {
19
        $value = is_null($value) ? [] : (is_array($value) ? $value : json_decode($value, true));
20
21
        return new static((array) $value);
22
    }
23
24
    public function has(string $key): bool
25
    {
26
        return ($this->get($key, '__NOTFOUND__') !== '__NOTFOUND__');
27
    }
28
29
    public function get(string $key, $default = null)
30
    {
31
        return data_get($this->values, $key, $default);
32
    }
33
34
    public function set(string $key, $value)
35
    {
36
        data_set($this->values, $key, $value);
37
    }
38
39
    public function all()
40
    {
41
        return $this->values;
42
    }
43
44
    public function toJson(): string
45
    {
46
        return json_encode($this->all());
47
    }
48
}
49