NOSQLModelTrait::fromArray()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 6
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 1
1
<?php
2
namespace NOSQL\Models\base;
3
4
use MongoDB\BSON\ObjectId;
5
use MongoDB\BSON\UTCDateTime;
6
use MongoDB\Database;
7
use NOSQL\Dto\Model\NOSQLModelDto;
8
9
/**
10
 * Trait NOSQLModelTrait
11
 * @package NOSQL\Models\base
12
 */
13
trait NOSQLModelTrait {
14
    use NOSQLStatusTrait;
15
    /**
16
     * @var NOSQLModelDto
17
     */
18
    protected $dto;
19
20
    /**
21
     * @param string $name
22
     * @return mixed|null
23
     */
24
    public function __get($name)
25
    {
26
        $value = null;
27
        if(null !== $this->dto && property_exists($this->dto, $name)) {
28
            $value = $this->dto->$name;
29
        }
30
        return $value;
31
    }
32
33
    /**
34
     * @param string $name
35
     * @param mixed $value
36
     */
37
    public function __set($name, $value)
38
    {
39
        if(null !== $this->dto && property_exists($this->dto, $name)) {
40
            $this->dto->$name = $value;
41
            $this->addChanges($name);
42
        }
43
        $this->setIsModified(true);
44
    }
45
46
    public function __call($name, $arguments)
47
    {
48
        if(preg_match('/^(set|get)/', $name)) {
49
            $property = lcfirst(str_replace(['set', 'Set', 'get', 'Get'], '', $name));
50
            if(false !== stripos($name, 'set')) {
51
                $this->dto->$property = $arguments[0];
52
            } else {
53
                return $this->dto->$property;
54
            }
55
56
        }
57
    }
58
59
    /**
60
     * @param array $data
61
     * @param bool $withName
62
     * @throws \NOSQL\Exceptions\NOSQLValidationException
63
     */
64
    public function feed(array $data, $withName = false) {
65
        $name = '';
66
        $sep = '';
67
        foreach($data as $key => $value) {
68
            if($value instanceof ObjectId) {
69
                $this->dto->setPk($value->jsonSerialize()['$oid']);
70
                $name = $this->dto->getPk();
71
            } elseif($key === '_last_update') {
72
                $this->dto->setLastUpdate($value instanceof UTCDateTime ? $value : null);
0 ignored issues
show
Bug introduced by
It seems like $value instanceof MongoD...ateTime ? $value : null can also be of type MongoDB\BSON\UTCDateTime; however, parameter $last_update of NOSQL\Dto\Model\NOSQLModelDto::setLastUpdate() does only seem to accept DateTime|null, maybe add an additional type check? ( Ignorable by Annotation )

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

72
                $this->dto->setLastUpdate(/** @scrutinizer ignore-type */ $value instanceof UTCDateTime ? $value : null);
Loading history...
73
            } else {
74
                if (is_object($value)) {
75
                    switch (get_class($value)) {
76
                        case UTCDateTime::class:
77
                            $value = $value->toDateTime()->format('Y-m-d H:i:s');
78
                            break;
79
                    }
80
                }
81
                $this->$key = $value;
82
                if(in_array(strtolower($key), ['name', 'label', 'title', 'method'])) {
83
                    $name .= $sep . $value;
84
                    $sep = ' ';
85
                }
86
            }
87
        }
88
        if($withName) {
89
            $this->dto->setName($name);
90
        }
91
    }
92
93
    /**
94
     * @param array $data
95
     * @return NOSQLModelTrait
96
     * @throws \NOSQL\Exceptions\NOSQLValidationException
97
     */
98
    public static function fromArray(array $data) {
99
        $modelName = get_called_class();
100
        /** @var NOSQLModelTrait $model */
101
        $model = new $modelName();
102
        $model->feed($data);
103
        return $model;
104
    }
105
106
    /**
107
     * Before insert hook
108
     * @param Database $con
109
     */
110
    protected function preInsert(Database $con = null) {}
0 ignored issues
show
Unused Code introduced by
The parameter $con is not used and could be removed. ( Ignorable by Annotation )

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

110
    protected function preInsert(/** @scrutinizer ignore-unused */ Database $con = null) {}

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
111
112
    /**
113
     * Before update hook
114
     * @param Database $con
115
     */
116
    protected function preUpdate(Database $con = null) {}
0 ignored issues
show
Unused Code introduced by
The parameter $con is not used and could be removed. ( Ignorable by Annotation )

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

116
    protected function preUpdate(/** @scrutinizer ignore-unused */ Database $con = null) {}

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
117
118
    /**
119
     * Before save hook
120
     * @param Database $con
121
     */
122
    protected function preSave(Database $con = null) {}
0 ignored issues
show
Unused Code introduced by
The parameter $con is not used and could be removed. ( Ignorable by Annotation )

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

122
    protected function preSave(/** @scrutinizer ignore-unused */ Database $con = null) {}

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
123
124
    /**
125
     * Before delete hook
126
     * @param Database $con
127
     */
128
    protected function preDelete(Database $con = null) {}
0 ignored issues
show
Unused Code introduced by
The parameter $con is not used and could be removed. ( Ignorable by Annotation )

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

128
    protected function preDelete(/** @scrutinizer ignore-unused */ Database $con = null) {}

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
129
130
    /**
131
     * After insert hook
132
     * @param Database $con
133
     */
134
    protected function postInsert(Database $con = null) {}
0 ignored issues
show
Unused Code introduced by
The parameter $con is not used and could be removed. ( Ignorable by Annotation )

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

134
    protected function postInsert(/** @scrutinizer ignore-unused */ Database $con = null) {}

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
135
136
    /**
137
     * After update hook
138
     * @param Database $con
139
     */
140
    protected function postUpdate(Database $con = null) {}
0 ignored issues
show
Unused Code introduced by
The parameter $con is not used and could be removed. ( Ignorable by Annotation )

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

140
    protected function postUpdate(/** @scrutinizer ignore-unused */ Database $con = null) {}

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
141
142
    /**
143
     * After save hook
144
     * @param Database $con
145
     */
146
    protected function postSave(Database $con = null) {}
0 ignored issues
show
Unused Code introduced by
The parameter $con is not used and could be removed. ( Ignorable by Annotation )

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

146
    protected function postSave(/** @scrutinizer ignore-unused */ Database $con = null) {}

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
147
148
    /**
149
     * After delete hook
150
     * @param Database $con
151
     */
152
    protected function postDelete(Database $con = null) {}
0 ignored issues
show
Unused Code introduced by
The parameter $con is not used and could be removed. ( Ignorable by Annotation )

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

152
    protected function postDelete(/** @scrutinizer ignore-unused */ Database $con = null) {}

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
153
}
154