-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5c8bf44
commit 77dc88a
Showing
3 changed files
with
88 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |