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

orchestral / sidekick / 14506578776

17 Apr 2025 02:16AM UTC coverage: 89.063% (-4.4%) from 93.443%
14506578776

Pull #15

github

web-flow
Merge 64e6c24e7 into fbd2254e6
Pull Request #15: Add `Orchestra\Sidekick\Eloquent\is_pivot_model()` function

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

2 existing lines in 1 file now uncovered.

57 of 64 relevant lines covered (89.06%)

2.11 hits per line

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

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

3
namespace Orchestra\Sidekick\Eloquent;
4

5
use Illuminate\Database\Eloquent\Concerns\HasUlids;
6
use Illuminate\Database\Eloquent\Concerns\HasUuids;
7
use Illuminate\Database\Eloquent\Model;
8
use Illuminate\Database\Eloquent\Relations\Concerns\AsPivot;
9
use Illuminate\Database\Eloquent\Relations\Pivot;
10
use InvalidArgumentException;
11

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

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

30
        return $model->qualifyColumn($attribute);
2✔
31
    }
32
}
33

34
if (! \function_exists('Orchestra\Sidekick\Eloquent\is_pivot_model')) {
35
    /**
36
     * Determine if the given model is a pivot model.
37
     * 
38
     * @param  (\Illuminate\Database\Eloquent\Model&\Illuminate\Database\Eloquent\Relations\Concerns\AsPivot)|\Illuminate\Database\Eloquent\Relations\Pivot  $model
39
     */
40
    function is_pivot_model(Model|Pivot $model) {
NEW
41
        if ($model instanceof Pivot) {
×
NEW
42
            return true;
×
43
        }
44

NEW
45
        return \in_array(AsPivot::class, class_uses_recursive($model), true);
×
46
    }
47
}
48

49
if (! \function_exists('Orchestra\Sidekick\Eloquent\model_exists')) {
50
    /**
51
     * Check whether given $model exists.
52
     *
53
     * @param  \Illuminate\Database\Eloquent\Model  $model
54
     */
55
    function model_exists(Model $model): bool
56
    {
57
        return $model->exists === true;
2✔
58
    }
59
}
60

61
if (! \function_exists('Orchestra\Sidekick\Eloquent\model_key_type')) {
62
    /**
63
     * Check whether given $model key type.
64
     *
65
     * @param  \Illuminate\Database\Eloquent\Model|class-string<\Illuminate\Database\Eloquent\Model>  $model
66
     */
67
    function model_key_type(Model|string $model): string
68
    {
69
        if (\is_string($model)) {
3✔
70
            $model = new $model;
2✔
71
        }
72

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

77
        $uses = class_uses_recursive($model);
2✔
78

79
        if (\in_array(HasUlids::class, $uses, true)) {
2✔
UNCOV
80
            return 'ulid';
×
81
        } elseif (\in_array(HasUuids::class, $uses, true)) {
2✔
UNCOV
82
            return 'uuid';
×
83
        }
84

85
        return $model->getKeyType();
2✔
86
    }
87
}
88

89
if (! \function_exists('Orchestra\Sidekick\Eloquent\table_name')) {
90
    /**
91
     * Get table name from Eloquent model.
92
     *
93
     * @param  \Illuminate\Database\Eloquent\Model|class-string<\Illuminate\Database\Eloquent\Model>  $model
94
     *
95
     * @throws \InvalidArgumentException
96
     */
97
    function table_name(Model|string $model): string
98
    {
99
        if (\is_string($model)) {
3✔
100
            $model = new $model;
2✔
101
        }
102

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

107
        return $model->getTable();
2✔
108
    }
109
}
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