aboutsummaryrefslogtreecommitdiff
path: root/classes/Model.php
blob: 9ad86f8bff0ae2c31f70943925df1d27bb555701 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php
/**
 * Provides the model class, an abstract interface to a database table
 *
 * @author Camil Staps
 *
 * BusinessAdmin: administrative software for small companies
 * Copyright (C) 2015 Camil Staps (ViviSoft)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * Thrown in Model when a table row could not be found
 */
class ModelNotFoundException extends Exception {
}

/**
 * Thrown in Model when a column cannot be edited
 */
class ModelIllegalAccessException extends Exception {
}

/**
 * Thrown in Model when a call to __set() fails
 */
class ModelSetFailedException extends Exception {
}

/**
 * An abstract interface to a database table
 */
abstract class Model {
	/**
	 * @var string   $table              The database table
	 * @var string   $primary_key        The table's primary key
	 * @var string[] $protected_columns  Columns that cannot be edited
	 * @var string[] $fillable_columns   Columns that can be edited
	 * @var string[] $timestamps         Columns that are TIMESTAMPs (special treatment in accessor and mutator)
	 * @var string[] $dates              Columns that are DATEs (special treatment in accessor and mutator)
	 * @var string[] $booleans           Columns that are BOOLEANs (special treatment in accessor and mutator)
	 */
	public static
		$table             = '',
		$primary_key       = 'id',
		$protected_columns = ['id'],
		$fillable_columns  = [],
		$timestamps        = [],
		$dates             = [],
		$booleans          = [];

	/**
	 * @var PDO $pdo              A PDO instance for database communication
	 * @var mixed[] $data         The column values
	 */
	protected $pdo, $data;

	/**
	 * Create a new instance
	 *
	 * @param PDO $pdo              The PDO class, to access the database
	 * @param int $id               The id of the row to fetch
	 *
	 * @throws PDOException         If something went wrong with the database
	 * @throws ModelNotFoundException If the id could not be found
	 */
	public function __construct($pdo, $id) {
		$this->pdo = $pdo;

		$stmt = $this->pdo->prepare("SELECT * FROM `".self::table()."` WHERE `".static::$primary_key."`=?");
		$stmt->execute([$id]);
		if ($stmt->rowCount() == 0) {
			throw new ModelNotFoundException("The ".static::$table." with id '$id' could not be found.");
		}
		$this->data = $stmt->fetch(PDO::FETCH_ASSOC);
	}

	/**
	 * Set a column value
	 *
	 * @param string $key         The column
	 * @param mixed $value        The value
	 *
	 * @throws PDOException       Database error
	 */
	public function __set($key, $value) {
		if (!in_array($key, static::$fillable_columns)) {
			throw new ModelIllegalAccessException("Column `".self::table()."`.`$key` cannot be edited.");
		}
		if ($this->data[$key] == $value) {
			return;
		}
		$stmt = $this->pdo->prepare("UPDATE `".self::table()."` SET `$key`=? WHERE `".static::$primary_key."`=?");
		$stmt->execute([
			$this->mutator($key, $value),
			$this->data[static::$primary_key]
		]);
		if ($stmt->rowCount() != 1) {
			throw new ModelEditFailedException("Failed to update `".self::table()."`.`$key` to '$value'.");
		}
		$this->data[$key] = $value;
	}

	/**
	 * Get a column value
	 *
	 * @param string $key         The column
	 *
	 * @return mixed              The value
	 */
	public function __get($key) {
		return $this->accessor($key, $this->data[$key]);
	}

	/**
	 * Create a new row
	 *
	 * @param PDO $pdo            Database connection
	 * @param mixed[] $values     The column values, in the order of $fillable_columns
	 *
	 * @throws PDOException       Database error
	 *
	 * @return self               The new item
	 */
	public static function create($pdo, $values) {
		$columns = array_combine(static::$fillable_columns, $values);
		$questions = [];

		foreach ($columns as $column => $value) {
			$columns[$column] = self::mutator($column, $value);
			$questions[] = '?';
		}

		$stmt = $pdo->prepare(
			"INSERT INTO `".self::table()."` " .
			"(`" . implode('`, `', array_keys($columns)) . "`) " .
			"VALUES (" . implode(',', $questions) . ")");
		$stmt->execute(array_values($columns));
		
		$class = get_called_class();
		return new $class($pdo, $pdo->lastInsertId());
	}

	/**
	 * Post-__get() hook to modify the value
	 *
	 * @param string $key         The column
	 * @param string $value       The value
	 *
	 * @return mixed              The modified value
	 */
	protected static function accessor($key, $value) {
		if (is_null($value)) {
			return null;
		} elseif (in_array($key, static::$booleans)) {
			return (bool) $value;
		} elseif (in_array($key, static::$dates) || in_array($key, static::$timestamps)) {
			return strtotime($value);
		} else {
			return $value;
		}
	}

	/**
	 * Pre-__set() hook to modify a value
	 *
	 * @param string $key         The column
	 * @param mixed $value        The value
	 *
	 * @return string             The modified value
	 */
	protected static function mutator($key, $value) {
		if (in_array($key, static::$dates) && is_int($value)) {
			return date('Y-m-d', $value);
		} elseif (in_array($key, static::$timestamps) && is_int($value)) {
			return date('Y-m-d H:i:s', $value);
		} else {
			return (string) $value;
		}
	}

	/**
	 * Delete the row
	 *
	 * @throws PDOException       Database error
	 *
	 * @return bool               True iff the row was really deleted
	 */
	public function delete() {
		$stmt = $this->pdo->prepare("DELETE FROM `".self::table()."` WHERE `".static::$primary_key."`=?");
		$stmt->execute([$this->data[static::$primary_key]]);
		return $stmt->rowCount() != 0;
	}

	/**
	 * The actual table, after adding prefixes and the like
	 *
	 * @return string The database table
	 */
	private static function table() {
		return Constants::db_prefix . static::$table;
	}
}