HasDataArrayWithAttributes   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 21
c 3
b 0
f 0
dl 0
loc 72
ccs 29
cts 29
cp 1
rs 10
wmc 12

10 Methods

Rating   Name   Duplication   Size   Complexity  
A isEmpty() 0 3 1
A hasAttribute() 0 3 1
A getRawData() 0 8 2
A removeAttribute() 0 5 1
A jsonSerialize() 0 3 1
A setAttribute() 0 5 1
A toArray() 0 3 1
A getAttribute() 0 3 1
A setData() 0 5 1
A getRawDataExcept() 0 9 2
1
<?php
2
3
namespace JsonFieldCast\Json;
4
5
use Illuminate\Support\Arr;
6
7
/**
8
 * @property array $data
9
 */
10
trait HasDataArrayWithAttributes
11
{
12
    use HasDateAttributes, HasNumericAttributes, HasMorphClassesAttributes;
13
14 14
    public function setData(array $data = []): static
15
    {
16 14
        $this->data = $data;
17
18 14
        return $this;
19
    }
20
21 7
    public function getRawData(array|string|null $keys = null): array
22
    {
23 7
        if (is_null($keys)) {
24 4
            return $this->data;
25
        }
26 3
        $keys = (array)$keys;
27
28 3
        return Arr::only($this->data, $keys);
29
    }
30
31 4
    public function getRawDataExcept(array|string $keys = []): array
32
    {
33 4
        $keys = (array)$keys;
34
35 4
        if (!empty($keys)) {
36 2
            return array_diff_key($this->data, array_flip($keys));
37
        }
38
39 2
        return $this->data;
40
    }
41
42 2
    public function isEmpty(): bool
43
    {
44 2
        return empty($this->data);
45
    }
46
47 50
    public function getAttribute(string $key, mixed $default = null): mixed
48
    {
49 50
        return Arr::get($this->data, $key, $default);
50
    }
51
52 2
    public function hasAttribute(string $key): bool
53
    {
54 2
        return Arr::has($this->data, $key);
55
    }
56
57 10
    public function setAttribute(string $key, $value): static
58
    {
59 10
        Arr::set($this->data, $key, $value);
60
61 10
        return $this;
62
    }
63
64 2
    public function removeAttribute(string $key): static
65
    {
66 2
        Arr::forget($this->data, $key);
67
68 2
        return $this;
69
    }
70
71 49
    public function toArray(): array
72
    {
73 49
        return $this->data;
74
    }
75
76
    /**
77
     * @inheritDoc
78
     */
79 49
    public function jsonSerialize(): mixed
80
    {
81 49
        return $this->toArray();
82
    }
83
}
84