[DB] Refactor findOneBy method

This commit is contained in:
Hugo Sales 2021-07-28 21:09:12 +00:00
parent 7263752b18
commit af5526d720
Signed by: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0
1 changed files with 6 additions and 7 deletions

View File

@ -176,14 +176,13 @@ abstract class DB
public static function findOneBy(string $table, array $criteria, ?array $orderBy = null, ?int $offset = null)
{
$res = self::findBy($table, $criteria, $orderBy, 2, $offset);
if (count($res) == 1) {
switch (count($res)) {
case 0:
throw new NotFoundException("No value in table {$table} matches the requested criteria");
case 1:
return $res[0];
} else {
if (count($res) == 0) {
throw new NotFoundException("No value in table {$table} matches the requested criteria");
} else {
throw new DuplicateFoundException("Multiple values in table {$table} match the requested criteria");
}
default:
throw new DuplicateFoundException("Multiple values in table {$table} match the requested criteria");
}
}