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

conedevelopment / bazar / 20694116184

04 Jan 2026 02:08PM UTC coverage: 68.615% (+4.5%) from 64.117%
20694116184

push

github

iamgergo
version

1679 of 2447 relevant lines covered (68.61%)

25.06 hits per line

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

71.88
/src/Fields/Items.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Cone\Bazar\Fields;
6

7
use Closure;
8
use Cone\Bazar\Models\Product;
9
use Cone\Bazar\Models\Variant;
10
use Cone\Root\Fields\MorphMany;
11
use Cone\Root\Fields\MorphTo;
12
use Cone\Root\Fields\Number;
13
use Cone\Root\Fields\Text;
14
use Illuminate\Database\Eloquent\Model;
15
use Illuminate\Http\Request;
16

17
class Items extends MorphMany
18
{
19
    /**
20
     * The relations to eager load on every query.
21
     */
22
    protected array $with = [
23
        'buyable',
24
        'checkoutable',
25
        'taxes',
26
    ];
27

28
    /**
29
     * The buyable types.
30
     */
31
    protected array $buyableTypes = [];
32

33
    /**
34
     * The buyable searchable columns.
35
     */
36
    protected array $buyableSearchableColumns = [];
37

38
    /**
39
     * The buyable item display resolver.
40
     */
41
    protected ?Closure $buyableItemDisplayResolver = null;
42

43
    /**
44
     * Create a new relation field instance.
45
     */
46
    public function __construct(?string $label = null, Closure|string|null $modelAttribute = null, Closure|string|null $relation = null)
101✔
47
    {
48
        parent::__construct($label ?: __('Products'), $modelAttribute ?: 'items', $relation);
101✔
49

50
        $this->display('name');
101✔
51
        $this->asSubResource();
101✔
52

53
        $this->buyableTypes([
101✔
54
            Product::getProxiedClass(),
101✔
55
            Variant::getProxiedClass(),
101✔
56
        ]);
101✔
57

58
        $this->buyableSearchableColumns([
101✔
59
            Product::getProxiedClass() => ['id', 'name', 'slug', 'description'],
101✔
60
            Variant::getProxiedClass() => ['id', 'alias', 'description'],
101✔
61
        ]);
101✔
62
    }
63

64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function fields(Request $request): array
101✔
68
    {
69
        return [
101✔
70
            MorphTo::make(__('Buyable Item'), 'buyable')
101✔
71
                ->required()
101✔
72
                ->async()
101✔
73
                ->searchable(columns: $this->buyableSearchableColumns)
101✔
74
                ->types($this->buyableTypes)
101✔
75
                ->display(function (Model $buyable) use ($request): string {
101✔
76
                    return $this->resolveBuyableItemDisplay($request, $buyable);
×
77
                }),
101✔
78

79
            Text::make(__('Name'), 'name')
101✔
80
                ->required(),
101✔
81

82
            Number::make(__('Price'), 'price')
101✔
83
                ->min(0)
101✔
84
                ->required()
101✔
85
                ->format(static function (Request $request, Model $model, ?float $value): string {
101✔
86
                    return $model->checkoutable->getCurrency()->format($value ?? 0);
×
87
                }),
101✔
88

89
            Number::make(__('Tax'), function (Request $request, Model $model): float {
101✔
90
                return $model->getTax();
×
91
            })->format(static function (Request $request, Model $model, float $value): string {
101✔
92
                return $model->checkoutable->getCurrency()->format($value ?? 0);
×
93
            }),
101✔
94

95
            Number::make(__('Quantity'), 'quantity')
101✔
96
                ->required()
101✔
97
                ->default(1)
101✔
98
                ->rules(['required', 'numeric', 'gt:0'])
101✔
99
                ->min(0),
101✔
100

101
            Number::make(__('Total'), function (Request $request, Model $model): float {
101✔
102
                return $model->getTotal();
×
103
            })->format(static function (Request $request, Model $model, float $value): string {
101✔
104
                return $model->checkoutable->getCurrency()->format($value ?? 0);
×
105
            }),
101✔
106
        ];
101✔
107
    }
108

109
    /**
110
     * Set the buyable item display resolver.
111
     */
112
    public function displayBuyableItem(Closure $callback): static
×
113
    {
114
        $this->buyableItemDisplayResolver = $callback;
×
115

116
        return $this;
×
117
    }
118

119
    /**
120
     * Resolve the buyable item display.
121
     */
122
    public function resolveBuyableItemDisplay(Request $request, Model $buyable): string
×
123
    {
124
        $callback = $this->buyableItemDisplayResolver ?: static function (Request $request, Model $buyable): string {
×
125
            return (string) match ($buyable::class) {
×
126
                Product::getProxiedClass() => $buyable->name,
×
127
                Variant::getProxiedClass() => $buyable->alias,
×
128
                default => $buyable->getKey(),
×
129
            };
×
130
        };
×
131

132
        return call_user_func_array($callback, [$request, $buyable]);
×
133
    }
134

135
    /**
136
     * Set the buyable types.
137
     */
138
    public function buyableTypes(array $types): static
101✔
139
    {
140
        $this->buyableTypes = array_merge($this->buyableTypes, $types);
101✔
141

142
        return $this;
101✔
143
    }
144

145
    /**
146
     * Set the buyable searchable columns.
147
     */
148
    public function buyableSearchableColumns(array $columns): static
101✔
149
    {
150
        $this->buyableSearchableColumns = array_merge($this->buyableSearchableColumns, $columns);
101✔
151

152
        return $this;
101✔
153
    }
154
}
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