diff --git a/src/NovaEditorJsCast.php b/src/NovaEditorJsCast.php index 3601c28..9490ed4 100644 --- a/src/NovaEditorJsCast.php +++ b/src/NovaEditorJsCast.php @@ -47,14 +47,16 @@ private static function getErrorObject(string $exceptionMessage): NovaEditorJsDa * @param array $attributes * @return NovaEditorJsData|null */ - public function get($model, string $key, $value, array $attributes) + public function get($model, string $key, $value, array $attributes): ?NovaEditorJsData { - if ($value === null) { - return null; - } - try { - return new NovaEditorJsData(json_decode($value, true, 512, JSON_THROW_ON_ERROR)); + // Recursively decode JSON, to solve a bug where the JSON is double-encoded. + while (is_string($value) && ! empty($value)) { + $value = json_decode($value, true, 512, JSON_THROW_ON_ERROR); + } + + // Return null if the new value is null + return $value === null ? null : new NovaEditorJsData($value); } catch (JsonException $exception) { return self::getErrorObject($exception->getMessage()); } @@ -67,9 +69,9 @@ public function get($model, string $key, $value, array $attributes) * @param string $key * @param mixed $value * @param array $attributes - * @return mixed + * @return array */ - public function set($model, string $key, $value, array $attributes) + public function set($model, string $key, $value, array $attributes): array { if ($value === null) { return [ @@ -83,7 +85,7 @@ public function set($model, string $key, $value, array $attributes) } return [ - $key => json_encode($value), + $key => is_string($value) ? $value : json_encode($value), ]; } }