Skip to content

Commit 7976353

Browse files
committed
Create default value modifier
1 parent a9a21ae commit 7976353

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

src/Concerns/HasModifiers.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Support\Str;
77
use SimonHamp\LaravelNovaCsvImport\Contracts\Modifier;
88
use SimonHamp\LaravelNovaCsvImport\Modifiers\Boolean;
9+
use SimonHamp\LaravelNovaCsvImport\Modifiers\DefaultValue;
910
use SimonHamp\LaravelNovaCsvImport\Modifiers\ExcelDate;
1011
use SimonHamp\LaravelNovaCsvImport\Modifiers\Hash;
1112
use SimonHamp\LaravelNovaCsvImport\Modifiers\Prefix;
@@ -23,6 +24,7 @@ protected function bootHasModifiers()
2324
// Register built-in modifiers
2425
static::registerModifiers(
2526
new Boolean,
27+
new DefaultValue,
2628
new ExcelDate,
2729
new StrModifier,
2830
new Hash,

src/Modifiers/DefaultValue.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace SimonHamp\LaravelNovaCsvImport\Modifiers;
4+
5+
use SimonHamp\LaravelNovaCsvImport\Contracts\Modifier;
6+
7+
class DefaultValue implements Modifier
8+
{
9+
public function title(): string
10+
{
11+
return 'Default Value';
12+
}
13+
14+
public function description(): string
15+
{
16+
return "Set a default value for the field if the CSV column is empty or missing";
17+
}
18+
19+
public function settings(): array
20+
{
21+
return [
22+
'string' => [
23+
'type' => 'string',
24+
'title' => 'Default Value',
25+
],
26+
];
27+
}
28+
29+
public function handle($value = null, array $settings = []): string
30+
{
31+
return empty($value) ? $settings['string'] : $value;
32+
}
33+
}

0 commit comments

Comments
 (0)