From d88e116e20b90483c9d422a44bec84bfd7c1faf1 Mon Sep 17 00:00:00 2001 From: Alex <21161486+alex552@users.noreply.github.com> Date: Sat, 11 Feb 2023 20:03:15 +0100 Subject: [PATCH] feat: add option for alternative column names for auto matching & tests --- src/Actions/ImportAction.php | 12 ++++++-- src/Actions/ImportField.php | 2 ++ src/Concerns/HasColumnMatching.php | 20 +++++++++++++ tests/Features/ImportTest.php | 17 +++++++++++ .../Pages/AlternativeColumnsNamesList.php | 29 +++++++++++++++++++ 5 files changed, 77 insertions(+), 3 deletions(-) create mode 100644 src/Concerns/HasColumnMatching.php create mode 100644 tests/Resources/Pages/AlternativeColumnsNamesList.php diff --git a/src/Actions/ImportAction.php b/src/Actions/ImportAction.php index 8c5a3ba..68f7cc7 100644 --- a/src/Actions/ImportAction.php +++ b/src/Actions/ImportAction.php @@ -168,19 +168,25 @@ private function getFields(ImportField|Field $field): Field $options = $this->cachedHeadingOptions; if (count($options) == 0) { - $options = $this->toCollection($filePath)->first()?->first()->filter(fn ($value) => $value != null)->toArray(); + $options = $this->toCollection($filePath)->first()?->first()->filter(fn ($value) => $value != null)->map('trim')->toArray(); } $selected = array_search($field->getName(), $options); - if ($selected != false) { + + if ($selected !== false) { $set($field->getName(), $selected); + } elseif (! empty($field->getAlternativeColumnNames())) { + $alternativeNames = array_intersect($field->getAlternativeColumnNames(), $options); + if (count($alternativeNames) > 0) { + $set($field->getName(), array_search(current($alternativeNames), $options)); + } } return $options; }); } - public function handleRecordCreation(Closure $closure) + public function handleRecordCreation(Closure $closure): static { $this->handleRecordCreation = $closure; $this->massCreate(false); diff --git a/src/Actions/ImportField.php b/src/Actions/ImportField.php index b02eacd..8c05bd8 100644 --- a/src/Actions/ImportField.php +++ b/src/Actions/ImportField.php @@ -2,6 +2,7 @@ namespace Konnco\FilamentImport\Actions; +use Konnco\FilamentImport\Concerns\HasColumnMatching; use Konnco\FilamentImport\Concerns\HasFieldHelper; use Konnco\FilamentImport\Concerns\HasFieldLabel; use Konnco\FilamentImport\Concerns\HasFieldMutation; @@ -17,6 +18,7 @@ class ImportField use HasFieldLabel; use HasFieldRequire; use HasFieldValidation; + use HasColumnMatching; public function __construct(private string $name) { diff --git a/src/Concerns/HasColumnMatching.php b/src/Concerns/HasColumnMatching.php new file mode 100644 index 0000000..7eb83bc --- /dev/null +++ b/src/Concerns/HasColumnMatching.php @@ -0,0 +1,20 @@ +alternativeColumnNames = $alternativeColumnNames; + + return $this; + } + + public function getAlternativeColumnNames(): array + { + return $this->alternativeColumnNames; + } +} diff --git a/tests/Features/ImportTest.php b/tests/Features/ImportTest.php index a7b046a..932ce07 100644 --- a/tests/Features/ImportTest.php +++ b/tests/Features/ImportTest.php @@ -1,6 +1,7 @@ mountPageAction('import') + ->setPageActionData([ + 'file' => [$file->store('file')], + 'fileRealPath' => $file->getRealPath(), + 'skipHeader' => false, + ]) + ->assertPageActionDataSet(['title' => 0, 'slug' => 1, 'body' => 2]) + ->callMountedPageAction() + ->assertHasNoPageActionErrors() + ->assertSuccessful(); + + assertDatabaseCount(Post::class, 11); +}); + //it('can manipulate single field', function () { // expect(true)->toBeTrue(); //}); diff --git a/tests/Resources/Pages/AlternativeColumnsNamesList.php b/tests/Resources/Pages/AlternativeColumnsNamesList.php new file mode 100644 index 0000000..e1019cf --- /dev/null +++ b/tests/Resources/Pages/AlternativeColumnsNamesList.php @@ -0,0 +1,29 @@ +fields([ + ImportField::make('title')->alternativeColumnNames(['Title']), + ImportField::make('slug')->alternativeColumnNames(['Slug']), + ImportField::make('body')->alternativeColumnNames(['Body']), + ]) + ->handleRecordCreation(function ($data) { + return Post::create($data); + }), + ]; + } +}