Passed
Push — master ( 17e6cc...350d8b )
by Aleksandr
41:29 queued 06:24
created

Transporter::__toObject()   A

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
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the eav package.
4
 *
5
 * @author    Alex Kuperwood <[email protected]>
6
 * @copyright 2025 Alex Kuperwood
7
 * @license   https://opensource.org/license/mit  The MIT License
8
 */
9
declare(strict_types=1);
10
11
namespace Kuperwood\Eav;
12
13
class Transporter
14
{
15
    private array $data = [];
16
17 1
    public function __get(string $field)
18
    {
19 1
        return $this->getField($field);
20
    }
21
22 1
    public function __set(string $field, $value): void
23
    {
24 1
        $this->setField($field, $value);
25
    }
26
27 1
    public function __isset(string $field): bool
28
    {
29 1
        return $this->hasField($field);
30
    }
31
32 1
    public function __unset(string $field): void
33
    {
34 1
        $this->removeField($field);
35
    }
36
37 1
    public function __toString(): string
38
    {
39 1
        return json_encode($this->data);
40
    }
41
42 1
    public function __toArray(): array
43
    {
44 1
        return $this->data;
45
    }
46
47 1
    public function __toJson(): string
48
    {
49 1
        return json_encode($this->data);
50
    }
51
52 1
    public function __toObject(): object
53
    {
54 1
        return (object) $this->data;
55
    }
56
57 1
    public function setField($field, $value): self
58
    {
59 1
        $this->data[$field] = $value;
60
61 1
        return $this;
62
    }
63
64
    /**
65
     * @param $field
66
     */
67 1
    public function getField($field)
68
    {
69 1
        if (!$this->hasField($field)) {
70 1
            return null;
71
        }
72
73 1
        return $this->data[$field];
74
    }
75
76 1
    public function getData(): array
77
    {
78 1
        return $this->data;
79
    }
80
81 1
    public function setData(array $data): self
82
    {
83 1
        $this->data = $data;
84
85 1
        return $this;
86
    }
87
88 1
    public function hasField($field): bool
89
    {
90 1
        return array_key_exists($field, $this->data);
91
    }
92
93 1
    public function removeField($field): void
94
    {
95 1
        unset($this->data[$field]);
96
    }
97
98 1
    public function clear(): void
99
    {
100 1
        $this->data = [];
101
    }
102
}
103