ModelAbstract   A
last analyzed

Complexity

Total Complexity 26

Size/Duplication

Total Lines 150
Duplicated Lines 0 %

Importance

Changes 17
Bugs 0 Features 1
Metric Value
eloc 48
c 17
b 0
f 1
dl 0
loc 150
rs 10
wmc 26

11 Methods

Rating   Name   Duplication   Size   Complexity  
A toJson() 0 3 1
A __construct() 0 4 2
A __get() 0 3 2
A __set() 0 4 2
A fromMapToModel() 0 13 3
A toArray() 0 12 3
A fromJsonToModel() 0 4 1
A toObject() 0 17 3
A toMap() 0 13 6
A toString() 0 8 1
A __isset() 0 3 2
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
    /**
22
     * @param array|null $params
23
     */
24
    public function __construct(?array $params = null)
25
    {
26
        if (!empty($params)) {
27
            $this->fromMapToModel($params);
28
        }
29
    }
30
31
    /**
32
     * @param $name
33
     */
34
    public function __get($name)
35
    {
36
        return property_exists($this, $name) ? $this->{$name} : null;
37
    }
38
39
    /**
40
     * @param $key
41
     * @param $value
42
     * @return void
43
     */
44
    public function __set($key, $value): void
45
    {
46
        if (property_exists($this, $key)) {
47
            $this->{$key} = $value;
48
        }
49
    }
50
51
    /**
52
     * @param $key
53
     * @return bool
54
     */
55
    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...
56
57
        return property_exists($this, $key) && isset($this->{$key});
58
59
    }
60
61
    /**
62
     * @param array $params
63
     * @return void
64
     */
65
    public function fromMapToModel(array $params)
66
    {
67
        $reflection = new \ReflectionClass($this);
68
69
        foreach ($params as $key => $item) {
70
            if ($reflection->hasProperty($key)) {
71
                $property = $reflection->getProperty($key);
72
                $property->setAccessible(true);
73
                $property->setValue($this, $item);
74
            }
75
        }
76
77
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type BMorais\Database\ModelAbstract which is incompatible with the documented return type void.
Loading history...
78
    }
79
80
    /**
81
     * @param string $json
82
     * @return void
83
     */
84
    public function fromJsonToModel(string $json)
85
    {
86
        $params = json_decode($json, true, flags: JSON_THROW_ON_ERROR);
87
        return $this->fromMapToModel($params);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->fromMapToModel($params) targeting BMorais\Database\ModelAbstract::fromMapToModel() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
88
89
    }
90
91
    /**
92
     * @param $objArray
93
     * @return array|null
94
     */
95
    public function toMap($objArray = null): ?array
96
    {
97
        $data = $objArray ?? $this;
98
        if (is_array($data) || is_object($data)) {
99
            $result = [];
100
            foreach ($data as $key => $value) {
101
                $result[$key] = (is_array($value) || is_object($value)) ? $this->toMap($value) : $value;
102
            }
103
104
            return $result;
105
        }
106
107
        return null;
108
    }
109
110
    /**
111
     * @return \stdClass
112
     */
113
    public function toObject(): \stdClass{
114
        $reflection = new \ReflectionClass($this);
115
        $objeto = new \stdClass;
116
117
        foreach ($reflection->getProperties() as $prop) {
118
            $prop->setAccessible(true);
119
120
            $method = 'get' . $prop->getName();
121
122
            if (method_exists($this, $method)) {
123
                $objeto->{$prop->getName()} = call_user_func([$this, $method]);
124
            } else {
125
                $objeto->{$prop->getName()} = $prop->getValue($this);
126
            }
127
        }
128
129
        return $objeto;
130
    }
131
132
    /**
133
     * @return array
134
     */
135
    public function toArray(): array
136
    {
137
        $array = [];
138
        foreach ((new \ReflectionClass($this))->getProperties() as $prop) {
139
            $prop->setAccessible(true);
140
            $method = 'get' . ucfirst($prop->getName());
141
142
            $array[$prop->getName()] = method_exists($this, $method)
143
                ? $this->{$method}()
144
                : $prop->getValue($this);
145
        }
146
        return $array;
147
    }
148
149
    /**
150
     * @return string
151
     */
152
    public function toJson(): string
153
    {
154
        return json_encode($this->toArray(), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR);
155
    }
156
157
    /**
158
     * @return string
159
     */
160
    public function toString(): string
161
    {
162
        $classname = (new \ReflectionClass($this))->getShortName();
163
        $properties = array_map(
164
            fn($prop) => "{$prop->getName()}: '" . ($prop->getValue($this) ?? '') . "'",
165
            (new \ReflectionClass($this))->getProperties()
166
        );
167
        return "$classname {" . implode(', ', $properties) . '}';
168
    }
169
170
}