Use Option for single doc queries
This commit is contained in:
17
src/Find.php
17
src/Find.php
@@ -3,6 +3,7 @@
|
||||
namespace BitBadger\PDODocument;
|
||||
|
||||
use BitBadger\PDODocument\Mapper\DocumentMapper;
|
||||
use PhpOption\Option;
|
||||
|
||||
/**
|
||||
* Functions to find documents
|
||||
@@ -30,10 +31,10 @@ class Find
|
||||
* @param string $tableName The table from which the document should be retrieved
|
||||
* @param mixed $docId The ID of the document to retrieve
|
||||
* @param class-string<TDoc> $className The name of the class to be retrieved
|
||||
* @return false|TDoc The document if it exists, false if not
|
||||
* @return Option<TDoc> A `Some` instance if the document is found, `None` otherwise
|
||||
* @throws DocumentException If any is encountered
|
||||
*/
|
||||
public static function byId(string $tableName, mixed $docId, string $className): mixed
|
||||
public static function byId(string $tableName, mixed $docId, string $className): Option
|
||||
{
|
||||
return Custom::single(Query\Find::byId($tableName, $docId), Parameters::id($docId),
|
||||
new DocumentMapper($className));
|
||||
@@ -97,11 +98,11 @@ class Find
|
||||
* @param array|Field[] $fields The field comparison to match
|
||||
* @param class-string<TDoc> $className The name of the class to be retrieved
|
||||
* @param FieldMatch|null $match How to handle multiple conditions (optional; defaults to All)
|
||||
* @return false|TDoc The first document if any matches are found, false otherwise
|
||||
* @return Option<TDoc> A `Some` instance with the first document if any matches are found, `None` otherwise
|
||||
* @throws DocumentException If any is encountered
|
||||
*/
|
||||
public static function firstByFields(string $tableName, array $fields, string $className,
|
||||
?FieldMatch $match = null): mixed
|
||||
?FieldMatch $match = null): Option
|
||||
{
|
||||
$namedFields = Parameters::nameFields($fields);
|
||||
return Custom::single(Query\Find::byFields($tableName, $namedFields, $match),
|
||||
@@ -115,10 +116,10 @@ class Find
|
||||
* @param string $tableName The name of the table from which documents should be retrieved
|
||||
* @param array|object $criteria The criteria for the JSON containment query
|
||||
* @param class-string<TDoc> $className The name of the class to be retrieved
|
||||
* @return false|TDoc The first document matching the JSON containment query if any is found, false otherwise
|
||||
* @return Option<TDoc> A `Some` instance with the first document if any matches are found, `None` otherwise
|
||||
* @throws DocumentException If the database mode is not PostgreSQL, or if an error occurs
|
||||
*/
|
||||
public static function firstByContains(string $tableName, array|object $criteria, string $className): mixed
|
||||
public static function firstByContains(string $tableName, array|object $criteria, string $className): Option
|
||||
{
|
||||
return Custom::single(Query\Find::byContains($tableName), Parameters::json(':criteria', $criteria),
|
||||
new DocumentMapper($className));
|
||||
@@ -131,10 +132,10 @@ class Find
|
||||
* @param string $tableName The name of the table from which documents should be retrieved
|
||||
* @param string $path The JSON Path match string
|
||||
* @param class-string<TDoc> $className The name of the class to be retrieved
|
||||
* @return false|TDoc The first document matching the JSON Path if any is found, false otherwise
|
||||
* @return Option<TDoc> A `Some` instance with the first document if any matches are found, `None` otherwise
|
||||
* @throws DocumentException If the database mode is not PostgreSQL, or if an error occurs
|
||||
*/
|
||||
public static function firstByJsonPath(string $tableName, string $path, string $className): mixed
|
||||
public static function firstByJsonPath(string $tableName, string $path, string $className): Option
|
||||
{
|
||||
return Custom::single(Query\Find::byJsonPath($tableName), [':path' => $path], new DocumentMapper($className));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user