Skip to content

Commit 1b91f7f

Browse files
author
Roelof Roos
committed
Fixed NovaEditorJsCast mistreating JSON
1 parent daa063b commit 1b91f7f

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

src/NovaEditorJsCast.php

+11-9
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,16 @@ private static function getErrorObject(string $exceptionMessage): NovaEditorJsDa
4747
* @param array $attributes
4848
* @return NovaEditorJsData|null
4949
*/
50-
public function get($model, string $key, $value, array $attributes)
50+
public function get($model, string $key, $value, array $attributes): ?NovaEditorJsData
5151
{
52-
if ($value === null) {
53-
return null;
54-
}
55-
5652
try {
57-
return new NovaEditorJsData(json_decode($value, true, 512, JSON_THROW_ON_ERROR));
53+
// Recursively decode JSON, to solve a bug where the JSON is double-encoded.
54+
while (is_string($value) && ! empty($value)) {
55+
$value = json_decode($value, true, 512, JSON_THROW_ON_ERROR);
56+
}
57+
58+
// Return null if the new value is null
59+
return $value === null ? null : new NovaEditorJsData($value);
5860
} catch (JsonException $exception) {
5961
return self::getErrorObject($exception->getMessage());
6062
}
@@ -67,9 +69,9 @@ public function get($model, string $key, $value, array $attributes)
6769
* @param string $key
6870
* @param mixed $value
6971
* @param array $attributes
70-
* @return mixed
72+
* @return array
7173
*/
72-
public function set($model, string $key, $value, array $attributes)
74+
public function set($model, string $key, $value, array $attributes): array
7375
{
7476
if ($value === null) {
7577
return [
@@ -83,7 +85,7 @@ public function set($model, string $key, $value, array $attributes)
8385
}
8486

8587
return [
86-
$key => json_encode($value),
88+
$key => is_string($value) ? $value : json_encode($value),
8789
];
8890
}
8991
}

0 commit comments

Comments
 (0)