Skip to content

Commit

Permalink
Revert "Use Illuminate\Database\Eloquent\Casts\Json if possible"
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenvanassche authored Dec 4, 2023
1 parent 9e96701 commit 93ff4df
Showing 1 changed file with 4 additions and 19 deletions.
23 changes: 4 additions & 19 deletions src/SettingsRepositories/DatabaseSettingsRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
}
Expand All @@ -49,15 +48,15 @@ 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
{
$this->getBuilder()->create([
'group' => $group,
'name' => $name,
'payload' => $this->encode($payload),
'payload' => json_encode($payload),
'locked' => false,
]);
}
Expand All @@ -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();

Expand Down Expand Up @@ -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);
}
}

0 comments on commit 93ff4df

Please sign in to comment.