HasDataArrayWithAttributes::isEmpty()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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