Passed
Push — main ( f06788...05a49e )
by BRUNO
02:20
created

ModelAbstract::getDataModelArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 2
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
     * @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
     * @return array
71
     */
72
    public function getDataModelArray():array {
73
        return $this->dataModelArray;
74
    }
75
76
    /**
77
     * @param array $params
78
     * @return void
79
     */
80
    public function fromMapToModel(array $params): void
81
    {
82
        foreach ($params as $key => $item) {
83
            $this->dataModelArray[$key] = $item;
84
            if (property_exists($this, $this->{$key})){
85
                $this->{$key} = $item;
86
            }
87
        }
88
    }
89
90
    /**
91
     * @param string $json
92
     * @return void
93
     */
94
    public function fromJsonToModel(string $json): void
95
    {
96
        $params = json_decode($json, true);
97
        $this->fromMapToModel($params);
98
    }
99
100
    /**
101
     * @param $objArray
102
     * @return array|null
103
     */
104
    public function toMap($objArray = null): ?array
105
    {
106
        $data = $objArray ?? $this;
107
        if (is_array($data) || is_object($data)) {
108
            $result = [];
109
            foreach ($data as $key => $value) {
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