Passed
Push — main ( ff7bb8...23065b )
by BRUNO
02:50
created

ModelAbstract::__isset()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 8
rs 10
1
<?php
2
3
namespace BMorais\Database;
4
5
/**
6
 * CLASS CRUD
7
 * Basic class to make connection between the database and the application
8
 *
9
 * @author Bruno Morais <[email protected]>
10
 * @copyright MIT, bmorais.com
11
 * @package bmorais\database
12
 * @subpackage class
13
 * @access private
14
 */
15
16
use ReflectionObject;
17
18
#[\AllowDynamicProperties]
19
abstract class ModelAbstract
20
{
21
    private array $dataModelArray = [];
22
23
    /**
24
     * @param array|null $params
25
     */
26
    public function __construct(array $params = null)
27
    {
28
        if (!empty($params)) {
29
            $this->fromMapToModel($params);
30
        }
31
    }
32
33
    /**
34
     * @param $name
35
     */
36
    public function __get($name)
37
    {
38
        return $this->{$name} ?? ($this->dataModelArray[$name]?? "");
39
    }
40
41
    /**
42
     * @param $key
43
     * @param $value
44
     * @return void
45
     */
46
    public function __set($key, $value): void
47
    {
48
        if (property_exists($this, $key)){
49
            $this->{$key} = $value;
50
        }
51
        $this->dataModelArray[$key] = $value;
52
    }
53
54
    /**
55
     * @param $key
56
     * @return bool
57
     */
58
    function __isset($key) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
59
60
        $result = isset($this->dataModelArray[$key]);
61
        if (property_exists($this, $key)){
62
            $result = isset($this->{$key});
63
        }
64
65
        return $result;
66
    }
67
68
    /**
69
     * @return array
70
     */
71
    public function getDataModelArray():array {
72
        return $this->dataModelArray;
73
    }
74
75
    /**
76
     * @param array $params
77
     * @return void
78
     */
79
    public function fromMapToModel(array $params): void
80
    {
81
        foreach ($params as $key => $item) {
82
            $this->dataModelArray[$key] = $item;
83
            if (property_exists($this, $key)){
84
                $this->{$key} = $item;
85
            }
86
        }
87
    }
88
89
    /**
90
     * @param string $json
91
     * @return void
92
     */
93
    public function fromJsonToModel(string $json): void
94
    {
95
        $params = json_decode($json, true);
96
        $this->fromMapToModel($params);
97
    }
98
99
    /**
100
     * @param $objArray
101
     * @return array|null
102
     */
103
    public function toMap($objArray = null): ?array
104
    {
105
        $data = $objArray ?? $this;
106
        if (is_array($data) || is_object($data)) {
107
            $result = [];
108
            foreach ($data as $key => $value) {
109
                if(strlen($value) > 0)
110
                    $result[$key] = (is_array($value) || is_object($value)) ? $this->toMap($value) : $value;
111
            }
112
113
            return $result;
114
        }
115
116
        return null;
117
    }
118
119
    public function toObject(): \stdClass{
120
        $reflection = new \ReflectionClass($this);
121
        $objeto = new \stdClass;
122
123
        foreach ($reflection->getProperties() as $prop) {
124
            $prop->setAccessible(true);
125
126
            $method = 'get' . $prop->getName();
127
128
            if (method_exists($this, $method)) {
129
                $objeto->{$prop->getName()} = call_user_func([$this, $method]);
130
            } else {
131
                $objeto->{$prop->getName()} = $prop->getValue($this);
132
            }
133
        }
134
135
        return $objeto;
136
    }
137
138
    /**
139
     * @return string
140
     */
141
    public function toJson(): string
142
    {
143
        return json_encode($this->toMap(), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
144
    }
145
146
    /**
147
     * @return string
148
     */
149
    public function toString(): string
150
    {
151
        $data = (object) $this->toMap();
152
        $re_2 = new ReflectionObject($data);
153
        $classname = get_class($this);
154
        if ($pos = strrpos($classname, '\\')) {
155
            $classname = substr($classname, $pos + 1);
156
        }
157
158
        return $classname . ' {' . implode(', ', array_map(
159
                function ($p_0) use ($data) {
160
                    $p_0->setAccessible(true);
161
162
                    return $p_0->getName() . ': ' . $p_0->getValue($data);
163
                },
164
                $re_2->getProperties()
165
            )) . '}';
166
    }
167
168
}
169
170