Passed
Pull Request — master (#125)
by
unknown
02:54
created

QtModel::setOrm()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 2
b 0
f 0
1
<?php
2
3
/**
4
 * Quantum PHP Framework
5
 *
6
 * An open source software development framework for PHP
7
 *
8
 * @package Quantum
9
 * @author Arman Ag. <[email protected]>
10
 * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
11
 * @link http://quantum.softberg.org/
12
 * @since 2.6.0
13
 */
14
15
namespace Quantum\Mvc;
16
17
use Quantum\Libraries\Database\DbalInterface;
18
use Quantum\Exceptions\ModelException;
19
20
/**
21
 * Class QtModel
22
 * @package Quantum\Mvc
23
 * @method string getTable()
24
 * @method DbalInterface select(...$columns)
25
 * @method DbalInterface findOne(int $id)
26
 * @method DbalInterface findOneBy(string $column, $value)
27
 * @method DbalInterface first()
28
 * @method DbalInterface criteria(string $column, string $operator, $value = null)
29
 * @method DbalInterface criterias(...$criterias)
30
 * @method DbalInterface having(string $column, string $operator, string $value = null)
31
 * @method DbalInterface orderBy(string $column, string $direction)
32
 * @method DbalInterface limit(int $limit)
33
 * @method mixed get()
34
 * @method array asArray()
35
 * @method DbalInterface create()
36
 * @method bool save()
37
 * @method bool delete()
38
 * @method bool deleteMany()
39
 * @method DbalInterface joinTo(QtModel $model, bool $switch = true)
40
 * @method DbalInterface joinThrough(QtModel $model, bool $switch = true)
41
 */
42
abstract class QtModel
43
{
44
45
    /**
46
     * Type array
47
     */
48
    const TYPE_ARRAY = 1;
49
50
    /**
51
     * The database table associated with model
52
     * @var string
53
     */
54
    public $table;
55
56
    /**
57
     * Id column of table
58
     * @var string
59
     */
60
    public $idColumn = 'id';
61
62
    /**
63
     * Foreign keys
64
     * @var array
65
     */
66
    public $foreignKeys = [];
67
68
    /**
69
     * Models fillable properties
70
     * @var array
71
     */
72
    protected $fillable = [];
73
74
    /**
75
     * Models hidden properties
76
     * @var array
77
     */
78
    protected $hidden = [];
79
80
    /**
81
     * ORM database abstract layer object
82
     * @var \Quantum\Libraries\Database\DbalInterface
83
     */
84
    private $orm;
85
86
    /**
87
     * Sets the ORM
88
     * @param \Quantum\Libraries\Database\DbalInterface $orm
89
     */
90
    public function setOrm(DbalInterface $orm)
91
    {
92
        $this->orm = $orm;
93
    }
94
95
    /**
96
     * Fills the object properties
97
     * @param array $props
98
     * @return \Quantum\Mvc\QtModel
99
     * @throws \Quantum\Exceptions\ModelException
100
     */
101
    public function fillObjectProps(array $props): QtModel
102
    {
103
        foreach ($props as $key => $value) {
104
            if ($key == $this->idColumn) {
105
                continue;
106
            }
107
108
            if (!in_array($key, $this->fillable)) {
109
                throw ModelException::inappropriateProperty($key);
110
            }
111
112
            $this->prop($key, $value);
113
        }
114
115
        return $this;
116
    }
117
118
    /**
119
     * @inheritDoc
120
     * @throws DatabaseException
121
     */
122
    public function get(?int $returnType = self::TYPE_ARRAY)
123
    {
124
        $result = $this->orm->get($returnType);
0 ignored issues
show
Unused Code introduced by
The call to Quantum\Libraries\Database\DbalInterface::get() has too many arguments starting with $returnType. ( Ignorable by Annotation )

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

124
        /** @scrutinizer ignore-call */ 
125
        $result = $this->orm->get($returnType);

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...
125
126
        if (count($this->hidden) > 0) {
127
            for ($i = 0; $i < count($result); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
128
                $result[$i] = array_diff_key($result[$i], array_flip($this->hidden));
129
            }
130
        }
131
132
        return $result;
133
    }
134
135
    /**
136
     * Sets or gets the model property
137
     * @param string $property
138
     * @param mixed|null $value
139
     * @return mixed
140
     */
141
    public function prop(string $property, $value = null)
142
    {
143
        return $this->orm->prop(...func_get_args());
0 ignored issues
show
Bug introduced by
func_get_args() is expanded, but the parameter $key of Quantum\Libraries\Database\DbalInterface::prop() does not expect variable arguments. ( Ignorable by Annotation )

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

143
        return $this->orm->prop(/** @scrutinizer ignore-type */ ...func_get_args());
Loading history...
144
    }
145
146
    /**
147
     * Gets the model property with magic
148
     * @param string $property
149
     * @return mixed
150
     */
151
    public function __get(string $property)
152
    {
153
        return $this->prop($property);
154
    }
155
156
    /**
157
     * Sets a value to the model property with magic
158
     * @param string $property
159
     * @param mixed $value
160
     */
161
    public function __set(string $property, $value)
162
    {
163
        $this->prop($property, $value);
164
    }
165
166
    /**
167
     * Allows calling the model methods
168
     * @param string $method
169
     * @param mixed|null $args
170
     * @return $this|array|int|string
171
     * @throws \Quantum\Exceptions\ModelException
172
     */
173
    public function __call(string $method, $args = null)
174
    {
175
        if (!method_exists($this->orm, $method)) {
176
            throw ModelException::undefinedMethod($method);
177
        }
178
179
        $result = $this->orm->{$method}(...$args);
180
181
        if (!is_object($result)) {
182
            return $result;
183
        }
184
185
        return $this;
186
    }
187
188
    /**
189
     * Keeps only relevant props at serialization
190
     * @return string[]
191
     */
192
    public function __sleep()
193
    {
194
        return [
195
            'table',
196
            'idColumn',
197
            'foreignKeys'
198
        ];
199
    }
200
}
201