From 1d503b87961110070c6a1d9819a1d9fb1dc634bb Mon Sep 17 00:00:00 2001 From: Duncan McClean Date: Tue, 12 Nov 2024 17:00:29 +0000 Subject: [PATCH] Parse JSON arrays as relationship values --- src/Transformers/AssetsTransformer.php | 5 +++++ src/Transformers/EntriesTransformer.php | 5 +++++ src/Transformers/TermsTransformer.php | 5 +++++ src/Transformers/UsersTransformer.php | 6 ++++++ 4 files changed, 21 insertions(+) diff --git a/src/Transformers/AssetsTransformer.php b/src/Transformers/AssetsTransformer.php index bacf3ca..58b72e0 100644 --- a/src/Transformers/AssetsTransformer.php +++ b/src/Transformers/AssetsTransformer.php @@ -17,6 +17,11 @@ public function transform(string $value): null|string|array $baseUrl = $this->config('base_url'); $relatedField = $this->config('related_field', 'path'); + // When $value is a JSON string, decode it. + if (Str::startsWith($value, ['{', '[']) || Str::startsWith($value, ['[', ']'])) { + $value = collect(json_decode($value, true))->join('|'); + } + $assets = collect(explode('|', $value))->map(function ($path) use ($assetContainer, $relatedField, $baseUrl) { $path = Str::of($path) ->when($relatedField === 'url' && $baseUrl, function ($str) use ($baseUrl) { diff --git a/src/Transformers/EntriesTransformer.php b/src/Transformers/EntriesTransformer.php index 097ffe0..cfd384d 100644 --- a/src/Transformers/EntriesTransformer.php +++ b/src/Transformers/EntriesTransformer.php @@ -17,6 +17,11 @@ public function transform(string $value): null|string|array $value = collect(unserialize($value))->join('|'); } + // When $value is a JSON string, decode it. + if (Str::startsWith($value, ['{', '[']) || Str::startsWith($value, ['[', ']'])) { + $value = collect(json_decode($value, true))->join('|'); + } + if ($this->config('related_field') === 'id') { return is_string($value) ? explode('|', $value) : $value; } diff --git a/src/Transformers/TermsTransformer.php b/src/Transformers/TermsTransformer.php index f8d1e1a..8e54855 100644 --- a/src/Transformers/TermsTransformer.php +++ b/src/Transformers/TermsTransformer.php @@ -11,6 +11,11 @@ class TermsTransformer 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('|'); + } + $terms = collect(explode('|', $value))->map(function ($value) { $term = Term::query() ->whereIn('taxonomy', Arr::wrap($this->field->get('taxonomies'))) diff --git a/src/Transformers/UsersTransformer.php b/src/Transformers/UsersTransformer.php index 900d025..f6cf1e1 100644 --- a/src/Transformers/UsersTransformer.php +++ b/src/Transformers/UsersTransformer.php @@ -3,11 +3,17 @@ namespace Statamic\Importer\Transformers; use Statamic\Facades\User; +use Statamic\Support\Str; class UsersTransformer 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('|'); + } + if ($this->config('related_field') === 'id') { if (is_string($value)) { return explode('|', $value);