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

orchestral / sidekick / 14631614900

24 Apr 2025 01:43AM UTC coverage: 92.135% (-2.9%) from 95.0%
14631614900

Pull #25

github

web-flow
Merge 19785ac56 into 12305ceb5
Pull Request #25: Add `Orchestra\Sidekick\Eloquent\normalize_value()` function

6 of 9 new or added lines in 1 file covered. (66.67%)

82 of 89 relevant lines covered (92.13%)

2.44 hits per line

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

91.89
/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 InvalidArgumentException;
12
use Stringable;
13
use Throwable;
14

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

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

35
        return $model->qualifyColumn($attribute);
2✔
36
    }
37
}
38

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

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

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

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

69
if (! \function_exists('Orchestra\Sidekick\Eloquent\model_exists')) {
70
    /**
71
     * Check whether given $model exists.
72
     *
73
     * @api
74
     *
75
     * @param  \Illuminate\Database\Eloquent\Model|mixed  $model
76
     */
77
    function model_exists($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
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\normalize_value')) {
116
    /**
117
     * Normalize the given value to be store to database as scalar.
118
     *
119
     * @api
120
     *
121
     * @return scalar
122
     */
123
    function normalize_value(mixed $value): mixed
124
    {
125
        if ($value instanceof BackedEnum) {
7✔
NEW
126
            return $value->value;
×
127
        } elseif (\is_object($value) && $value instanceof Stringable) {
7✔
128
            return (string) $value;
2✔
129
        } elseif (\is_object($value) || \is_array($value)) {
5✔
130
            try {
131
                return json_encode($value);
3✔
NEW
132
            } catch (Throwable $e) { // @phpstan-ignore catch.neverThrown
×
NEW
133
                return $value;
×
134
            }
135
        }
136

137
        return $value;
2✔
138
    }
139
}
140

141
if (! \function_exists('Orchestra\Sidekick\Eloquent\table_name')) {
142
    /**
143
     * Get table name from Eloquent model.
144
     *
145
     * @api
146
     *
147
     * @param  \Illuminate\Database\Eloquent\Model|class-string<\Illuminate\Database\Eloquent\Model>  $model
148
     *
149
     * @throws \InvalidArgumentException
150
     */
151
    function table_name($model): string
152
    {
153
        if (\is_string($model)) {
3✔
154
            $model = new $model;
2✔
155
        }
156

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

161
        return $model->getTable();
2✔
162
    }
163
}
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