Passed
Push — main ( ed5796...0cf401 )
by BRUNO
01:49
created

ModelAbstract::copyWith()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
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 3
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->modelFromMap($params);
15
    }
16
17
    /**
18
     * @param array $params
19
     * @return void
20
     */
21
    public function modelFromMap(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 modelFromJson(string $json): void
34
    {
35
        $params = json_decode($json, true);
36
        foreach ($params as $key => $item)
37
        {
38
            $this->{$key} = $item;
39
        }
40
    }
41
42
    /**
43
     * @return array|null
44
     */
45
    public function toMap(): ?array
46
    {
47
        $data = $this;
48
        if (is_array($data) || is_object($data))
49
        {
50
            $result = [];
51
            foreach ($data as $key => $value)
52
            {
53
                $result[$key] = (is_array($value) || is_object($value)) ? object_to_array($value) : $value;
0 ignored issues
show
Bug introduced by
The function object_to_array was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

53
                $result[$key] = (is_array($value) || is_object($value)) ? /** @scrutinizer ignore-call */ object_to_array($value) : $value;
Loading history...
54
            }
55
            return $result;
56
        }
57
        return null;
58
    }
59
60
    /**
61
     * @return string
62
     */
63
    public function toJson():string
64
    {
65
        return json_encode($this->toMap(), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
66
    }
67
68
    /**
69
     * @return string
70
     */
71
    public function toString():string
72
    {
73
        $data = (object) $this->toMap();
74
        $re_2 = new ReflectionObject($data);
75
        $classname = get_class($this);
76
        if ($pos = strrpos($classname, '\\')) $classname = substr($classname, $pos + 1);
77
        return $classname .' {' . implode(', ', array_map(
78
                function($p_0) use ($data)
79
                {
80
                    $p_0->setAccessible(true);
81
                    return $p_0->getName() .': '. $p_0->getValue($data);
82
                }, $re_2->getProperties())) .'}';
83
84
    }
85
86
    /**
87
     * @return $this
88
     */
89
    public function copyWith():self
90
    {
91
        return $this;
92
    }
93
}