|
|
@@ -12,9 +12,9 @@ abstract class Model {
|
|
|
protected static $repositories = [];
|
|
|
|
|
|
// Object variables
|
|
|
- protected $model_data;
|
|
|
- protected $modified_fields = [];
|
|
|
- protected $in_db;
|
|
|
+ protected $modelData;
|
|
|
+ protected $modifiedFields = [];
|
|
|
+ protected $inDatabase;
|
|
|
|
|
|
// Magic methods
|
|
|
|
|
|
@@ -23,8 +23,7 @@ abstract class Model {
|
|
|
* @return mixed
|
|
|
*/
|
|
|
public function __get(string $name) {
|
|
|
-
|
|
|
- if(in_array($name, static::$object_fields)) {
|
|
|
+ if(in_array($name, static::$object_fields, true)) {
|
|
|
return $this->$name ?? null;
|
|
|
}
|
|
|
|
|
|
@@ -32,17 +31,17 @@ abstract class Model {
|
|
|
$name = static::$field_map[$name];
|
|
|
}
|
|
|
|
|
|
- return isset($this->model_data->$name) ? $this->model_data->$name : null;
|
|
|
+ return $this->modelData->$name ?? null;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @param string $name
|
|
|
- * @param string $value
|
|
|
+ * @param string|null $value
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
- public function __set(string $name, $value): void {
|
|
|
+ public function __set(string $name, ?string $value): void {
|
|
|
|
|
|
- if(in_array($name, static::$object_fields)) {
|
|
|
+ if(in_array($name, static::$object_fields, true)) {
|
|
|
$this->$name = $value;
|
|
|
return;
|
|
|
}
|
|
|
@@ -53,28 +52,23 @@ abstract class Model {
|
|
|
|
|
|
$id_field = static::$id_field;
|
|
|
|
|
|
- if($name == "model_data") {
|
|
|
- $this->model_data = $value;
|
|
|
+ if($name === "model_data") {
|
|
|
+ $this->modelData = $value;
|
|
|
}
|
|
|
- else if(in_array($name, static::$fillable_fields)) {
|
|
|
-
|
|
|
- if(!is_null($value)) {
|
|
|
- $value = strval($value);
|
|
|
- }
|
|
|
-
|
|
|
- if(!isset($this->model_data->$name) || ($this->model_data->$name !== $value)) {
|
|
|
- $this->model_data->$name = $value;
|
|
|
- $this->modified_fields[] = $name;
|
|
|
+ else if(in_array($name, static::$fillable_fields, true)) {
|
|
|
+ if(!isset($this->modelData->$name) || ($this->modelData->$name !== $value)) {
|
|
|
+ $this->modelData->$name = $value;
|
|
|
+ $this->modifiedFields[] = $name;
|
|
|
}
|
|
|
}
|
|
|
- else if($name == $id_field && empty($this->model_data->$id_field)) {
|
|
|
- $this->model_data->$id_field = $value;
|
|
|
+ else if($name === $id_field && empty($this->modelData->$id_field)) {
|
|
|
+ $this->modelData->$id_field = $value;
|
|
|
}
|
|
|
else if(static::$enforce_field_check) {
|
|
|
throw new Exception("Attempt to set an unfillable field $name");
|
|
|
}
|
|
|
else {
|
|
|
- $this->model_data->$name = $value;
|
|
|
+ $this->modelData->$name = $value;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -111,7 +105,7 @@ abstract class Model {
|
|
|
$args = func_get_args();
|
|
|
$fields = $id_field;
|
|
|
|
|
|
- if(count($args) != count($fields)) return null;
|
|
|
+ if(count($args) !== count($fields)) return null;
|
|
|
|
|
|
foreach($fields as $index => $field) {
|
|
|
$nvp[$field] = $args[$index];
|
|
|
@@ -121,11 +115,11 @@ abstract class Model {
|
|
|
$nvp[$id_field] = $id;
|
|
|
}
|
|
|
|
|
|
- $where = db::get_where_string($nvp, true);
|
|
|
+ $where = DB::getWhereString($nvp, true);
|
|
|
|
|
|
$sql = "SELECT * FROM `$table` WHERE $where";
|
|
|
- $result = db::query($sql);
|
|
|
- $object = db::object_or_null($result);
|
|
|
+ $result = DB::query($sql);
|
|
|
+ $object = DB::objectOrNull($result);
|
|
|
|
|
|
if($object) {
|
|
|
$object = new static($object);
|
|
|
@@ -139,13 +133,12 @@ abstract class Model {
|
|
|
|
|
|
/**
|
|
|
* @param int $id
|
|
|
- * @param bool $exception_message
|
|
|
+ * @param string|null $exception_message
|
|
|
* @return static|null
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
- public static function getOrException(int $id, bool $exception_message = false): ?object {
|
|
|
-
|
|
|
- if(!$exception_message) $exception_message = get_called_class() . " not found.";
|
|
|
+ public static function getOrException(int $id, string $exception_message = null): ?object {
|
|
|
+ if(!$exception_message) $exception_message = static::class . " not found.";
|
|
|
|
|
|
$object = static::get($id);
|
|
|
if(!$object) throw new Exception($exception_message);
|
|
|
@@ -162,8 +155,8 @@ abstract class Model {
|
|
|
$table = static::$table;
|
|
|
|
|
|
$sql = "SELECT * FROM `$table`";
|
|
|
- $result = db::query($sql);
|
|
|
- while($row = db::fetch_object($result)) {
|
|
|
+ $result = DB::query($sql);
|
|
|
+ while($row = DB::fetchObject($result)) {
|
|
|
$objects[] = new static($row);
|
|
|
}
|
|
|
|
|
|
@@ -171,7 +164,7 @@ abstract class Model {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * @param array ...$id_sets
|
|
|
+ * @param int[] ...$id_sets
|
|
|
* @return static[]
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
@@ -179,10 +172,7 @@ abstract class Model {
|
|
|
|
|
|
$table = static::$table;
|
|
|
|
|
|
- $ids = [];
|
|
|
- foreach($id_sets as $set) {
|
|
|
- $ids = array_merge($ids, $set);
|
|
|
- }
|
|
|
+ $ids = array_merge(...$id_sets);
|
|
|
|
|
|
if(empty($ids)) return [];
|
|
|
|
|
|
@@ -216,20 +206,20 @@ abstract class Model {
|
|
|
* @return int
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
- public static function deleteMultiple($nvp = []): int {
|
|
|
+ public static function deleteMultiple(array $nvp = []): int {
|
|
|
$table = static::$table;
|
|
|
|
|
|
$vs = "1";
|
|
|
if(!empty($nvp)) {
|
|
|
$nvp = static::getRemappedNVP($nvp);
|
|
|
- $vs = db::get_where_string($nvp);
|
|
|
+ $vs = DB::getWhereString($nvp);
|
|
|
}
|
|
|
|
|
|
$sql = "DELETE FROM `$table` WHERE $vs";
|
|
|
- $result = db::query($sql);
|
|
|
+ $result = DB::query($sql);
|
|
|
if(!$result) return false;
|
|
|
|
|
|
- return db::affected_rows();
|
|
|
+ return DB::affectedRows();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -238,13 +228,12 @@ abstract class Model {
|
|
|
* @return static[]
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
- public static function query($nvp = [], $options = []): array {
|
|
|
+ public static function query($nvp = [], array $options = []): array {
|
|
|
if(is_array($nvp)) {
|
|
|
return static::findByNVP($nvp, $options);
|
|
|
}
|
|
|
- else {
|
|
|
- return static::findByValueString($nvp, $options);
|
|
|
- }
|
|
|
+
|
|
|
+ return static::findByValueString($nvp, $options);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -253,7 +242,7 @@ abstract class Model {
|
|
|
* @return static[]
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
- public static function queryFuzzy($nvp = [], $options = []): array {
|
|
|
+ public static function queryFuzzy(array $nvp = [], array $options = []): array {
|
|
|
$options["fuzzy"] = true;
|
|
|
return static::findByNVP($nvp, $options);
|
|
|
}
|
|
|
@@ -264,7 +253,7 @@ abstract class Model {
|
|
|
* @return static|null
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
- public static function queryOne($nvp = [], $options = []): ?object {
|
|
|
+ public static function queryOne(array $nvp = [], array $options = []): ?object {
|
|
|
$rows = static::findByNVP($nvp, $options);
|
|
|
return $rows ? $rows[0] : null;
|
|
|
}
|
|
|
@@ -273,7 +262,7 @@ abstract class Model {
|
|
|
* @param array $nvp
|
|
|
* @return array
|
|
|
*/
|
|
|
- private static function getRemappedNVP($nvp = []): array {
|
|
|
+ private static function getRemappedNVP(array $nvp = []): array {
|
|
|
$mapped_nvp = [];
|
|
|
foreach($nvp as $field => $value) {
|
|
|
$field = static::$field_map[$field] ?? $field;
|
|
|
@@ -287,20 +276,18 @@ abstract class Model {
|
|
|
* @return int
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
- public static function count($nvp = []): int {
|
|
|
+ public static function count(array $nvp = []): int {
|
|
|
$table = static::$table;
|
|
|
|
|
|
$vs = "1";
|
|
|
if(!empty($nvp)) {
|
|
|
$nvp = static::getRemappedNVP($nvp);
|
|
|
- $vs = db::get_where_string($nvp);
|
|
|
+ $vs = DB::getWhereString($nvp);
|
|
|
}
|
|
|
|
|
|
$sql = "SELECT COUNT(*) AS `count` FROM `$table` WHERE $vs";
|
|
|
- $result = db::query($sql);
|
|
|
- $row = db::fetch_object($result);
|
|
|
-
|
|
|
- return $row->count;
|
|
|
+ $result = DB::query($sql);
|
|
|
+ return DB::fetchObject($result)->count;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -316,7 +303,7 @@ abstract class Model {
|
|
|
}
|
|
|
else {
|
|
|
$nvp = static::getRemappedNVP($nvp);
|
|
|
- $vs = db::get_where_string($nvp, $options);
|
|
|
+ $vs = DB::getWhereString($nvp, $options);
|
|
|
}
|
|
|
|
|
|
return static::findByValueString($vs, $options);
|
|
|
@@ -324,14 +311,14 @@ abstract class Model {
|
|
|
|
|
|
/**
|
|
|
* @param string $vs
|
|
|
- * @param array $options
|
|
|
+ * @param array $options_array
|
|
|
* @return static[]
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
- private static function findByValueString(string $vs, array $options = []): array {
|
|
|
+ private static function findByValueString(string $vs, array $options_array = []): array {
|
|
|
$table = static::$table;
|
|
|
|
|
|
- $options = (object) $options;
|
|
|
+ $options = (object) $options_array;
|
|
|
|
|
|
$objects = [];
|
|
|
|
|
|
@@ -354,8 +341,8 @@ abstract class Model {
|
|
|
$sql .= " $options->limit";
|
|
|
}
|
|
|
|
|
|
- $result = db::query($sql);
|
|
|
- while($row = db::fetch_object($result)) {
|
|
|
+ $result = DB::query($sql);
|
|
|
+ while($row = DB::fetchObject($result)) {
|
|
|
$objects[] = new static($row);
|
|
|
}
|
|
|
|
|
|
@@ -376,25 +363,25 @@ abstract class Model {
|
|
|
$model_data = (object) $model_data;
|
|
|
}
|
|
|
|
|
|
- $this->model_data = $model_data;
|
|
|
+ $this->modelData = $model_data;
|
|
|
|
|
|
- $this->in_db = !is_null($this->getId());
|
|
|
+ $this->inDatabase = !is_null($this->getId());
|
|
|
|
|
|
}
|
|
|
|
|
|
// Object methods
|
|
|
- public final function isModified() {
|
|
|
- return !empty($this->modified_fields);
|
|
|
+ final public function isModified(): bool {
|
|
|
+ return !empty($this->modifiedFields);
|
|
|
}
|
|
|
|
|
|
- public final function getModifiedFields() {
|
|
|
- return $this->modified_fields;
|
|
|
+ final public function getModifiedFields(): array {
|
|
|
+ return $this->modifiedFields;
|
|
|
}
|
|
|
|
|
|
- private function preCommit() {
|
|
|
+ private function preCommit(): void {
|
|
|
}
|
|
|
|
|
|
- private final function getIDWhere() {
|
|
|
+ private function getIDWhere(): string {
|
|
|
|
|
|
$id_field = static::$id_field;
|
|
|
$id = $this->getId();
|
|
|
@@ -407,9 +394,7 @@ abstract class Model {
|
|
|
$nvp[$id_field] = $id;
|
|
|
}
|
|
|
|
|
|
- $where = db::get_where_string($nvp, true);
|
|
|
-
|
|
|
- return $where;
|
|
|
+ return DB::getWhereString($nvp, true);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -417,7 +402,7 @@ abstract class Model {
|
|
|
* @return bool
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
- public final function commit($testing = false) {
|
|
|
+ final public function commit(bool $testing = false): bool {
|
|
|
|
|
|
$this->preCommit();
|
|
|
|
|
|
@@ -427,25 +412,25 @@ abstract class Model {
|
|
|
|
|
|
$nvp = [];
|
|
|
|
|
|
- foreach($this->modified_fields as $field) {
|
|
|
- if(in_array($field, $blob_fields)) {
|
|
|
- $nvp[$field] = "x'" . bin2hex($this->model_data->$field) . "'";
|
|
|
+ foreach($this->modifiedFields as $field) {
|
|
|
+ if(in_array($field, $blob_fields, true)) {
|
|
|
+ $nvp[$field] = "x'" . bin2hex($this->modelData->$field) . "'";
|
|
|
}
|
|
|
// NULL
|
|
|
- else if(is_null($this->model_data->$field)) {
|
|
|
+ else if(is_null($this->modelData->$field)) {
|
|
|
$nvp[$field] = null;
|
|
|
}
|
|
|
// Value
|
|
|
else {
|
|
|
- $nvp[$field] = db::escape($this->model_data->$field);
|
|
|
+ $nvp[$field] = DB::escape($this->modelData->$field);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if(empty($nvp)) return true;
|
|
|
|
|
|
- $value_string = db::get_value_string($nvp);
|
|
|
+ $value_string = DB::getValueString($nvp);
|
|
|
|
|
|
- if($this->in_db) {
|
|
|
+ if($this->inDatabase) {
|
|
|
// Update
|
|
|
$where = $this->getIDWhere();
|
|
|
$sql = "UPDATE `$table` SET $value_string WHERE $where";
|
|
|
@@ -455,30 +440,30 @@ abstract class Model {
|
|
|
$sql = "INSERT INTO `$table` SET $value_string";
|
|
|
}
|
|
|
|
|
|
- if($testing == false) {
|
|
|
- $result = db::query($sql);
|
|
|
+ if($testing === false) {
|
|
|
+ $result = DB::query($sql);
|
|
|
|
|
|
- if(empty($this->in_db) && $result) {
|
|
|
+ if(empty($this->inDatabase) && $result) {
|
|
|
// Set ID
|
|
|
- $insert_id = db::insert_id();
|
|
|
+ $insert_id = DB::insertId();
|
|
|
if(!empty($insert_id)) {
|
|
|
$this->$id_field = $insert_id;
|
|
|
}
|
|
|
- $this->in_db = true;
|
|
|
+ $this->inDatabase = true;
|
|
|
}
|
|
|
}
|
|
|
else {
|
|
|
- db::$last_query = $sql;
|
|
|
+ DB::$lastQuery = $sql;
|
|
|
|
|
|
$result = true;
|
|
|
- if(empty($this->in_db) && !is_array(static::$id_field)) {
|
|
|
- $this->$id_field = mt_rand(1, 32768);
|
|
|
+ if(empty($this->inDatabase) && !is_array(static::$id_field)) {
|
|
|
+ $this->$id_field = random_int(1, 32768);
|
|
|
}
|
|
|
- $this->in_db = true;
|
|
|
+ $this->inDatabase = true;
|
|
|
}
|
|
|
|
|
|
if($result) {
|
|
|
- $this->modified_fields = [];
|
|
|
+ $this->modifiedFields = [];
|
|
|
}
|
|
|
|
|
|
return !empty($result);
|
|
|
@@ -488,7 +473,7 @@ abstract class Model {
|
|
|
* @return bool
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
- public function testCommit() {
|
|
|
+ public function testCommit(): bool {
|
|
|
return $this->commit(true);
|
|
|
}
|
|
|
|
|
|
@@ -497,10 +482,10 @@ abstract class Model {
|
|
|
* @return bool
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
- public final function commitOrException($testing = false) {
|
|
|
+ final public function commitOrException(bool $testing = false): bool {
|
|
|
$success = $this->commit($testing);
|
|
|
if(!$success) throw new Exception("Database error.");
|
|
|
- return $success;
|
|
|
+ return true;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -517,9 +502,9 @@ abstract class Model {
|
|
|
$where = $this->getIDWhere();
|
|
|
|
|
|
$sql = "DELETE FROM `$table` WHERE $where";
|
|
|
- $result = db::query($sql);
|
|
|
+ $result = DB::query($sql);
|
|
|
|
|
|
- return !empty($result) && db::affected_rows();
|
|
|
+ return !empty($result) && DB::affectedRows();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -538,18 +523,18 @@ abstract class Model {
|
|
|
$values = [];
|
|
|
$fields = $id_field;
|
|
|
foreach($fields as $field) {
|
|
|
- $value = $this->model_data->$field ?? null;
|
|
|
+ $value = $this->modelData->$field ?? null;
|
|
|
if($value === null) return null;
|
|
|
- $values[] = $this->model_data->$field;
|
|
|
+ $values[] = $this->modelData->$field;
|
|
|
}
|
|
|
return $values;
|
|
|
}
|
|
|
|
|
|
- return $this->model_data->$id_field ?? null;
|
|
|
+ return $this->modelData->$id_field ?? null;
|
|
|
}
|
|
|
|
|
|
final public function getModelData() {
|
|
|
- return clone $this->model_data;
|
|
|
+ return clone $this->modelData;
|
|
|
}
|
|
|
|
|
|
}
|