Completed
Push — master ( be8bdb...7218e6 )
by Maxim
03:21
created

AbstractEntity::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
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->set($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->get($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->get($name);
78
    }
79
80
    /**
81
     * @param $name
82
     * @param $value
83
     */
84
    public function __set($name, $value)
85
    {
86
        $this->set($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 $field
101
     * @param $value
102
     */
103
    public function set($field, $value)
104
    {
105
        $fields = static::fields();
106
        if (isset($fields[$field])) {
107
            $this->data[$field] = cast($value, $fields[$field]);
108
        }
109
    }
110
111
    /**
112
     * @param $field
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
    public function get($field, $default = null)
118
    {
119
        return $this->data[$field] ?? $default;
120
    }
121
}
122