Passed
Push — main ( 984178...6cf14e )
by BRUNO
53s queued 16s
created

ModelAbstract::fromJsonToModel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 1
b 0
f 0
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
    /**
120
     * @return string
121
     */
122
    public function toJson(): string
123
    {
124
        return json_encode($this->toMap(), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
125
    }
126
127
    /**
128
     * @return string
129
     */
130
    public function toString(): string
131
    {
132
        $data = (object) $this->toMap();
133
        $re_2 = new ReflectionObject($data);
134
        $classname = get_class($this);
135
        if ($pos = strrpos($classname, '\\')) {
136
            $classname = substr($classname, $pos + 1);
137
        }
138
139
        return $classname . ' {' . implode(', ', array_map(
140
                function ($p_0) use ($data) {
141
                    $p_0->setAccessible(true);
142
143
                    return $p_0->getName() . ': ' . $p_0->getValue($data);
144
                },
145
                $re_2->getProperties()
146
            )) . '}';
147
    }
148
149
}
150
151