From 93ff4dfe974115f171beb2e0a8c95a7eb97d62b2 Mon Sep 17 00:00:00 2001 From: Ruben Van Assche Date: Mon, 4 Dec 2023 12:36:34 +0100 Subject: [PATCH] Revert "Use Illuminate\Database\Eloquent\Casts\Json if possible" --- .../DatabaseSettingsRepository.php | 23 ++++--------------- 1 file changed, 4 insertions(+), 19 deletions(-) diff --git a/src/SettingsRepositories/DatabaseSettingsRepository.php b/src/SettingsRepositories/DatabaseSettingsRepository.php index b40db46..20b0ed9 100644 --- a/src/SettingsRepositories/DatabaseSettingsRepository.php +++ b/src/SettingsRepositories/DatabaseSettingsRepository.php @@ -3,7 +3,6 @@ namespace Spatie\LaravelSettings\SettingsRepositories; use Illuminate\Database\Eloquent\Builder; -use Illuminate\Database\Eloquent\Casts\Json; use Spatie\LaravelSettings\Models\SettingsProperty; class DatabaseSettingsRepository implements SettingsRepository @@ -28,7 +27,7 @@ public function getPropertiesInGroup(string $group): array ->where('group', $group) ->get(['name', 'payload']) ->mapWithKeys(function (object $object) { - return [$object->name => $this->decode($object->payload, true)]; + return [$object->name => json_decode($object->payload, true)]; }) ->toArray(); } @@ -49,7 +48,7 @@ public function getPropertyPayload(string $group, string $name) ->first('payload') ->toArray(); - return $this->decode($setting['payload']); + return json_decode($setting['payload']); } public function createProperty(string $group, string $name, $payload): void @@ -57,7 +56,7 @@ public function createProperty(string $group, string $name, $payload): void $this->getBuilder()->create([ 'group' => $group, 'name' => $name, - 'payload' => $this->encode($payload), + 'payload' => json_encode($payload), 'locked' => false, ]); } @@ -68,7 +67,7 @@ public function updatePropertiesPayload(string $group, array $properties): void return [ 'group' => $group, 'name' => $name, - 'payload' => $this->encode($payload), + 'payload' => json_encode($payload), ]; })->values()->toArray(); @@ -124,18 +123,4 @@ public function getBuilder(): Builder return $model->newQuery(); } - - private function encode(mixed $value): mixed - { - return class_exists(Json::class) - ? Json::encode($value) - : json_encode($value); - } - - private function decode(string $payload, bool $associative = false): mixed - { - return class_exists(Json::class) - ? Json::decode($payload, ! $associative) - : json_decode($payload, $associative); - } }