44 lines
1.7 KiB
PHP
44 lines
1.7 KiB
PHP
|
<?php declare(strict_types=1);
|
||
|
|
||
|
namespace BitBadger\PDODocument;
|
||
|
|
||
|
use BitBadger\PDODocument\Mapper\CountMapper;
|
||
|
use PDO;
|
||
|
|
||
|
/**
|
||
|
* Functions to count documents
|
||
|
*/
|
||
|
class Count
|
||
|
{
|
||
|
/**
|
||
|
* Count all documents in a table
|
||
|
*
|
||
|
* @param string $tableName The name of the table in which documents should be counted
|
||
|
* @param PDO|null $pdo The database connection to use (optional; will obtain one if not provided)
|
||
|
* @return int The count of documents in the table
|
||
|
* @throws DocumentException If one is encountered
|
||
|
*/
|
||
|
public static function all(string $tableName, ?PDO $pdo = null): int
|
||
|
{
|
||
|
return Custom::scalar(Query\Count::all($tableName), [], new CountMapper(), $pdo);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Count matching documents using a comparison on JSON fields
|
||
|
*
|
||
|
* @param string $tableName The name of the table in which documents should be counted
|
||
|
* @param array|Field[] $fields The field comparison to match
|
||
|
* @param PDO|null $pdo The database connection to use (optional; will obtain one if not provided)
|
||
|
* @param string $conjunction How to handle multiple conditions (optional; defaults to `AND`)
|
||
|
* @return int The count of documents matching the field comparison
|
||
|
* @throws DocumentException If one is encountered
|
||
|
*/
|
||
|
public static function byFields(string $tableName, array $fields, ?PDO $pdo = null,
|
||
|
string $conjunction = 'AND'): int
|
||
|
{
|
||
|
$namedFields = Parameters::nameFields($fields);
|
||
|
return Custom::scalar(Query\Count::byFields($tableName, $namedFields, $conjunction),
|
||
|
Parameters::addFields($namedFields, []), new CountMapper(), $pdo);
|
||
|
}
|
||
|
}
|