Passed
Push — main ( 9a2714...604d6f )
by
unknown
02:00
created

ModelAbstract::__set()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 2
dl 0
loc 2
rs 10
1
<?php
2
3
namespace BMorais\Database;
4
5
use ReflectionObject;
6
7
abstract class ModelAbstract
8
{
9
    /**
10
     * @param array|null $params
11
     */
12
    public function __construct(array $params = null){
13
        if (!empty($params))
14
            $this->fromMapToModel($params);
15
    }
16
17
    /**
18
     * @param array $params
19
     * @return void
20
     */
21
    public function fromMapToModel(array $params): void
22
    {
23
        foreach ($params as $key => $item)
24
        {
25
            $this->{$key} = $item;
26
        }
27
    }
28
29
    /**
30
     * @param string $json
31
     * @return void
32
     */
33
    public function fromJsonToModel(string $json): void
34
    {
35
        $params = json_decode($json, true);
36
        $this->fromMapToModel($params);
37
    }
38
39
    /**
40
     * @return array|null
41
     */
42
    public function toMap(): ?array
43
    {
44
        $data = $this;
45
        if (is_array($data) || is_object($data))
46
        {
47
            $result = [];
48
            foreach ($data as $key => $value)
49
            {
50
                $result[$key] = (is_array($value) || is_object($value)) ? $this->toMap($value) : $value;
0 ignored issues
show
Unused Code introduced by
The call to BMorais\Database\ModelAbstract::toMap() has too many arguments starting with $value. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

50
                $result[$key] = (is_array($value) || is_object($value)) ? $this->/** @scrutinizer ignore-call */ toMap($value) : $value;

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
51
            }
52
            return $result;
53
        }
54
        return null;
55
    }
56
57
    /**
58
     * @return string
59
     */
60
    public function toJson():string
61
    {
62
        return json_encode($this->toMap(), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
63
    }
64
65
    /**
66
     * @return string
67
     */
68
    public function toString():string
69
    {
70
        $data = (object) $this->toMap();
71
        $re_2 = new ReflectionObject($data);
72
        $classname = get_class($this);
73
        if ($pos = strrpos($classname, '\\')) $classname = substr($classname, $pos + 1);
74
        return $classname .' {' . implode(', ', array_map(
75
                function($p_0) use ($data)
76
                {
77
                    $p_0->setAccessible(true);
78
                    return $p_0->getName() .': '. $p_0->getValue($data);
79
                }, $re_2->getProperties())) .'}';
80
81
    }
82
83
    /**
84
     * @param 
85
     * @return string|null 
86
     */
0 ignored issues
show
Documentation Bug introduced by
The doc comment @return at position 0 could not be parsed: Unknown type name '@return' at position 0 in @return.
Loading history...
87
    public function __get($attribute) {
88
        return $this->{$attribute} ; 
89
    }
90
91
    
92
    public function __set($attribute, $value): void {
93
        $this->{$attribute} = $value ; 
94
    }
95
}