Use Option for single doc queries
This commit is contained in:
@@ -6,6 +6,7 @@ use BitBadger\PDODocument\Mapper\Mapper;
|
||||
use PDO;
|
||||
use PDOException;
|
||||
use PDOStatement;
|
||||
use PhpOption\{None, Option, Some};
|
||||
|
||||
/**
|
||||
* Functions to execute custom queries
|
||||
@@ -92,14 +93,14 @@ class Custom
|
||||
* @param string $query The query to be executed (will have "LIMIT 1" appended)
|
||||
* @param array $parameters Parameters to use in executing the query
|
||||
* @param Mapper<TDoc> $mapper Mapper to deserialize the result
|
||||
* @return false|TDoc The item if it is found, false if not
|
||||
* @return Option<TDoc> A `Some` instance if the item is found, `None` otherwise
|
||||
* @throws DocumentException If any is encountered
|
||||
*/
|
||||
public static function single(string $query, array $parameters, Mapper $mapper): mixed
|
||||
public static function single(string $query, array $parameters, Mapper $mapper): Option
|
||||
{
|
||||
try {
|
||||
$stmt = &self::runQuery("$query LIMIT 1", $parameters);
|
||||
return ($first = $stmt->fetch(PDO::FETCH_ASSOC)) ? $mapper->map($first) : false;
|
||||
return ($first = $stmt->fetch(PDO::FETCH_ASSOC)) ? Some::create($mapper->map($first)) : None::create();
|
||||
} finally {
|
||||
$stmt = null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user