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

orchestral / sidekick / 14631564558

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

Pull #25

github

web-flow
Merge f656b49cb 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%)

2 existing lines in 1 file now uncovered.

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
     * @template TPivotModel of (\Illuminate\Database\Eloquent\Model&\Illuminate\Database\Eloquent\Relations\Concerns\AsPivot)|\Illuminate\Database\Eloquent\Relations\Pivot
44
     *
45
     * @param  TPivotModel|class-string<TPivotModel>  $model
46
     *
47
     * @throws \InvalidArgumentException
48
     */
49
    function is_pivot_model($model): bool
50
    {
51
        if (\is_string($model)) {
4✔
52
            $model = new $model;
1✔
53
        }
54

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

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

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

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

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

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

101
        $uses = class_uses_recursive($model);
5✔
102

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

109
        return $model->getKeyType();
3✔
110
    }
111
}
112

113
if (! \function_exists('Orchestra\Sidekick\Eloquent\normalize_value')) {
114
    /**
115
     * Normalize the given value to be store to database as scalar.
116
     *
117
     * @return scalar
118
     */
119
    function normalize_value(mixed $value): mixed
120
    {
121
        if ($value instanceof BackedEnum) {
7✔
NEW
122
            return $value->value;
×
123
        } elseif (\is_object($value) && $value instanceof Stringable) {
7✔
124
            return (string) $value;
2✔
125
        } elseif (\is_object($value) || \is_array($value)) {
5✔
126
            try {
127
                return json_encode($value);
3✔
NEW
128
            } catch (Throwable $e) { // @phpstan-ignore catch.neverThrown
×
NEW
129
                return $value;
×
130
            }
131
        }
132

133
        return $value;
2✔
134
    }
135
}
136

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

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

157
        return $model->getTable();
2✔
158
    }
159
}
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