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

tempestphp / tempest-framework / 14112896474

27 Mar 2025 05:24PM UTC coverage: 79.673% (+0.3%) from 79.334%
14112896474

Pull #1076

github

web-flow
Merge a57778677 into 6af05d563
Pull Request #1076: refactor(database): remove `DatabaseModel` interface

550 of 564 new or added lines in 40 files covered. (97.52%)

1 existing line in 1 file now uncovered.

10759 of 13504 relevant lines covered (79.67%)

93.05 hits per line

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

88.24
/src/Tempest/Database/src/Builder/QueryBuilders/UpdateQueryBuilder.php
1
<?php
2

3
namespace Tempest\Database\Builder\QueryBuilders;
4

5
use Tempest\Database\Id;
6
use Tempest\Database\Query;
7
use Tempest\Database\QueryStatements\UpdateStatement;
8
use Tempest\Database\QueryStatements\WhereStatement;
9
use Tempest\Mapper\SerializerFactory;
10
use Tempest\Reflection\ClassReflector;
11
use Tempest\Support\Arr\ImmutableArray;
12

13
use function Tempest\Database\model;
14
use function Tempest\Support\arr;
15

16
final class UpdateQueryBuilder
17
{
18
    private UpdateStatement $update;
19

20
    private array $bindings = [];
21

22
    public function __construct(
11✔
23
        private string|object $model,
24
        private array|ImmutableArray $values,
25
        private SerializerFactory $serializerFactory,
26
    )
27
    {
28
        $this->update = new UpdateStatement(
11✔
29
            table: model($this->model)->getTableDefinition(),
11✔
30
        );
11✔
31
    }
32

33
    public function execute(...$bindings): Id
3✔
34
    {
35
        return $this->build()->execute(...$bindings);
3✔
36
    }
37

38
    public function allowAll(): self
1✔
39
    {
40
        $this->update->allowAll = true;
1✔
41

42
        return $this;
1✔
43
    }
44

45
    public function where(string $where, mixed ...$bindings): self
9✔
46
    {
47
        $this->update->where[] = new WhereStatement($where);
9✔
48

49
        $this->bind(...$bindings);
9✔
50

51
        return $this;
9✔
52
    }
53

54
    public function bind(mixed ...$bindings): self
9✔
55
    {
56
        $this->bindings = [...$this->bindings, ...$bindings];
9✔
57

58
        return $this;
9✔
59
    }
60

61
    public function build(): Query
11✔
62
    {
63
        $values = $this->resolveValues();
11✔
64

65
        unset($values['id']);
11✔
66

67
        $this->update->values = $values;
11✔
68

69
        if (model($this->model)->isObjectModel() && is_object($this->model)) {
11✔
70
            $this->where('`id` = ?', id: $this->model->id->id);
7✔
71
        }
72

73
        $bindings = [];
11✔
74

75
        foreach ($values as $value) {
11✔
76
            $bindings[] = $value;
11✔
77
        }
78

79
        foreach ($this->bindings as $binding) {
11✔
80
            $bindings[] = $binding;
9✔
81
        }
82

83
        return new Query($this->update, $bindings);
11✔
84
    }
85

86
    private function resolveValues(): ImmutableArray
11✔
87
    {
88
        if (! model($this->model)->isObjectModel()) {
11✔
89
            return arr($this->values);
3✔
90
        }
91

92
        $values = arr();
8✔
93

94
        $modelClass = new ClassReflector($this->model);
8✔
95

96
        foreach ($this->values as $column => $value) {
8✔
97
            $property = $modelClass->getProperty($column);
8✔
98

99
            if ($property->getType()->isRelation()) {
8✔
100
                $column .= '_id';
1✔
101

102
                $value = match (true) {
1✔
103
                    $value === null => null,
1✔
NEW
104
                    isset($value->id) => $value->id->id,
×
NEW
105
                    default => new InsertQueryBuilder(
×
NEW
106
                        $value::class,
×
NEW
107
                        [$value],
×
NEW
108
                        $this->serializerFactory,
×
NEW
109
                    )->build(),
×
110
                };
1✔
111
            }
112

113
            $serializer = $this->serializerFactory->forProperty($property);
8✔
114

115
            if ($value !== null && $serializer !== null) {
8✔
116
                $value = $serializer->serialize($value);
8✔
117
            }
118

119
            $values[$column] = $value;
8✔
120
        }
121

122
        return $values;
8✔
123
    }
124
}
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