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

conedevelopment / root / 15084089635

17 May 2025 10:00AM UTC coverage: 77.93% (+0.04%) from 77.891%
15084089635

push

github

web-flow
Modernize back-end.yml (#240)

3291 of 4223 relevant lines covered (77.93%)

36.04 hits per line

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

71.67
/src/Fields/Select.php
1
<?php
2

3
namespace Cone\Root\Fields;
4

5
use BackedEnum;
6
use Closure;
7
use Cone\Root\Filters\Filter;
8
use Cone\Root\Filters\RenderableFilter;
9
use Illuminate\Database\Eloquent\Builder;
10
use Illuminate\Database\Eloquent\Model;
11
use Illuminate\Http\Request;
12
use Illuminate\Support\Arr;
13
use Illuminate\Support\Collection;
14

15
class Select extends Field
16
{
17
    /**
18
     * The Blade template.
19
     */
20
    protected string $template = 'root::fields.select';
21

22
    /**
23
     * The options resolver callback.
24
     */
25
    protected ?Closure $optionsResolver = null;
26

27
    /**
28
     * Indicates if the field should be nullable.
29
     */
30
    protected bool $nullable = false;
31

32
    /**
33
     * Set the nullable attribute.
34
     */
35
    public function nullable(bool $value = true): static
1✔
36
    {
37
        $this->nullable = $value;
1✔
38

39
        return $this;
1✔
40
    }
41

42
    /**
43
     * Determine if the field is nullable.
44
     */
45
    public function isNullable(): bool
3✔
46
    {
47
        return $this->nullable;
3✔
48
    }
49

50
    /**
51
     * Set the "multiple" HTML attribute.
52
     */
53
    public function multiple(bool $value = true): static
2✔
54
    {
55
        return $this->setAttribute('multiple', $value);
2✔
56
    }
57

58
    /**
59
     * Set the "size" HTML attribute.
60
     */
61
    public function size(int $value): static
1✔
62
    {
63
        return $this->setAttribute('size', $value);
1✔
64
    }
65

66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function resolveFormat(Request $request, Model $model): ?string
4✔
70
    {
71
        if (is_null($this->formatResolver)) {
4✔
72
            $this->formatResolver = function (Request $request, Model $model, mixed $value): string {
4✔
73
                $options = array_column(
4✔
74
                    $this->resolveOptions($request, $model), 'label', 'value'
4✔
75
                );
4✔
76

77
                return Collection::make($value)
4✔
78
                    ->map(static function (mixed $value) use ($options): string {
4✔
79
                        $value = $value instanceof BackedEnum ? $value->value : $value;
2✔
80

81
                        return $options[$value] ?? $value;
2✔
82
                    })
4✔
83
                    ->implode(', ');
4✔
84
            };
4✔
85
        }
86

87
        return parent::resolveFormat($request, $model);
4✔
88
    }
89

90
    /**
91
     * Set the options attribute.
92
     */
93
    public function options(array|Closure $value): static
198✔
94
    {
95
        $this->optionsResolver = is_callable($value) ? $value : static fn (): array => $value;
198✔
96

97
        return $this;
198✔
98
    }
99

100
    /**
101
     * Resolve the options for the field.
102
     */
103
    public function resolveOptions(Request $request, Model $model): array
5✔
104
    {
105
        if (is_null($this->optionsResolver)) {
5✔
106
            return [];
2✔
107
        }
108

109
        $options = call_user_func_array($this->optionsResolver, [$request, $model]);
4✔
110

111
        $value = Arr::wrap($this->resolveValue($request, $model));
4✔
112

113
        return array_map(function (mixed $label, mixed $option) use ($value): array {
4✔
114
            $option = $label instanceof Option ? $label : $this->newOption($option, $label);
4✔
115

116
            $option->selected(in_array($option->getAttribute('value'), $value));
4✔
117

118
            return $option->toArray();
4✔
119
        }, $options, array_keys($options));
4✔
120
    }
121

122
    /**
123
     * Make a new option instance.
124
     */
125
    public function newOption(mixed $value, string $label): Option
6✔
126
    {
127
        return new Option($value, $label);
6✔
128
    }
129

130
    /**
131
     * {@inheritdoc}
132
     */
133
    public function toInput(Request $request, Model $model): array
2✔
134
    {
135
        return array_merge(parent::toInput($request, $model), [
2✔
136
            'nullable' => $this->isNullable(),
2✔
137
            'options' => $this->resolveOptions($request, $model),
2✔
138
        ]);
2✔
139
    }
140

141
    /**
142
     * Get the filter representation of the field.
143
     */
144
    public function toFilter(): Filter
×
145
    {
146
        return new class($this) extends RenderableFilter
×
147
        {
×
148
            protected Select $field;
149

150
            public function __construct(Select $field)
151
            {
152
                parent::__construct($field->getModelAttribute());
×
153

154
                $this->field = $field;
×
155
            }
156

157
            public function apply(Request $request, Builder $query, mixed $value): Builder
158
            {
159
                return $this->field->resolveFilterQuery($request, $query, $value);
×
160
            }
161

162
            public function toField(): Field
163
            {
164
                return Select::make($this->field->getLabel(), $this->getRequestKey())
×
165
                    ->value(fn (Request $request): mixed => $this->getValue($request))
×
166
                    ->nullable()
×
167
                    ->options(function (Request $request, Model $model): array {
×
168
                        return array_column(
×
169
                            $this->field->resolveOptions($request, $model),
×
170
                            'label',
×
171
                            'value',
×
172
                        );
×
173
                    });
×
174
            }
175
        };
×
176
    }
177
}
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