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

orchestral / sidekick / 14894588912

07 May 2025 10:38PM UTC coverage: 79.646%. Remained the same
14894588912

push

github

crynobone
Merge branch '1.1.x' into 1.2.x

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>

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

90 of 113 relevant lines covered (79.65%)

2.77 hits per line

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

63.64
/src/Eloquent/functions.php
1
<?php
2

3
namespace Orchestra\Sidekick\Eloquent;
4

5
use BackedEnum;
6
use Illuminate\Database\Eloquent\Concerns\HasUlids;
7
use Illuminate\Database\Eloquent\Concerns\HasUuids;
8
use Illuminate\Database\Eloquent\Model;
9
use Illuminate\Database\Eloquent\Relations\Concerns\AsPivot;
10
use Illuminate\Database\Eloquent\Relations\Pivot;
11
use Illuminate\Support\Collection;
12
use InvalidArgumentException;
13
use Orchestra\Sidekick\SensitiveValue;
14
use Stringable;
15
use Throwable;
16

17
if (! \function_exists('Orchestra\Sidekick\Eloquent\column_name')) {
18
    /**
19
     * Get qualify column name from Eloquent model.
20
     *
21
     * @api
22
     *
23
     * @param  \Illuminate\Database\Eloquent\Model|class-string<\Illuminate\Database\Eloquent\Model>  $model
24
     *
25
     * @throws \InvalidArgumentException
26
     */
27
    function column_name(Model|string $model, string $attribute): string
28
    {
29
        if (\is_string($model)) {
3✔
30
            $model = new $model;
2✔
31
        }
32

33
        if (! $model instanceof Model) {
3✔
34
            throw new InvalidArgumentException(\sprintf('Given $model is not an instance of [%s].', Model::class));
1✔
35
        }
36

37
        return $model->qualifyColumn($attribute);
2✔
38
    }
39
}
40

41
if (! \function_exists('Orchestra\Sidekick\Eloquent\is_pivot_model')) {
42
    /**
43
     * Determine if the given model is a pivot model.
44
     *
45
     * @api
46
     *
47
     * @template TPivotModel of (\Illuminate\Database\Eloquent\Model&\Illuminate\Database\Eloquent\Relations\Concerns\AsPivot)|\Illuminate\Database\Eloquent\Relations\Pivot
48
     *
49
     * @param  TPivotModel|class-string<TPivotModel>  $model
50
     *
51
     * @throws \InvalidArgumentException
52
     */
53
    function is_pivot_model(Pivot|Model|string $model): bool
54
    {
55
        if (\is_string($model)) {
4✔
56
            $model = new $model;
1✔
57
        }
58

59
        if (! $model instanceof Model) {
4✔
60
            throw new InvalidArgumentException(\sprintf('Given $model is not an instance of [%s|%s].', Model::class, Pivot::class));
1✔
61
        }
62

63
        if ($model instanceof Pivot) {
3✔
64
            return true;
1✔
65
        }
66

67
        return \in_array(AsPivot::class, class_uses_recursive($model), true);
2✔
68
    }
69
}
70

71
if (! \function_exists('Orchestra\Sidekick\Eloquent\model_exists')) {
72
    /**
73
     * Check whether given $model exists.
74
     *
75
     * @api
76
     */
77
    function model_exists(mixed $model): bool
78
    {
79
        return $model instanceof Model && $model->exists === true;
2✔
80
    }
81
}
82

83
if (! \function_exists('Orchestra\Sidekick\Eloquent\model_key_type')) {
84
    /**
85
     * Check whether given $model key type.
86
     *
87
     * @api
88
     *
89
     * @param  \Illuminate\Database\Eloquent\Model|class-string<\Illuminate\Database\Eloquent\Model>  $model
90
     *
91
     * @throws \InvalidArgumentException
92
     */
93
    function model_key_type(Model|string $model): string
94
    {
95
        if (\is_string($model)) {
6✔
96
            $model = new $model;
2✔
97
        }
98

99
        if (! $model instanceof Model) {
6✔
100
            throw new InvalidArgumentException(\sprintf('Given $model is not an instance of [%s].', Model::class));
1✔
101
        }
102

103
        $uses = class_uses_recursive($model);
5✔
104

105
        if (\in_array(HasUlids::class, $uses, true)) {
5✔
106
            return 'ulid';
1✔
107
        } elseif (\in_array(HasUuids::class, $uses, true)) {
4✔
108
            return 'uuid';
1✔
109
        }
110

111
        return $model->getKeyType();
3✔
112
    }
113
}
114

