73 lines
2.7 KiB
PHP
73 lines
2.7 KiB
PHP
<?php
|
|
/**
|
|
* @author Daniel J. Summers <daniel@bitbadger.solutions>
|
|
* @license MIT
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace BitBadger\PDODocument;
|
|
|
|
use BitBadger\PDODocument\Mapper\CountMapper;
|
|
|
|
/**
|
|
* 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
|
|
* @return int The count of documents in the table
|
|
* @throws DocumentException If one is encountered
|
|
*/
|
|
public static function all(string $tableName): int
|
|
{
|
|
return Custom::scalar(Query\Count::all($tableName), [], new CountMapper());
|
|
}
|
|
|
|
/**
|
|
* Count matching documents using a comparison on JSON fields
|
|
*
|
|
* @param string $tableName The name of the table in which documents should be counted
|
|
* @param Field[] $fields The field comparison to match
|
|
* @param FieldMatch|null $match How to handle multiple conditions (optional; defaults to All)
|
|
* @return int The count of documents matching the field comparison
|
|
* @throws DocumentException If one is encountered
|
|
*/
|
|
public static function byFields(string $tableName, array $fields, ?FieldMatch $match = null): int
|
|
{
|
|
$namedFields = Parameters::nameFields($fields);
|
|
return Custom::scalar(Query\Count::byFields($tableName, $namedFields, $match),
|
|
Parameters::addFields($namedFields, []), new CountMapper());
|
|
}
|
|
|
|
/**
|
|
* Count matching documents using a JSON containment query (`@>`; PostgreSQL only)
|
|
*
|
|
* @param string $tableName The name of the table in which documents should be counted
|
|
* @param mixed[]|object $criteria The criteria for the JSON containment query
|
|
* @return int The number of documents matching the JSON containment query
|
|
* @throws DocumentException If the database mode is not PostgreSQL, or if an error occurs
|
|
*/
|
|
public static function byContains(string $tableName, array|object $criteria): int
|
|
{
|
|
return Custom::scalar(Query\Count::byContains($tableName), Parameters::json(':criteria', $criteria),
|
|
new CountMapper());
|
|
}
|
|
|
|
/**
|
|
* Count matching documents using a JSON Path match query (`@?`; PostgreSQL only)
|
|
*
|
|
* @param string $tableName The name of the table in which documents should be counted
|
|
* @param string $path The JSON Path match string
|
|
* @return int The number of documents matching the given JSON Path criteria
|
|
* @throws DocumentException If the database mode is not PostgreSQL, or if an error occurs
|
|
*/
|
|
public static function byJsonPath(string $tableName, string $path): int
|
|
{
|
|
return Custom::scalar(Query\Count::byJsonPath($tableName), [':path' => $path], new CountMapper());
|
|
}
|
|
}
|