167 lines
3.3 KiB
PHP
167 lines
3.3 KiB
PHP
<?php
|
|
|
|
class UserRepository extends ModelRepository
|
|
{
|
|
private static $_singletonInstance = null;
|
|
|
|
public function __construct()
|
|
{
|
|
parent::__construct('User');
|
|
}
|
|
|
|
/**
|
|
* Returnes an initiated UserRepository
|
|
*
|
|
* @return UserRepository
|
|
*/
|
|
public static function getInstance()
|
|
{
|
|
if (self::$_singletonInstance === null)
|
|
{
|
|
self::$_singletonInstance = new UserRepository();
|
|
}
|
|
|
|
return self::$_singletonInstance;
|
|
}
|
|
|
|
|
|
/**
|
|
* Returns an object based on the provided sql
|
|
*
|
|
* @param string $sql
|
|
* @param array $arg
|
|
* @return StandardItem
|
|
*/
|
|
public function getObjectFromSql($sql, $arg)
|
|
{
|
|
$pdo = PDOMysqlConnection::getInstance();
|
|
|
|
$stmt = $pdo->prepareAndExec($sql, $arg);
|
|
if ($record = $stmt->fetch(PDO::FETCH_ASSOC))
|
|
{
|
|
return $this->_getObjectFromRecord($record);
|
|
}
|
|
|
|
// No object found, return empty object
|
|
return new User(0);
|
|
}
|
|
|
|
|
|
/**
|
|
* Returns an array of object based on the provided sql
|
|
*
|
|
* @param string $sql
|
|
* @param array $arg
|
|
* @param string $index - Column to use as array index for values returned
|
|
* @return array
|
|
*/
|
|
public function getObjectListFromSql($sql, $arg, $index = null)
|
|
{
|
|
$pdo = PDOMysqlConnection::getInstance();
|
|
|
|
$stmt = $pdo->prepareAndExec($sql, $arg);
|
|
$records = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
if (is_array($records) && !empty($records))
|
|
{
|
|
return $this->_getObjectListFromRecords($records, $index);
|
|
}
|
|
|
|
// No object found, return empty array
|
|
return [];
|
|
}
|
|
|
|
|
|
/**
|
|
* Get user object by id
|
|
*
|
|
* @param int $id
|
|
* @return User
|
|
*/
|
|
public function getObjectById($id)
|
|
{
|
|
if (!isInt($id))
|
|
{
|
|
return new User(0);
|
|
}
|
|
|
|
static $cache = array();
|
|
$key = $id;
|
|
if (!isset($cache[$key]))
|
|
{
|
|
$cache[$key] = $this->getObjectFromSql('select * from users where id = ? LIMIT 1', [$id]);
|
|
}
|
|
return $cache[$key];
|
|
}
|
|
|
|
|
|
/**
|
|
* Get user object by username
|
|
*
|
|
* @param string $username
|
|
* @return User
|
|
*/
|
|
public function getObjectByUsername($username)
|
|
{
|
|
$username = trim($username);
|
|
if (empty($username))
|
|
{
|
|
return new User(0);
|
|
}
|
|
|
|
static $cache = array();
|
|
$key = $username;
|
|
if (!isset($cache[$key]))
|
|
{
|
|
$cache[$key] = $this->getObjectFromSql('select * from users where user_name = ? LIMIT 1', [$username]);
|
|
}
|
|
return $cache[$key];
|
|
}
|
|
|
|
|
|
/**
|
|
* Get user object by call sign
|
|
*
|
|
* @param string $username
|
|
* @return User
|
|
*/
|
|
public function getObjectByCallsign($callsign)
|
|
{
|
|
$callsign = trim($callsign);
|
|
if (empty($callsign))
|
|
{
|
|
return new User(0);
|
|
}
|
|
|
|
static $cache = array();
|
|
$key = $callsign;
|
|
if (!isset($cache[$key]))
|
|
{
|
|
$cache[$key] = $this->getObjectFromSql('select * from users where callsign = ? LIMIT 1', [$callsign]);
|
|
}
|
|
return $cache[$key];
|
|
}
|
|
|
|
/**
|
|
* Get user object by email address
|
|
*
|
|
* @param string $email
|
|
* @return User
|
|
*/
|
|
public function getObjectByEmail($email)
|
|
{
|
|
$email = trim($email);
|
|
if (empty($email))
|
|
{
|
|
return new User(0);
|
|
}
|
|
|
|
static $cache = array();
|
|
$key = $email;
|
|
if (!isset($cache[$key]))
|
|
{
|
|
$cache[$key] = $this->getObjectFromSql('select * from users where email = ? LIMIT 1', [$email]);
|
|
}
|
|
return $cache[$key];
|
|
}
|
|
}
|