1 | <?php |
||
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() |
|
36 | |||
37 | /** |
||
38 | * Find if the model is active (i.e. visible to everyone) |
||
39 | * |
||
40 | * @return bool |
||
41 | */ |
||
42 | 1 | public function isActive() |
|
46 | |||
47 | /** |
||
48 | * Get the models's status |
||
49 | * |
||
50 | * @return string |
||
51 | */ |
||
52 | 1 | public function getStatus() |
|
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() |
||
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) |
|
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() |
|
155 | |||
156 | /** |
||
157 | * Gets a human-readable format of the model's type |
||
158 | * @return string |
||
159 | */ |
||
160 | public static function getTypeForHumans() |
||
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) |
|
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) |
|
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) |
|
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) |
|
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: