Completed
Push — master ( f731c8...255ed4 )
by Arman
14s queued 12s
created

QtModel   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 26
dl 0
loc 125
rs 10
c 3
b 0
f 0
wmc 11

7 Methods

Rating   Name   Duplication   Size   Complexity  
A prop() 0 3 1
A setOrm() 0 3 1
A __sleep() 0 6 1
A __call() 0 13 3
A __set() 0 3 1
A fillObjectProps() 0 11 3
A __get() 0 3 1
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
     * 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
     * ORM database abstract layer object
71
     * @var \Quantum\Libraries\Database\DbalInterface
72
     */
73
    private $orm;
74
75
    /**
76
     * Sets the ORM
77
     * @param \Quantum\Libraries\Database\DbalInterface $orm
78
     */
79
    public function setOrm(DbalInterface $orm)
80
    {
81
        $this->orm = $orm;
82
    }
83
84
    /**
85
     * Fills the object properties
86
     * @param array $arguments
87
     * @return \Quantum\Mvc\QtModel
88
     * @throws \Quantum\Exceptions\ModelException
89
     */
90
    public function fillObjectProps(array $arguments): QtModel
91
    {
92
        foreach ($arguments as $key => $value) {
93
            if (!in_array($key, $this->fillable)) {
94
                throw ModelException::inappropriateProperty($key);
95
            }
96
97
            $this->prop($key, $value);
98
        }
99
100
        return $this;
101
    }
102
103
    /**
104
     * Sets or gets the model property
105
     * @param string $property
106
     * @param mixed|null $value
107
     * @return mixed
108
     */
109
    public function prop(string $property, $value = null)
110
    {
111
        return $this->orm->prop($property, $value);
112
    }
113
114
    /**
115
     * Gets the model property with magic
116
     * @param string $property
117
     * @return mixed
118
     */
119
    public function __get(string $property)
120
    {
121
        return $this->prop($property);
122
    }
123
124
    /**
125
     * Sets a value to the model property with magic
126
     * @param string $property
127
     * @param mixed $value
128
     */
129
    public function __set(string $property, $value)
130
    {
131
        $this->prop($property, $value);
132
    }
133
134
    /**
135
     * Allows calling the model methods
136
     * @param string $method
137
     * @param mixed|null $args
138
     * @return $this|array|int|string
139
     * @throws \Quantum\Exceptions\ModelException
140
     */
141
    public function __call(string $method, $args = null)
142
    {
143
        if (method_exists($this->orm, $method)) {
144
145
            $result = $this->orm->{$method}(...$args);
146
147
            if (!is_object($result)) {
148
                return $result;
149
            }
150
151
            return $this;
152
        } else {
153
            throw ModelException::undefinedMethod($method);
154
        }
155
    }
156
157
    /**
158
     * Keeps only relevant props at serialization
159
     * @return string[]
160
     */
161
    public function __sleep()
162
    {
163
        return [
164
            'table',
165
            'idColumn',
166
            'foreignKeys'
167
        ];
168
    }
169
170
}
171