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

tempestphp / tempest-framework / 14140550176

28 Mar 2025 10:29PM UTC coverage: 80.716% (+1.4%) from 79.334%
14140550176

push

github

web-flow
feat(support): support `$default` on array `first` and `last` methods (#1096)

11 of 12 new or added lines in 2 files covered. (91.67%)

135 existing lines in 16 files now uncovered.

10941 of 13555 relevant lines covered (80.72%)

100.32 hits per line

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

98.51
/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
use Tempest\Validation\SkipValidation;
13

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
75✔
22
    {
23
        return make(self::class)->from($params);
75✔
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
58✔
35
    {
36
        return query(self::class)->select();
58✔
37
    }
38

39
    /** @return self[] */
40
    public static function all(array $relations = []): array
23✔
41
    {
42
        return self::select()
23✔
43
            ->with(...$relations)
23✔
44
            ->all();
23✔
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
65✔
64
    {
65
        return self::new(...$params)->save();
65✔
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✔
UNCOV
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
65✔
125
    {
126
        if (! isset($this->id)) {
65✔
127
            $query = query($this::class)->insert($this);
65✔
128

129
            $this->id = $query->execute();
65✔
130
        } else {
131
            query($this)->update(
9✔
132
                ...model($this)->getPropertyValues(),
9✔
133
            )->execute();
9✔
134
        }
135

136
        return $this;
65✔
137
    }
138

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

145
        query($this)
2✔
146
            ->update(...$params)
2✔
147
            ->build()
2✔
148
            ->execute();
2✔
149

150
        return $this;
2✔
151
    }
152

153
    public function delete(): void
2✔
154
    {
155
        query($this)
2✔
156
            ->delete()
2✔
157
            ->build()
2✔
158
            ->execute();
2✔
159
    }
160
}
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