• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

tempestphp / tempest-framework / 14643394133

24 Apr 2025 01:52PM UTC coverage: 80.218%. First build
14643394133

Pull #1174

github

web-flow
Merge baa0b4c69 into 5f38986ac
Pull Request #1174: feat(database): add `Count` Query Builder and Statement

39 of 49 new or added lines in 3 files covered. (79.59%)

11825 of 14741 relevant lines covered (80.22%)

106.08 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

70.59
/src/Tempest/Database/src/Builder/QueryBuilders/CountQueryBuilder.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Tempest\Database\Builder\QueryBuilders;
6

7
use Tempest\Database\Builder\ModelDefinition;
8
use Tempest\Database\Builder\TableDefinition;
9
use Tempest\Database\Query;
10
use Tempest\Database\QueryStatements\CountStatement;
11
use Tempest\Database\QueryStatements\WhereStatement;
12
use Tempest\Support\Conditions\HasConditions;
13

14
/**
15
 * @template TModelClass of object
16
 */
17
final class CountQueryBuilder
18
{
19
    use HasConditions;
20

21
    /** @var class-string<TModelClass> $modelClass */
22
    private readonly string $modelClass;
23

24
    private ?ModelDefinition $modelDefinition;
25

26
    private CountStatement $count;
27

28
    private array $bindings = [];
29

30
    public function __construct(string|object $model, ?string $column = null)
4✔
31
    {
32
        $this->modelDefinition = ModelDefinition::tryFrom($model);
4✔
33
        $this->modelClass = is_object($model) ? $model::class : $model;
4✔
34

35
        $this->count = new CountStatement(
4✔
36
            table: $this->resolveTable($model),
4✔
37
            column: $column,
4✔
38
        );
4✔
39
    }
40

NEW
41
    public function execute(mixed ...$bindings): int
×
42
    {
NEW
43
        $key = "COUNT({$this->count->countArgument})";
×
44

NEW
45
        return $this->build()->fetchFirst(...$bindings)[$key];
×
46
    }
47

48
    /** @return self<TModelClass> */
49
    public function where(string $where, mixed ...$bindings): self
2✔
50
    {
51
        $this->count->where[] = new WhereStatement($where);
2✔
52

53
        $this->bind(...$bindings);
2✔
54

55
        return $this;
2✔
56
    }
57

58
    public function andWhere(string $where, mixed ...$bindings): self
2✔
59
    {
60
        return $this->where("AND {$where}", ...$bindings);
2✔
61
    }
62

63
    public function orWhere(string $where, mixed ...$bindings): self
2✔
64
    {
65
        return $this->where("OR {$where}", ...$bindings);
2✔
66
    }
67

68
    /** @return self<TModelClass> */
NEW
69
    public function whereField(string $field, mixed $value): self
×
70
    {
NEW
71
        $field = $this->modelDefinition->getFieldDefinition($field);
×
72

NEW
73
        return $this->where("{$field} = :{$field->name}", ...[$field->name => $value]);
×
74
    }
75

76
    /** @return self<TModelClass> */
77
    public function bind(mixed ...$bindings): self
2✔
78
    {
79
        $this->bindings = [...$this->bindings, ...$bindings];
2✔
80

81
        return $this;
2✔
82
    }
83

NEW
84
    public function toSql(): string
×
85
    {
NEW
86
        return $this->build()->getSql();
×
87
    }
88

89
    public function build(array $bindings = []): Query
4✔
90
    {
91
        return new Query($this->count, [...$this->bindings, ...$bindings]);
4✔
92
    }
93

NEW
94
    private function clone(): self
×
95
    {
NEW
96
        return clone $this;
×
97
    }
98

99
    private function resolveTable(string|object $model): TableDefinition
4✔
100
    {
101
        if ($this->modelDefinition === null) {
4✔
102
            return new TableDefinition($model);
3✔
103
        }
104

105
        return $this->modelDefinition->getTableDefinition();
1✔
106
    }
107
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc