Non puoi selezionare più di 25 argomenti
Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
160 righe
5.5 KiB
160 righe
5.5 KiB
<?php |
|
|
|
namespace MwgAuth\Core; |
|
|
|
use PDO; |
|
use PDOStatement; |
|
|
|
class Model |
|
{ |
|
private static $database = null; |
|
private static $lastErrorMessage = ''; |
|
|
|
private static function connect(): void |
|
{ |
|
if (null === self::$database) { |
|
$config = Config::getSection('database'); |
|
self::$database = new PDO( |
|
'mysql:host=' . $config['host'] . ';dbname=' . $config['database'], |
|
$config['username'], |
|
$config['password'], |
|
); |
|
} |
|
} |
|
|
|
protected static function delete(string $table, array $conditions): bool |
|
{ |
|
$where = $parameters = []; |
|
foreach ($conditions as $condition) { |
|
switch (count($condition)) { |
|
case 1: |
|
$where[] = '`' . $condition[0] . '`'; |
|
break; |
|
case 2: |
|
$where[] = '`' . $condition[0] . '`=:' . $condition[0]; |
|
$parameters[':' . $condition[0]] = $condition[1]; |
|
break; |
|
case 3: |
|
$where[] = '`' . $condition[0] . '`' . $condition[1] . ':' . $condition[0]; |
|
$parameters[':' . $condition[0]] = $condition[2]; |
|
break; |
|
default: |
|
return false; |
|
} |
|
} |
|
$sql = "DELETE FROM `$table` WHERE (" . implode(") AND (", $where) . ")"; |
|
$statement = self::prepare($sql, $parameters); |
|
return self::execute($statement); |
|
} |
|
|
|
private static function execute(PDOStatement $statement): bool |
|
{ |
|
if ($ret = $statement->execute()) { |
|
self::$lastErrorMessage = ''; |
|
} else { |
|
$errorInfo = $statement->errorInfo(); |
|
self::$lastErrorMessage = $errorInfo[0] . ': ' . $errorInfo[2]; |
|
} |
|
return $ret; |
|
} |
|
|
|
protected static function getLastErrorMessage(): string |
|
{ |
|
return self::$lastErrorMessage; |
|
} |
|
|
|
private static function getParameters(array $conditions): array |
|
{ |
|
$parameters = []; |
|
foreach ($conditions as $field => $value) { |
|
$parameters[":$field"] = $value; |
|
} |
|
return $parameters; |
|
} |
|
|
|
private static function getSelectQuery(string $table, array $fields, array $conditions): string |
|
{ |
|
$where = []; |
|
foreach ($conditions as $field => $value) { |
|
$where[] = "(`$field`=:$field)"; |
|
} |
|
return "SELECT `" . implode('`,`', $fields) . "` FROM `$table` WHERE " . implode(' AND ', $where); |
|
} |
|
|
|
protected static function insert(string $table, array $fields): bool |
|
{ |
|
$fields['created_at'] = $fields['modified_at'] = date('Y-m-d H:i:s'); |
|
$parameters = self::getParameters($fields); |
|
$sql = "INSERT INTO `$table`(`" . implode('`,`', array_keys($fields)) . "`) VALUES (" . |
|
implode(',', array_keys($parameters)) . ')'; |
|
$statement = self::prepare($sql, $parameters); |
|
return self::execute($statement); |
|
} |
|
|
|
private static function prepare(string $sql, array $parameters): PDOStatement |
|
{ |
|
self::connect(); |
|
$statement = self::$database->prepare($sql); |
|
foreach ($parameters as $name => $value) { |
|
if (is_bool($value)) { |
|
$type = PDO::PARAM_BOOL; |
|
} elseif (is_int($value)) { |
|
$type = PDO::PARAM_INT; |
|
} else { |
|
$type = PDO::PARAM_STR; |
|
} |
|
$statement->bindValue($name, $value, $type); |
|
} |
|
return $statement; |
|
} |
|
|
|
protected static function select(string $table, array $fields, array $conditions): ?array |
|
{ |
|
$parameters = self::getParameters($conditions); |
|
$sql = self::getSelectQuery($table, $fields, $conditions); |
|
$statement = self::prepare($sql, $parameters); |
|
self::execute($statement); |
|
$ret = $statement->fetchAll(PDO::FETCH_ASSOC); |
|
$statement->closeCursor(); |
|
return false === $ret ? null : $ret; |
|
} |
|
|
|
protected static function selectOne(string $table, array $fields, array $conditions): ?array |
|
{ |
|
$parameters = self::getParameters($conditions); |
|
$sql = self::getSelectQuery($table, $fields, $conditions) . ' LIMIT 1'; |
|
$statement = self::prepare($sql, $parameters); |
|
self::execute($statement); |
|
$ret = $statement->fetch(PDO::FETCH_ASSOC); |
|
$statement->closeCursor(); |
|
return false === $ret ? null : $ret; |
|
} |
|
|
|
protected static function selectSingleValue(string $table, string $field, array $conditions) |
|
{ |
|
$parameters = self::getParameters($conditions); |
|
$sql = self::getSelectQuery($table, [$field], $conditions) . ' LIMIT 1'; |
|
$statement = self::prepare($sql, $parameters); |
|
self::execute($statement); |
|
$ret = $statement->fetchColumn(); |
|
$statement->closeCursor(); |
|
return false === $ret ? null : $ret; |
|
} |
|
|
|
protected static function update(string $table, array $fields, array $conditions): bool |
|
{ |
|
$fields['modified_at'] = date('Y-m-d H:i:s'); |
|
$where = $parameters = $set = []; |
|
foreach ($fields as $field => $value) { |
|
$set[] = "`$field`=:p$field"; |
|
$parameters[":p$field"] = $value; |
|
} |
|
foreach ($conditions as $field => $value) { |
|
$where[] = "(`$field`=:w$field)"; |
|
$parameters[":w$field"] = $value; |
|
} |
|
$sql = "UPDATE `$table` SET " . implode(',', $set) . " WHERE " . implode(',', $where); |
|
$statement = self::prepare($sql, $parameters); |
|
return self::execute($statement); |
|
} |
|
}
|
|
|