58 lines
1.8 KiB
PHP
58 lines
1.8 KiB
PHP
<?php namespace RVR\Repository;
|
|
|
|
use Generator;
|
|
use SokoWeb\Interfaces\Repository\IUserRepository;
|
|
use SokoWeb\Database\Query\Select;
|
|
use RVR\PersistentData\Model\User;
|
|
|
|
class UserRepository implements IUserRepository
|
|
{
|
|
public function getById(int $userId): ?User
|
|
{
|
|
return \Container::$persistentDataManager->selectFromDbById($userId, User::class);
|
|
}
|
|
|
|
public function getByEmail(string $email): ?User
|
|
{
|
|
$select = new Select(\Container::$dbConnection);
|
|
$select->where('email', '=', $email);
|
|
|
|
return \Container::$persistentDataManager->selectFromDb($select, User::class);
|
|
}
|
|
|
|
public function getByUsername(string $username): ?User
|
|
{
|
|
$select = new Select(\Container::$dbConnection);
|
|
$select->where('username', '=', $username);
|
|
|
|
return \Container::$persistentDataManager->selectFromDb($select, User::class);
|
|
}
|
|
|
|
public function getByEmailOrUsername(string $emailOrUsername): ?User
|
|
{
|
|
if (filter_var($emailOrUsername, FILTER_VALIDATE_EMAIL)) {
|
|
return $this->getByEmail($emailOrUsername);
|
|
}
|
|
|
|
return $this->getByUsername($emailOrUsername);
|
|
}
|
|
|
|
public function getByGoogleSub(string $sub): ?User
|
|
{
|
|
$select = new Select(\Container::$dbConnection);
|
|
$select->where('google_sub', '=', $sub);
|
|
|
|
return \Container::$persistentDataManager->selectFromDb($select, User::class);
|
|
}
|
|
|
|
public function searchByName(string $name): Generator
|
|
{
|
|
$select = new Select(\Container::$dbConnection);
|
|
$select->where('full_name', 'LIKE', '%' . $name . '%');
|
|
$select->orWhere('nickname', 'LIKE', '%' . $name . '%');
|
|
$select->limit(10);
|
|
|
|
yield from \Container::$persistentDataManager->selectMultipleFromDb($select, User::class);
|
|
}
|
|
}
|