41 lines
1.6 KiB
PHP
41 lines
1.6 KiB
PHP
|
<?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
|
||
|
* @param string $conjunction How to handle multiple conditions (optional; defaults to `AND`)
|
||
|
* @return bool True if any documents match the field comparison, false if not
|
||
|
* @throws DocumentException If any is encountered
|
||
|
*/
|
||
|
public static function byFields(string $tableName, array $fields, string $conjunction = 'AND'): bool
|
||
|
{
|
||
|
$namedFields = Parameters::nameFields($fields);
|
||
|
return Custom::scalar(Query\Exists::byFields($tableName, $namedFields, $conjunction),
|
||
|
Parameters::addFields($namedFields, []), new ExistsMapper());
|
||
|
}
|
||
|
}
|