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

tempestphp / tempest-framework / 14104110817

27 Mar 2025 10:16AM UTC coverage: 79.581% (+0.2%) from 79.334%
14104110817

Pull #1076

github

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

471 of 480 new or added lines in 34 files covered. (98.13%)

1 existing line in 1 file now uncovered.

10679 of 13419 relevant lines covered (79.58%)

92.77 hits per line

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

98.39
/src/Tempest/Database/src/IsDatabaseModel.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Tempest\Database;
6

7
use Tempest\Database\Builder\QueryBuilders\SelectQueryBuilder;
8
use Tempest\Database\Exceptions\MissingRelation;
9
use Tempest\Database\Exceptions\MissingValue;
10
use Tempest\Reflection\ClassReflector;
11
use Tempest\Reflection\PropertyReflector;
12

13
use Tempest\Validation\SkipValidation;
14
use function Tempest\make;
15

16
trait IsDatabaseModel
17
{
18
    #[SkipValidation]
19
    public Id $id;
20

21
    public static function new(mixed ...$params): self
63✔
22
    {
23
        return make(self::class)->from($params);
63✔
24
    }
25

26
    public static function resolve(string $input): static
1✔
27
    {
28
        return self::get(new Id($input));
1✔
29
    }
30

31
    /**
32
     * @return \Tempest\Database\Builder\QueryBuilders\SelectQueryBuilder<self>
33
     */
34
    public static function select(): SelectQueryBuilder
49✔
35
    {
36
        return query(self::class)->select();
49✔
37
    }
38

39
    /** @return self[] */
40
    public static function all(array $relations = []): array
14✔
41
    {
42
        return self::select()
14✔
43
            ->with(...$relations)
14✔
44
            ->all();
14✔
45
    }
46

47
    public static function get(Id $id, array $relations = []): ?self
20✔
48
    {
49
        return self::select()
20✔
50
            ->with(...$relations)
20✔
51
            ->get($id);
20✔
52
    }
53

54
    public static function find(mixed ...$conditions): SelectQueryBuilder
1✔
55
    {
56
        $query = self::select();
1✔
57

58
        array_walk($conditions, fn ($value, $column) => $query->whereField($column, $value));
1✔
59

60
        return $query;
1✔
61
    }
62

63
    public static function create(mixed ...$params): self
57✔
64
    {
65
        return self::new(...$params)->save();
57✔
66
    }
67

68
    public static function updateOrNew(array $find, array $update): self
1✔
69
    {
70
        $existing = self::select()->bind(...$find);
1✔
71

72
        foreach ($find as $key => $value) {
1✔
73
            $existing = $existing->where("{$key} = :{$key}");
1✔
74
        }
75

76
        $model = $existing->first() ?? self::new(...$find);
1✔
77

78
        foreach ($update as $key => $value) {
1✔
79
            $model->{$key} = $value;
1✔
80
        }
81

82
        return $model;
1✔
83
    }
84

85
    public static function updateOrCreate(array $find, array $update): self
1✔
86
    {
87
        return self::updateOrNew($find, $update)->save();
1✔
88
    }
89

90
    public function __get(string $name): mixed
3✔
91
    {
92
        $property = PropertyReflector::fromParts($this, $name);
3✔
93

94
        if ($property->hasAttribute(Lazy::class)) {
3✔
95
            $this->load($name);
1✔
96

97
            return $property->getValue($this);
1✔
98
        }
99

100
        $type = $property->getType();
2✔
101

102
        if ($type->isIterable()) {
2✔
NEW
103
            throw new MissingRelation($this, $name);
×
104
        }
105

106
        if ($type->isBuiltIn()) {
2✔
107
            throw new MissingValue($this, $name);
1✔
108
        }
109

110
        throw new MissingRelation($this, $name);
1✔
111
    }
112

113
    public function load(string ...$relations): self
9✔
114
    {
115
        $new = self::get($this->id, $relations);
9✔
116

117
        foreach (new ClassReflector($new)->getPublicProperties() as $property) {
9✔
118
            $property->setValue($this, $property->getValue($new));
9✔
119
        }
120

121
        return $this;
9✔
122
    }
123

124
    public function save(): self
57✔
125
    {
126
        if (! isset($this->id)) {
57✔
127
            $query = query($this::class)->insert($this);
57✔
128
        } else {
129
            $query = query($this)->update();
4✔
130
        }
131

132
        $id = $query->build()->execute();
57✔
133

134
        $this->id = $id;
57✔
135

136
        return $this;
57✔
137
    }
138

139
    public function update(mixed ...$params): self
1✔
140
    {
141
        foreach ($params as $key => $value) {
1✔
142
            $this->{$key} = $value;
1✔
143
        }
144

145
        return $this->save();
1✔
146
    }
147

148
    public function delete(): void
2✔
149
    {
150
        query($this)
2✔
151
            ->delete()
2✔
152
            ->build()
2✔
153
            ->execute();
2✔
154
    }
155
}
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