Skip to content

Commit

Permalink
Fixed NovaEditorJsCast mistreating JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
Roelof Roos committed Jul 17, 2022
1 parent 157929c commit 28b7830
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions src/NovaEditorJsCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand All @@ -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 [
Expand All @@ -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),
];
}
}

0 comments on commit 28b7830

Please sign in to comment.