|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file contains the skeleton for all of the database objects |
|
4
|
|
|
* |
|
5
|
|
|
* @package BZiON\Models |
|
6
|
|
|
* @license https://github.com/allejo/bzion/blob/master/LICENSE.md GNU General Public License Version 3 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* A database object (e.g. A player or a team) |
|
11
|
|
|
* @package BZiON\Models |
|
12
|
|
|
*/ |
|
13
|
|
|
abstract class Model extends CachedModel |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* Generates a string with the object's type and ID |
|
17
|
|
|
*/ |
|
18
|
2 |
|
public function __toString() |
|
19
|
|
|
{ |
|
20
|
2 |
|
return get_class($this) . " #" . $this->getId(); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Find if the model is in the trash can (or doesn't exist) |
|
25
|
|
|
* |
|
26
|
|
|
* @return bool True if the model has been deleted |
|
27
|
|
|
*/ |
|
28
|
1 |
|
public function isDeleted() |
|
29
|
|
|
{ |
|
30
|
1 |
|
if (!$this->isValid() || $this->getStatus() == 'deleted') { |
|
31
|
1 |
|
return true; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
1 |
|
return false; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Find if the model is active (i.e. visible to everyone) |
|
39
|
|
|
* |
|
40
|
|
|
* @return bool |
|
41
|
|
|
*/ |
|
42
|
1 |
|
public function isActive() |
|
43
|
|
|
{ |
|
44
|
1 |
|
return in_array($this->getStatus(), $this->getActiveStatuses()); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Get the models's status |
|
49
|
|
|
* |
|
50
|
|
|
* @return string |
|
51
|
|
|
*/ |
|
52
|
1 |
|
public function getStatus() |
|
53
|
|
|
{ |
|
54
|
1 |
|
if (!isset($this->status)) { |
|
55
|
|
|
return 'active'; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
1 |
|
return $this->status; |
|
|
|
|
|
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Find if two objects represent the same model |
|
63
|
|
|
* |
|
64
|
|
|
* @param object $model The model to compare |
|
65
|
|
|
* @return bool |
|
66
|
|
|
*/ |
|
67
|
11 |
|
public function isSameAs($model) |
|
68
|
|
|
{ |
|
69
|
11 |
|
if (!$model instanceof Model) { |
|
70
|
1 |
|
return false; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
11 |
|
if (!$this->valid || !$model->valid) { |
|
74
|
1 |
|
return false; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
11 |
|
$sameType = $this instanceof $model || $model instanceof $this; |
|
78
|
|
|
|
|
79
|
11 |
|
return $sameType && $this->id === $model->id; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Get the possible statuses representing an active model (visible to everyone) |
|
84
|
|
|
* |
|
85
|
|
|
* @return string[] |
|
86
|
|
|
*/ |
|
87
|
|
|
public static function getActiveStatuses() |
|
88
|
|
|
{ |
|
89
|
|
|
return array('active'); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Converts an array of IDs to an array of Models |
|
94
|
|
|
* @param int[] $idArray The list of IDs |
|
95
|
|
|
* @return static[] An array of models |
|
96
|
|
|
*/ |
|
97
|
70 |
|
public static function arrayIdToModel($idArray) |
|
98
|
|
|
{ |
|
99
|
70 |
|
$return = array(); |
|
100
|
70 |
|
foreach ($idArray as $id) { |
|
101
|
70 |
|
$return[] = static::get($id); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
70 |
|
return $return; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Converts an array of Models to an array of IDs |
|
109
|
|
|
* |
|
110
|
|
|
* All model type information is lost |
|
111
|
|
|
* |
|
112
|
|
|
* @param ModelInterface[] $modelArray The list of models |
|
113
|
|
|
* @return int[] An array of IDs |
|
114
|
|
|
*/ |
|
115
|
|
|
public static function mapToIDs($modelArray) |
|
116
|
|
|
{ |
|
117
|
|
|
return array_map(function (ModelInterface $model) { |
|
118
|
|
|
return $model->getId(); |
|
119
|
|
|
}, $modelArray); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Update a property and the corresponding database column |
|
124
|
|
|
* |
|
125
|
|
|
* @param mixed $property The protected class property to update |
|
126
|
|
|
* @param string $dbColumn The name of the database column to update |
|
127
|
|
|
* @param mixed $value The value to insert |
|
128
|
|
|
* @param string $type The mysqli type of the value (s, i, d, b) |
|
129
|
|
|
* @return self Returns the model itself to allow method chaining |
|
130
|
|
|
*/ |
|
131
|
70 |
|
protected function updateProperty(&$property, $dbColumn, $value, $type = 'i') |
|
|
|
|
|
|
132
|
|
|
{ |
|
133
|
|
|
// Don't waste time with mysql if there aren't any changes |
|
134
|
70 |
|
if ($property !== $value) { |
|
135
|
70 |
|
$property = $value; |
|
136
|
|
|
|
|
137
|
70 |
|
if ($value instanceof TimeDate) { |
|
138
|
1 |
|
$value = $value->toMysql(); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
70 |
|
$this->update($dbColumn, $value); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
70 |
|
return $this; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Gets the type of the model |
|
149
|
|
|
* @return string The type of the model, e.g. "server" |
|
150
|
|
|
*/ |
|
151
|
1 |
|
public static function getType() |
|
152
|
|
|
{ |
|
153
|
1 |
|
return self::toSnakeCase(get_called_class()); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Gets a human-readable format of the model's type |
|
158
|
|
|
* @return string |
|
159
|
|
|
*/ |
|
160
|
|
|
public static function getTypeForHumans() |
|
161
|
|
|
{ |
|
162
|
|
|
return self::getType(); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Change a parameter if a model is not valid |
|
167
|
|
|
* |
|
168
|
|
|
* Useful for form validation |
|
169
|
|
|
* |
|
170
|
|
|
* @param string $property The name of the property to change |
|
171
|
|
|
* @param mixed $value The value of the property |
|
172
|
|
|
* @return self |
|
173
|
|
|
*/ |
|
174
|
1 |
|
protected function inject($property, $value) |
|
175
|
|
|
{ |
|
176
|
1 |
|
if (!$this->isValid()) { |
|
177
|
1 |
|
$this->{$property} = $value; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
1 |
|
return $this; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Takes a CamelCase string and converts it to a snake_case one |
|
185
|
|
|
* @param string $input The string to convert |
|
186
|
|
|
* @return string |
|
187
|
|
|
*/ |
|
188
|
1 |
|
private static function toSnakeCase($input) |
|
189
|
|
|
{ |
|
190
|
1 |
|
preg_match_all('!([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)!', $input, $matches); |
|
191
|
1 |
|
$ret = $matches[0]; |
|
192
|
|
|
|
|
193
|
1 |
|
foreach ($ret as &$match) { |
|
194
|
1 |
|
$match = ($match == strtoupper($match)) ? strtolower($match) : lcfirst($match); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
1 |
|
return implode('_', $ret); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* Escape special HTML characters from a string |
|
202
|
|
|
* @param string $string |
|
203
|
|
|
* @return string |
|
204
|
|
|
*/ |
|
205
|
2 |
|
public static function escape($string) |
|
206
|
|
|
{ |
|
207
|
2 |
|
return htmlspecialchars($string, ENT_QUOTES, 'UTF-8'); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* Create model objects, given their MySQL entries |
|
212
|
|
|
* |
|
213
|
|
|
* @param array $results The MySQL rows of the model |
|
214
|
|
|
* @return static[] |
|
215
|
|
|
*/ |
|
216
|
24 |
|
public static function createFromDatabaseResults(&$results) |
|
217
|
|
|
{ |
|
218
|
24 |
|
$models = array(); |
|
219
|
|
|
|
|
220
|
24 |
|
foreach ($results as $result) { |
|
221
|
23 |
|
$model = new static($result['id'], $result); |
|
222
|
23 |
|
$model->storeInCache(); |
|
223
|
|
|
|
|
224
|
23 |
|
$models[] = $model; |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
24 |
|
return $models; |
|
228
|
|
|
} |
|
229
|
|
|
} |
|
230
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: