|
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 3.0.0 |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Quantum\Model; |
|
16
|
|
|
|
|
17
|
|
|
use Quantum\Model\Exceptions\ModelException; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Class Model |
|
21
|
|
|
* @package Quantum\Model |
|
22
|
|
|
*/ |
|
23
|
|
|
abstract class Model |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* Internal attributes |
|
27
|
|
|
* @var array |
|
28
|
|
|
*/ |
|
29
|
|
|
protected array $attributes = []; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Models fillable properties |
|
33
|
|
|
* @var array |
|
34
|
|
|
*/ |
|
35
|
|
|
protected array $fillable = []; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Models hidden properties |
|
39
|
|
|
* Used by DBAL and plain models |
|
40
|
|
|
* @var array |
|
41
|
|
|
*/ |
|
42
|
|
|
public array $hidden = []; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param string $key |
|
46
|
|
|
* @param $value |
|
47
|
|
|
* @return $this|mixed|null |
|
48
|
|
|
*/ |
|
49
|
|
|
public function prop(string $key, $value = null) |
|
50
|
|
|
{ |
|
51
|
|
|
if (func_num_args() === 1) { |
|
52
|
|
|
return $this->attributes[$key] ?? null; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
$this->attributes[$key] = $value; |
|
56
|
|
|
return $this; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Fill object properties |
|
61
|
|
|
* @param array $props |
|
62
|
|
|
* @return $this |
|
63
|
|
|
* @throws ModelException |
|
64
|
|
|
*/ |
|
65
|
|
|
public function fill(array $props): self |
|
66
|
|
|
{ |
|
67
|
|
|
foreach ($props as $key => $value) { |
|
68
|
|
|
if (!$this->shouldFill($key)) { |
|
69
|
|
|
throw ModelException::inappropriateProperty($key); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
$this->attributes[$key] = $value; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return $this; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Converts to array |
|
80
|
|
|
* @return array |
|
81
|
|
|
*/ |
|
82
|
|
|
public function asArray(): array |
|
83
|
|
|
{ |
|
84
|
|
|
if (!$this->hidden) { |
|
|
|
|
|
|
85
|
|
|
return $this->attributes; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return array_diff_key( |
|
89
|
|
|
$this->attributes, |
|
90
|
|
|
array_flip($this->hidden) |
|
91
|
|
|
); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Checks if model is empty |
|
96
|
|
|
* @return bool |
|
97
|
|
|
*/ |
|
98
|
|
|
public function isEmpty(): bool |
|
99
|
|
|
{ |
|
100
|
|
|
return empty($this->asArray()); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @param string $key |
|
105
|
|
|
* @return $this|mixed|null |
|
106
|
|
|
*/ |
|
107
|
|
|
public function __get(string $key) |
|
108
|
|
|
{ |
|
109
|
|
|
return $this->prop($key); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @param string $key |
|
114
|
|
|
* @param $value |
|
115
|
|
|
* @return void |
|
116
|
|
|
*/ |
|
117
|
|
|
public function __set(string $key, $value): void |
|
118
|
|
|
{ |
|
119
|
|
|
$this->prop($key, $value); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @param string $key |
|
124
|
|
|
* @return bool |
|
125
|
|
|
*/ |
|
126
|
|
|
public function __isset(string $key): bool |
|
127
|
|
|
{ |
|
128
|
|
|
return isset($this->attributes[$key]); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @param string $key |
|
133
|
|
|
* @return void |
|
134
|
|
|
*/ |
|
135
|
|
|
public function __unset(string $key): void |
|
136
|
|
|
{ |
|
137
|
|
|
unset($this->attributes[$key]); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @param string $key |
|
142
|
|
|
* @return bool |
|
143
|
|
|
*/ |
|
144
|
|
|
protected function shouldFill(string $key): bool |
|
145
|
|
|
{ |
|
146
|
|
|
return in_array($key, $this->fillable, true); |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.