2024-06-08 23:58:45 +00:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace BitBadger\PDODocument;
|
|
|
|
|
|
|
|
use BitBadger\PDODocument\Mapper\ExistsMapper;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Functions to determine if documents exist
|
|
|
|
*/
|
|
|
|
class Exists
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Determine if a document exists for the given ID
|
|
|
|
*
|
|
|
|
* @param string $tableName The name of the table in which document existence should be determined
|
|
|
|
* @param mixed $docId The ID of the document whose existence should be determined
|
|
|
|
* @return bool True if the document exists, false if not
|
|
|
|
* @throws DocumentException If any is encountered
|
|
|
|
*/
|
|
|
|
public static function byId(string $tableName, mixed $docId): bool
|
|
|
|
{
|
|
|
|
return Custom::scalar(Query\Exists::byId($tableName), Parameters::id($docId), new ExistsMapper());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if a document exists using a comparison on JSON fields
|
|
|
|
*
|
|
|
|
* @param string $tableName The name of the table in which document existence should be determined
|
|
|
|
* @param Field[] $fields The field comparison to match
|
2024-06-11 11:07:56 +00:00
|
|
|
* @param FieldMatch|null $match How to handle multiple conditions (optional; defaults to All)
|
2024-06-08 23:58:45 +00:00
|
|
|
* @return bool True if any documents match the field comparison, false if not
|
|
|
|
* @throws DocumentException If any is encountered
|
|
|
|
*/
|
2024-06-11 11:07:56 +00:00
|
|
|
public static function byFields(string $tableName, array $fields, ?FieldMatch $match = null): bool
|
2024-06-08 23:58:45 +00:00
|
|
|
{
|
|
|
|
$namedFields = Parameters::nameFields($fields);
|
2024-06-11 11:07:56 +00:00
|
|
|
return Custom::scalar(Query\Exists::byFields($tableName, $namedFields, $match),
|
2024-06-08 23:58:45 +00:00
|
|
|
Parameters::addFields($namedFields, []), new ExistsMapper());
|
|
|
|
}
|
|
|
|
}
|