Completed
Push — master ( 70f45f...310ffc )
by Maxim
02:42
created

AbstractEntity::mapFromArray()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.2
c 0
b 0
f 0
cc 4
eloc 6
nc 8
nop 2
1
<?php
2
3
namespace WebComplete\core\entity;
4
5
use function WebComplete\core\utils\typecast\cast;
0 ignored issues
show
introduced by
The function WebComplete\core\utils\typecast\cast was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
6
7
abstract class AbstractEntity
8
{
9
10
    protected $id;
11
12
    private $data = [];
13
14
    /**
15
     * @return array
16
     */
17
    abstract public static function fields(): array;
18
19
    /**
20
     * @return int|string|null
21
     */
22
    public function getId()
23
    {
24
        return $this->id;
25
    }
26
27
    /**
28
     * @param int|string $id
29
     */
30
    public function setId($id)
31
    {
32
        $this->id = $id;
33
    }
34
35
    /**
36
     * @param array $data
37
     * @param bool $update
38
     */
39
    public function mapFromArray(array $data, bool $update = false)
40
    {
41
        if (!$update) {
42
            $this->data = [];
43
        }
44
45
        foreach ($data as $field => $value) {
46
            $this->setField($field, $value);
47
        }
48
49
        if (isset($data['id'])) {
50
            $this->setId($data['id']);
51
        }
52
    }
53
54
    /**
55
     * @return array
56
     */
57
    public function mapToArray(): array
58
    {
59
        $result = [
60
            'id' => $this->getId()
61
        ];
62
        $fields = \array_keys(static::fields());
63
        foreach ($fields as $field) {
64
            $result[$field] = $this->getField($field);
65
        }
66
67
        return $result;
68
    }
69
70
    /**
71
     * @param $name
72
     *
73
     * @return mixed|null
74
     */
75
    public function __get($name)
76
    {
77
        return $this->getField($name);
78
    }
79
80
    /**
81
     * @param $name
82
     * @param $value
83
     */
84
    public function __set($name, $value)
85
    {
86
        $this->setField($name, $value);
87
    }
88
89
    /**
90
     * @param $name
91
     *
92
     * @return bool
93
     */
94
    public function __isset($name)
95
    {
96
        return isset($this->data[$name]);
97
    }
98
99
    /**
100
     * @param $name
101
     * @param $value
102
     */
103
    protected function setField($name, $value)
104
    {
105
        $fields = static::fields();
106
        if (isset($fields[$name])) {
107
            $this->data[$name] = cast($value, $fields[$name]);
108
        }
109
    }
110
111
    /**
112
     * @param $name
113
     * @param null $default
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $default is correct as it would always require null to be passed?
Loading history...
114
     *
115
     * @return mixed|null
116
     */
117
    protected function getField($name, $default = null)
118
    {
119
        return $this->data[$name] ?? $default;
120
    }
121
}
122