php-censor/src/Store/UserStore.php

195 lines
4.7 KiB
PHP
Raw Permalink Normal View History

2013-05-10 17:25:51 +02:00
<?php
2016-07-19 20:28:11 +02:00
namespace PHPCensor\Store;
2013-05-10 17:25:51 +02:00
2017-02-16 13:45:50 +01:00
use PHPCensor\Store;
2018-03-04 09:34:19 +01:00
use PHPCensor\Database;
2018-03-04 11:14:09 +01:00
use PHPCensor\Exception\HttpException;
2017-02-16 13:45:50 +01:00
use PHPCensor\Model\User;
2013-05-10 17:25:51 +02:00
/**
2017-02-16 13:45:50 +01:00
* @author Dan Cryer <dan@block8.co.uk>
*/
class UserStore extends Store
2013-05-10 17:25:51 +02:00
{
2017-11-05 15:48:36 +01:00
/**
* @var string
*/
2017-10-14 20:29:29 +02:00
protected $tableName = 'user';
2017-11-05 15:48:36 +01:00
/**
* @var string
*/
2017-10-14 20:29:29 +02:00
protected $modelName = '\PHPCensor\Model\User';
2017-11-05 15:48:36 +01:00
/**
* @var string
*/
2017-10-14 20:29:29 +02:00
protected $primaryKey = 'id';
2017-02-16 13:45:50 +01:00
/**
* Get a User by primary key (Id)
2017-10-14 20:29:29 +02:00
*
* @param integer $key
* @param string $useConnection
*
* @return null|User
2017-02-16 13:45:50 +01:00
*/
2017-10-14 20:29:29 +02:00
public function getByPrimaryKey($key, $useConnection = 'read')
2017-02-16 13:45:50 +01:00
{
2017-10-14 20:29:29 +02:00
return $this->getById($key, $useConnection);
2017-02-16 13:45:50 +01:00
}
/**
* Get a single User by Id.
2017-10-14 20:29:29 +02:00
*
* @param integer $id
* @param string $useConnection
*
2017-02-16 13:45:50 +01:00
* @return null|User
2017-10-14 20:29:29 +02:00
*
* @throws HttpException
2017-02-16 13:45:50 +01:00
*/
2017-10-14 20:29:29 +02:00
public function getById($id, $useConnection = 'read')
2017-02-16 13:45:50 +01:00
{
2017-10-14 20:29:29 +02:00
if (is_null($id)) {
2017-02-16 13:45:50 +01:00
throw new HttpException('Value passed to ' . __FUNCTION__ . ' cannot be null.');
}
$query = 'SELECT * FROM {{user}} WHERE {{id}} = :id LIMIT 1';
$stmt = Database::getConnection($useConnection)->prepareCommon($query);
2017-10-14 20:29:29 +02:00
$stmt->bindValue(':id', $id);
2017-02-16 13:45:50 +01:00
if ($stmt->execute()) {
if ($data = $stmt->fetch(\PDO::FETCH_ASSOC)) {
return new User($data);
}
}
return null;
}
/**
* Get a single User by Email.
*
2017-10-14 20:29:29 +02:00
* @param string $email
2017-02-16 13:45:50 +01:00
*
* @throws HttpException
*
* @return User
*/
2017-10-14 20:29:29 +02:00
public function getByEmail($email)
2017-02-16 13:45:50 +01:00
{
2017-10-14 20:29:29 +02:00
if (is_null($email)) {
2017-02-16 13:45:50 +01:00
throw new HttpException('Value passed to ' . __FUNCTION__ . ' cannot be null.');
}
$query = 'SELECT * FROM {{user}} WHERE {{email}} = :email LIMIT 1';
$stmt = Database::getConnection()->prepareCommon($query);
2017-10-14 20:29:29 +02:00
$stmt->bindValue(':email', $email);
2017-02-16 13:45:50 +01:00
if ($stmt->execute()) {
if ($data = $stmt->fetch(\PDO::FETCH_ASSOC)) {
return new User($data);
}
}
return null;
}
/**
* Get a single User by Email or Name.
*
2017-10-14 20:29:29 +02:00
* @param string $emailOrName
2017-02-16 13:45:50 +01:00
*
* @throws HttpException
*
* @return User
*/
2017-10-14 20:29:29 +02:00
public function getByEmailOrName($emailOrName)
2017-02-16 13:45:50 +01:00
{
2017-10-14 20:29:29 +02:00
if (is_null($emailOrName)) {
2017-02-16 13:45:50 +01:00
throw new HttpException('Value passed to ' . __FUNCTION__ . ' cannot be null.');
}
$query = 'SELECT * FROM {{user}} WHERE {{email}} = :value OR {{name}} = :value LIMIT 1';
2017-08-28 16:23:27 +02:00
$stmt = Database::getConnection()->prepareCommon($query);
2017-10-14 20:29:29 +02:00
$stmt->bindValue(':value', $emailOrName);
2017-08-28 16:23:27 +02:00
if ($stmt->execute()) {
if ($data = $stmt->fetch(\PDO::FETCH_ASSOC)) {
return new User($data);
}
}
return null;
}
/**
* Get a single User by RememberKey.
*
2017-10-14 20:29:29 +02:00
* @param string $rememberKey
2017-08-28 16:23:27 +02:00
*
* @throws HttpException
*
* @return User
*/
2017-10-14 20:29:29 +02:00
public function getByRememberKey($rememberKey)
2017-08-28 16:23:27 +02:00
{
2017-10-14 20:29:29 +02:00
if (is_null($rememberKey)) {
2017-08-28 16:23:27 +02:00
throw new HttpException('Value passed to ' . __FUNCTION__ . ' cannot be null.');
}
2017-10-14 20:29:29 +02:00
$query = 'SELECT * FROM {{user}} WHERE {{remember_key}} = :remember_key LIMIT 1';
2017-08-28 16:23:27 +02:00
$stmt = Database::getConnection()->prepareCommon($query);
2017-10-14 20:29:29 +02:00
$stmt->bindValue(':remember_key', $rememberKey);
2017-02-16 13:45:50 +01:00
if ($stmt->execute()) {
if ($data = $stmt->fetch(\PDO::FETCH_ASSOC)) {
return new User($data);
}
}
return null;
}
/**
* Get multiple User by Name.
2017-10-14 20:29:29 +02:00
*
* @param string $name
* @param integer $limit
* @param string $useConnection
*
2017-02-16 13:45:50 +01:00
* @return array
2017-10-14 20:29:29 +02:00
*
* @throws HttpException
2017-02-16 13:45:50 +01:00
*/
2017-10-14 20:29:29 +02:00
public function getByName($name, $limit = 1000, $useConnection = 'read')
2017-02-16 13:45:50 +01:00
{
2017-10-14 20:29:29 +02:00
if (is_null($name)) {
2017-02-16 13:45:50 +01:00
throw new HttpException('Value passed to ' . __FUNCTION__ . ' cannot be null.');
}
$query = 'SELECT * FROM {{user}} WHERE {{name}} = :name LIMIT :limit';
$stmt = Database::getConnection($useConnection)->prepareCommon($query);
2017-10-14 20:29:29 +02:00
$stmt->bindValue(':name', $name);
2017-02-16 13:45:50 +01:00
$stmt->bindValue(':limit', (int)$limit, \PDO::PARAM_INT);
if ($stmt->execute()) {
$res = $stmt->fetchAll(\PDO::FETCH_ASSOC);
$map = function ($item) {
return new User($item);
};
$rtn = array_map($map, $res);
$count = count($rtn);
return ['items' => $rtn, 'count' => $count];
} else {
return ['items' => [], 'count' => 0];
}
}
2013-05-16 17:03:34 +02:00
}