Skip to content

Commit

Permalink
Add support for Dictionary fields
Browse files Browse the repository at this point in the history
  • Loading branch information
duncanmcclean committed Nov 18, 2024
1 parent 5c8bf44 commit 77dc88a
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@

class ServiceProvider extends AddonServiceProvider
{
public array $transformers = [
protected array $transformers = [
'assets' => Transformers\AssetsTransformer::class,
'bard' => Transformers\BardTransformer::class,
'date' => Transformers\DateTransformer::class,
'dictionary' => Transformers\DictionaryTransformer::class,
'entries' => Transformers\EntriesTransformer::class,
'terms' => Transformers\TermsTransformer::class,
'toggle' => Transformers\ToggleTransformer::class,
Expand Down
35 changes: 35 additions & 0 deletions src/Transformers/DictionaryTransformer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Statamic\Importer\Transformers;

use Statamic\Dictionaries\Dictionary;
use Statamic\Facades;
use Statamic\Support\Str;

class DictionaryTransformer extends AbstractTransformer
{
public function transform(string $value): null|string|array
{
// When $value is a JSON string, decode it.
if (Str::startsWith($value, ['{', '[']) || Str::startsWith($value, ['[', ']'])) {
$value = collect(json_decode($value, true))->join('|');
}

$options = collect(explode('|', $value))->map(function ($value) {
return $this->dictionary()->get($value)?->value();
})->filter()->values();

return $this->field->get('max_items') === 1 ? $options->first() : $options->all();
}

private function dictionary(): Dictionary
{
$dictionary = $this->field->get('dictionary');

if (is_string($dictionary)) {
return Facades\Dictionary::find($dictionary);
}

return Facades\Dictionary::find($dictionary['type']);
}
}
51 changes: 51 additions & 0 deletions tests/Transformers/DictionaryTransformerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Statamic\Importer\Tests\Transformers;

use PHPUnit\Framework\Attributes\Test;
use Statamic\Facades\Collection;
use Statamic\Facades\User;
use Statamic\Importer\Facades\Import;
use Statamic\Importer\Tests\TestCase;
use Statamic\Importer\Transformers\DictionaryTransformer;
use Statamic\Importer\Transformers\UsersTransformer;
use Statamic\Testing\Concerns\PreventsSavingStacheItemsToDisk;

class DictionaryTransformerTest extends TestCase
{
use PreventsSavingStacheItemsToDisk;

public $collection;
public $blueprint;
public $field;
public $import;

public function setUp(): void
{
parent::setUp();

$this->collection = tap(Collection::make('pages'))->save();

$this->blueprint = $this->collection->entryBlueprint();
$this->blueprint->ensureField('countries', ['type' => 'dictionary', 'dictionary' => 'countries'])->save();

$this->field = $this->blueprint->field('countries');

$this->import = Import::make();
}

#[Test]
public function it_returns_keys()
{
$transformer = new DictionaryTransformer(
import: $this->import,
blueprint: $this->blueprint,
field: $this->field,
config: []
);

$output = $transformer->transform('USA|CAN|FOO|DEU|GBR');

$this->assertEquals(['USA', 'CAN', 'DEU', 'GBR'], $output);
}
}

0 comments on commit 77dc88a

Please sign in to comment.