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

tempestphp / tempest-framework / 14631166702

23 Apr 2025 11:43AM UTC coverage: 80.194% (-1.0%) from 81.234%
14631166702

push

github

web-flow
fix(support): fix psr-4 namespace path generation with dots and slashes in the composer path (#1166)

1 of 1 new or added line in 1 file covered. (100.0%)

12 existing lines in 4 files now uncovered.

11758 of 14662 relevant lines covered (80.19%)

105.26 hits per line

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

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

3
namespace Tempest\Database\Builder\QueryBuilders;
4

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

15
use function Tempest\Database\model;
16
use function Tempest\Support\arr;
17

18
final class UpdateQueryBuilder
19
{
20
    use HasConditions;
21

22
    private UpdateStatement $update;
23

24
    private array $bindings = [];
25

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

36
    public function execute(mixed ...$bindings): Id
9✔
37
    {
38
        return $this->build()->execute(...$bindings);
9✔
39
    }
40

41
    public function allowAll(): self
1✔
42
    {
43
        $this->update->allowAll = true;
1✔
44

45
        return $this;
1✔
46
    }
47

48
    public function where(string $where, mixed ...$bindings): self
18✔
49
    {
50
        $this->update->where[] = new WhereStatement($where);
18✔
51

52
        $this->bind(...$bindings);
18✔
53

54
        return $this;
18✔
55
    }
56

57
    public function bind(mixed ...$bindings): self
18✔
58
    {
59
        $this->bindings = [...$this->bindings, ...$bindings];
18✔
60

61
        return $this;
18✔
62
    }
63

64
    public function build(): Query
20✔
65
    {
66
        $values = $this->resolveValues();
20✔
67

68
        unset($values['id']);
20✔
69

70
        $this->update->values = $values;
20✔
71

72
        if (model($this->model)->isObjectModel() && is_object($this->model)) {
20✔
73
            $this->where('`id` = ?', id: $this->model->id->id);
15✔
74
        }
75

76
        $bindings = [];
20✔
77

78
        foreach ($values as $value) {
20✔
79
            $bindings[] = $value;
20✔
80
        }
81

82
        foreach ($this->bindings as $binding) {
20✔
83
            $bindings[] = $binding;
18✔
84
        }
85

86
        return new Query($this->update, $bindings);
20✔
87
    }
88

89
    private function resolveValues(): ImmutableArray
20✔
90
    {
91
        if (! model($this->model)->isObjectModel()) {
20✔
92
            return arr($this->values);
4✔
93
        }
94

95
        $values = arr();
16✔
96

97
        $modelClass = new ClassReflector($this->model);
16✔
98

99
        foreach ($this->values as $column => $value) {
16✔
100
            $property = $modelClass->getProperty($column);
16✔
101

102
            if ($property->getIterableType()?->isRelation()) {
16✔
UNCOV
103
                throw new CannotUpdateHasManyRelation($modelClass->getName(), $property->getName());
×
104
            }
105

106
            if ($property->getType()->isRelation()) {
16✔
107
                $column .= '_id';
3✔
108

109
                $value = match (true) {
3✔
110
                    $value === null => null,
1✔
111
                    isset($value->id) => $value->id->id,
2✔
112
                    default => new InsertQueryBuilder(
1✔
113
                        $value::class,
1✔
114
                        [$value],
1✔
115
                        $this->serializerFactory,
1✔
116
                    )->build(),
1✔
117
                };
3✔
118
            }
119

120
            if (! $property->getType()->isRelation() && ! $property->getIterableType()?->isRelation()) {
16✔
121
                $serializer = $this->serializerFactory->forProperty($property);
14✔
122

123
                if ($value !== null && $serializer !== null) {
14✔
124
                    $value = $serializer->serialize($value);
14✔
125
                }
126
            }
127

128
            $values[$column] = $value;
16✔
129
        }
130

131
        return $values;
16✔
132
    }
133
}
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

© 2025 Coveralls, Inc