Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON encoding / decoding customization for JSON based cass #46552

Merged
merged 3 commits into from
Mar 24, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Illuminate/Database/Eloquent/Casts/AsArrayObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ public function get($model, $key, $value, $attributes)
return;
}

$data = json_decode($attributes[$key], true);
$data = Json::decode($attributes[$key]);

return is_array($data) ? new ArrayObject($data) : null;
}

public function set($model, $key, $value, $attributes)
{
return [$key => json_encode($value)];
return [$key => Json::encode($value)];
}

public function serialize($model, string $key, $value, array $attributes)
Expand Down
4 changes: 2 additions & 2 deletions src/Illuminate/Database/Eloquent/Casts/AsCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ public function get($model, $key, $value, $attributes)
return;
}

$data = json_decode($attributes[$key], true);
$data = Json::decode($attributes[$key]);

return is_array($data) ? new Collection($data) : null;
}

public function set($model, $key, $value, $attributes)
{
return [$key => json_encode($value)];
return [$key => Json::encode($value)];
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static function castUsing(array $arguments)
public function get($model, $key, $value, $attributes)
{
if (isset($attributes[$key])) {
return new ArrayObject(json_decode(Crypt::decryptString($attributes[$key]), true));
return new ArrayObject(Json::decode(Crypt::decryptString($attributes[$key])));
}

return null;
Expand All @@ -30,7 +30,7 @@ public function get($model, $key, $value, $attributes)
public function set($model, $key, $value, $attributes)
{
if (! is_null($value)) {
return [$key => Crypt::encryptString(json_encode($value))];
return [$key => Crypt::encryptString(Json::encode($value))];
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static function castUsing(array $arguments)
public function get($model, $key, $value, $attributes)
{
if (isset($attributes[$key])) {
return new Collection(json_decode(Crypt::decryptString($attributes[$key]), true));
return new Collection(Json::decode(Crypt::decryptString($attributes[$key])));
}

return null;
Expand All @@ -31,7 +31,7 @@ public function get($model, $key, $value, $attributes)
public function set($model, $key, $value, $attributes)
{
if (! is_null($value)) {
return [$key => Crypt::encryptString(json_encode($value))];
return [$key => Crypt::encryptString(Json::encode($value))];
}

return null;
Expand Down
4 changes: 2 additions & 2 deletions src/Illuminate/Database/Eloquent/Casts/AsEnumArrayObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function get($model, $key, $value, $attributes)
return;
}

$data = json_decode($attributes[$key], true);
$data = Json::decode($attributes[$key]);

if (! is_array($data)) {
return;
Expand All @@ -61,7 +61,7 @@ public function set($model, $key, $value, $attributes)
$storable[] = $this->getStorableEnumValue($enum);
}

return [$key => json_encode($storable)];
return [$key => Json::encode($storable)];
}

public function serialize($model, string $key, $value, array $attributes)
Expand Down
6 changes: 3 additions & 3 deletions src/Illuminate/Database/Eloquent/Casts/AsEnumCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function get($model, $key, $value, $attributes)
return;
}

$data = json_decode($attributes[$key], true);
$data = Json::decode($attributes[$key]);

if (! is_array($data)) {
return;
Expand All @@ -52,9 +52,9 @@ public function get($model, $key, $value, $attributes)
public function set($model, $key, $value, $attributes)
{
$value = $value !== null
? (new Collection($value))->map(function ($enum) {
? Json::encode((new Collection($value))->map(function ($enum) {
return $this->getStorableEnumValue($enum);
})->toJson()
})->jsonSerialize())
: null;

return [$key => $value];
Expand Down
54 changes: 54 additions & 0 deletions src/Illuminate/Database/Eloquent/Casts/Json.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace Illuminate\Database\Eloquent\Casts;

class Json
{
/**
* The custom JSON encoder.
*
* @var callable|null
*/
protected $encoder;

/**
* The custom JSON decode.
*
* @var callable|null
*/
protected $decoder;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@taylorotwell I guess these need to become static?


/**
* Encode the given value.
*/
public static function encode(mixed $value): mixed
{
return isset(static::$encoder) ? (static::$encoder)($value) : json_encode($value);
}

/**
* Decode the given value.
*/
public static function decode(mixed $value, ?bool $associative = true): mixed
{
return isset(static::$decoder)
? (static::$decoder)($value, $associative)
: json_decode($value, $associative);
}

/**
* Encode all values using the given callable.
*/
public static function encodeUsing(?callable $encoder): void
{
static::$encoder = $encoder;
}

/**
* Decode all values using the given callable.
*/
public static function decodeUsing(?callable $decoder): void
{
static::$decoder = $decoder;
}
}
5 changes: 3 additions & 2 deletions src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Illuminate\Database\Eloquent\Casts\AsEncryptedArrayObject;
use Illuminate\Database\Eloquent\Casts\AsEncryptedCollection;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Casts\Json;
use Illuminate\Database\Eloquent\InvalidCastException;
use Illuminate\Database\Eloquent\JsonEncodingException;
use Illuminate\Database\Eloquent\MissingAttributeException;
Expand Down Expand Up @@ -1241,7 +1242,7 @@ protected function castAttributeAsJson($key, $value)
*/
protected function asJson($value)
{
return json_encode($value);
return Json::encode($value);
}

/**
Expand All @@ -1253,7 +1254,7 @@ protected function asJson($value)
*/
public function fromJson($value, $asObject = false)
{
return json_decode($value ?? '', ! $asObject);
return Json::decode($value ?? '', ! $asObject);
}

/**
Expand Down