115
if (! \function_exists('Orchestra\Sidekick\Eloquent\model_state')) {
116
    /**
117
     * Get attributes original and changed state from a model.
118
     *
119
     * @api
120
     *
121
     * @template TPivotModel of (\Illuminate\Database\Eloquent\Model&\Illuminate\Database\Eloquent\Relations\Concerns\AsPivot)|\Illuminate\Database\Eloquent\Relations\Pivot
122
     *
123
     * @param  TPivotModel  $model
124
     * @return array{0: array<string, mixed>|null, 1: array<string, mixed>}
125
     */
126
    function model_state(Pivot|Model $model): array
127
    {
128
        $copy = clone $model;
×
129
        $hiddenAttributes = $model->getHidden();
×
130

131
        $copy->setHidden([]);
×
132

133
        $sanitizeValues = function (array $attributes) use ($hiddenAttributes): array {
134
            return Collection::make($attributes)
×
135
                ->map(
×
136
                    static fn (mixed $value, string $attribute) => \in_array($attribute, $hiddenAttributes, true)
×
137
                        ? new SensitiveValue($value)
×
138
                        : normalize_value($value)
×
139
                )->all();
×
140
        };
141

NEW
142
        if (! model_exists($model) || $model->wasRecentlyCreated == true) {
×
143
            $original = null;
×
144
            $changes = $sanitizeValues($copy->attributesToArray());
×
145

146
            return [$original, $changes];
×
147
        }
148

149
        $changes = $sanitizeValues($copy->getDirty());
×
150
        $original = $sanitizeValues(
×
151
            array_intersect_key($model->newInstance()->setRawAttributes($model->getRawOriginal())->attributesToArray(), $changes)
×
152
        );
×
153

154
        return [$original, $changes];
×
155
    }
156
}
157

158
if (! \function_exists('Orchestra\Sidekick\Eloquent\normalize_value')) {
159
    /**
160
     * Normalize the given value to be store to database as scalar.
161
     *
162
     * @api
163
     *
164
     * @return scalar
165
     */
166
    function normalize_value(mixed $value): mixed
167
    {
168
        if ($value instanceof BackedEnum) {
8✔
169
            return $value->value;
1✔
170
        } elseif (\is_object($value) && $value instanceof Stringable) {
7✔
171
            return (string) $value;
2✔
172
        } elseif (\is_object($value) || \is_array($value)) {
5✔
173
            try {
174
                return json_encode($value);
3✔
175
            } catch (Throwable $e) { // @phpstan-ignore catch.neverThrown
×
176
                return $value;
×
177
            }
178
        }
179

180
        return $value;
2✔
181
    }
182
}
183

184
if (! \function_exists('Orchestra\Sidekick\Eloquent\table_name')) {
185
    /**
186
     * Get table name from Eloquent model.
187
     *
188
     * @api
189
     *
190
     * @param  \Illuminate\Database\Eloquent\Model|class-string<\Illuminate\Database\Eloquent\Model>  $model
191
     *
192
     * @throws \InvalidArgumentException
193
     */
194
    function table_name(Model|string $model): string
195
    {
196
        if (\is_string($model)) {
3✔
197
            $model = new $model;
2✔
198
        }
199

200
        if (! $model instanceof Model) {
3✔
201
            throw new InvalidArgumentException(\sprintf('Given $model is not an instance of [%s].', Model::class));
1✔
202
        }
203

204
        return $model->getTable();
2✔
205
    }
206
}
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