Passed
Push — main ( c4aba7...f06788 )
by BRUNO
02:25
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
    protected 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
     * @return string
36
     */
37
    public function __get($name): string
38
    {
39
        return $this->{$name} ?? ($this->dataModelArray[$name]?? "");
40
    }
41
42
    /**
43
     * @param $name
44
     * @param $value
45
     * @return void
46
     */
47
    public function __set($name, $value): void
48
    {
49
        if (property_exists($this, $this->{$key})){
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $key seems to be never defined.
Loading history...
50
            $this->{$key} = $value;
51
        }
52
        $this->dataModelArray[$name] = $value;
53
    }
54
55
    /**
56
     * @param $isset
57
     * @return bool
58
     */
59
    function __isset($isset) {
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...
60
61
        $result = isset($this->dataModelArray[$isset]);
62
        if (property_exists($this, $this->{$key})){
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $key seems to be never defined.
Loading history...
63
            $result = isset($this->{$isset});
64
        }
65
66
        return $result;
67
    }
68
69
    /**
70
     * @param array $params
71
     * @return void
72
     */
73
    public function fromMapToModel(array $params): void
74
    {
75
        foreach ($params as $key => $item) {
76
            $this->dataModelArray[$key] = $item;
77
            if (property_exists($this, $this->{$key})){
78
                $this->{$key} = $item;
79
            }
80
        }
81
    }
82
83
    /**
84
     * @param string $json
85
     * @return void
86
     */
87
    public function fromJsonToModel(string $json): void
88
    {
89
        $params = json_decode($json, true);
90
        $this->fromMapToModel($params);
91
    }
92
93
    /**
94
     * @param $objArray
95
     * @return array|null
96
     */
97
    public function toMap($objArray = null): ?array
98
    {
99
        $data = $objArray ?? $this;
100
        if (is_array($data) || is_object($data)) {
101
            $result = [];
102
            foreach ($data as $key => $value) {
103
                $result[$key] = (is_array($value) || is_object($value)) ? $this->toMap($value) : $value;
104
            }
105
106
            return $result;
107
        }
108
109
        return null;
110
    }
111
112
    /**
113
     * @return string
114
     */
115
    public function toJson(): string
116
    {
117
        return json_encode($this->toMap(), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
118
    }
119
120
    /**
121
     * @return string
122
     */
123
    public function toString(): string
124
    {
125
        $data = (object) $this->toMap();
126
        $re_2 = new ReflectionObject($data);
127
        $classname = get_class($this);
128
        if ($pos = strrpos($classname, '\\')) {
129
            $classname = substr($classname, $pos + 1);
130
        }
131
132
        return $classname . ' {' . implode(', ', array_map(
133
                function ($p_0) use ($data) {
134
                    $p_0->setAccessible(true);
135
136
                    return $p_0->getName() . ': ' . $p_0->getValue($data);
137
                },
138
                $re_2->getProperties()
139
            )) . '}';
140
    }
141
142
}
143
144