Passed
Pull Request — master (#125)
by
unknown
03:15
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.9.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
     * The database table associated with model
47
     * @var string
48
     */
49
    public $table;
50
51
    /**
52
     * Id column of table
53
     * @var string
54
     */
55
    public $idColumn = 'id';
56
57
    /**
58
     * Foreign keys
59
     * @var array
60
     */
61
    public $foreignKeys = [];
62
63
    /**
64
     * Models fillable properties
65
     * @var array
66
     */
67
    protected $fillable = [];
68
69
    /**
70
     * Models hidden properties
71
     * @var array
72
     */
73
    protected $hidden = [];
74
75
    /**
76
     * ORM database abstract layer object
77
     * @var \Quantum\Libraries\Database\DbalInterface
78
     */
79
    private $orm;
80
81
    /**
82
     * Sets the ORM
83
     * @param \Quantum\Libraries\Database\DbalInterface $orm
84
     */
85
    public function setOrm(DbalInterface $orm)
86
    {
87
        $this->orm = $orm;
88
    }
89
90
    /**
91
     * Fills the object properties
92
     * @param array $props
93
     * @return \Quantum\Mvc\QtModel
94
     * @throws \Quantum\Exceptions\ModelException
95
     */
96
    public function fillObjectProps(array $props): QtModel
97
    {
98
        foreach ($props as $key => $value) {
99
            if ($key == $this->idColumn) {
100
                continue;
101
            }
102
103
            if (!in_array($key, $this->fillable)) {
104
                throw ModelException::inappropriateProperty($key);
105
            }
106
107
            $this->prop($key, $value);
108
        }
109
110
        return $this;
111
    }
112
113
    /**
114
     * @inheritDoc
115
     * @throws DatabaseException
116
     */
117
    public function get(?int $returnType = DbalInterface::TYPE_ARRAY)
118
    {
119
        $result = $this->orm->get($returnType);
120
121
        $resultCount = count($result);
122
123
        if (count($this->hidden) > 0) {
124
            for ($i = 0; $i < $resultCount; $i++) {
125
                $result[$i] = array_diff_key($result[$i], array_flip($this->hidden));
126
            }
127
        }
128
129
        return $result;
130
    }
131
132
    /**
133
     * Sets or gets the model property
134
     * @param string $property
135
     * @param mixed|null $value
136
     * @return mixed
137
     */
138
    public function prop(string $property, $value = null)
139
    {
140
        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

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