Skip to content

Commit 815a76e

Browse files
Merge pull request #3 from rodrigopedra/main
Allow to customize label and value array keys
2 parents 86ffeaf + 327a205 commit 815a76e

8 files changed

+56
-9
lines changed

config/options.php

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
return [
4+
5+
/*
6+
* By default the when serializing an option to an array
7+
* the key `label` is used for the instance's label
8+
* and the key `value` for the instance's value
9+
*
10+
* You can customize these keys here.
11+
*/
12+
'label_key' => 'label',
13+
'value_key' => 'value',
14+
15+
];

src/Options.php

-2
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@
55
use Closure;
66
use Exception;
77
use Illuminate\Contracts\Support\Arrayable;
8-
use Illuminate\Contracts\Support\Htmlable;
98
use Illuminate\Contracts\Support\Jsonable;
109
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
1110
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
1211
use Illuminate\Database\Eloquent\Model;
1312
use Illuminate\Database\Eloquent\Relations\Relation;
14-
use Illuminate\Support\Arr;
1513
use Illuminate\Support\Collection;
1614
use Illuminate\Validation\Rule;
1715
use Illuminate\Validation\Rules\In;

src/OptionsServiceProvider.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class OptionsServiceProvider extends PackageServiceProvider
99
{
1010
public function configurePackage(Package $package): void
1111
{
12-
$package->name('options');
12+
$package->name('options')
13+
->hasConfigFile();
1314
}
1415
}

src/Providers/ArrayProvider.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ public function provide(): Collection
2525
}
2626

2727
return $items->map(fn ($label, $value) => [
28-
'label' => $label,
29-
'value' => $value,
28+
config('options.label_key') => $label,
29+
config('options.value_key') => $value,
3030
]);
3131
}
3232

3333
public function map(mixed $item): SelectOption
3434
{
35-
['label' => $label, 'value' => $value] = $item;
35+
[config('options.label_key') => $label, config('options.value_key') => $value] = $item;
3636

3737
return new SelectOption($label, $value);
3838
}

src/SelectOption.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ public static function create(string $label, string | int $value, array $extra =
1919
public function toArray(): array
2020
{
2121
return array_merge([
22-
'label' => $this->label,
23-
'value' => $this->value,
22+
config('options.label_key') => $this->label,
23+
config('options.value_key') => $this->value,
2424
], $this->extra);
2525
}
2626

tests/OptionsTest.php

+17
Original file line numberDiff line numberDiff line change
@@ -211,3 +211,20 @@
211211
->toHaveCount(2)
212212
->toEqual([new In(['frodo', 'sam', 'merry', 'pippin']), 'nullable']);
213213
});
214+
215+
it('can use custom keys', function () {
216+
config([
217+
'options.label_key' => 'name',
218+
'options.value_key' => 'id',
219+
]);
220+
221+
$options = Options::forProvider(new NativeEnumProvider(StringEnum::class))
222+
->toArray();
223+
224+
expect($options)->toBeArray()->toBe([
225+
['name' => 'Frodo', 'id' => 'frodo'],
226+
['name' => 'Sam', 'id' => 'sam'],
227+
['name' => 'Merry', 'id' => 'merry'],
228+
['name' => 'Pippin', 'id' => 'pippin'],
229+
]);
230+
});

tests/Providers/ArrayProviderTest.php

+17
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,20 @@
3434
['label' => 'Pippin', 'value' => 'Pippin'],
3535
]);
3636
});
37+
38+
it('can map options from an array with custom keys', function () {
39+
config([
40+
'options.label_key' => 'name',
41+
'options.value_key' => 'id',
42+
]);
43+
44+
$option = (new ArrayProvider([]))->map([
45+
'name' => 'Frodo',
46+
'id' => 'frodo',
47+
]);
48+
49+
expect($option->toArray())->toBeArray()->toBe([
50+
'name' => 'Frodo',
51+
'id' => 'frodo',
52+
]);
53+
});

tests/Providers/EmptyProviderTest.php

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?php
22

33
use Spatie\LaravelOptions\Options;
4-
use Spatie\LaravelOptions\Providers\ArrayProvider;
54
use Spatie\LaravelOptions\Providers\EmptyProvider;
65

76
it('can have empty options', function () {

0 commit comments

Comments
 (0)