Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Oct 11, 2024
1 parent 1fe87f7 commit a1a690a
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 92 deletions.
42 changes: 0 additions & 42 deletions src/Casts/SettingValue.php

This file was deleted.

26 changes: 0 additions & 26 deletions src/Http/Controllers/SettingController.php

This file was deleted.

11 changes: 0 additions & 11 deletions src/Models/Setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Cone\Root\Models;

use Cone\Root\Casts\SettingValue;
use Cone\Root\Database\Factories\SettingFactory;
use Cone\Root\Interfaces\Models\Setting as Contract;
use Cone\Root\Traits\InteractsWithProxy;
Expand Down Expand Up @@ -46,14 +45,4 @@ protected static function newFactory(): SettingFactory
{
return SettingFactory::new();
}

/**
* Get the attributes that should be cast.
*/
protected function casts(): array
{
return [
'value' => SettingValue::class.':'.$this->key,
];
}
}
113 changes: 100 additions & 13 deletions src/Settings/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,127 @@

namespace Cone\Root\Settings;

use ArrayAccess;
use Cone\Root\Interfaces\Settings\Repository as Contract;
use Cone\Root\Models\Setting;
use Illuminate\Contracts\Support\Arrayable;

class Repository implements Contract
class Repository implements Arrayable, ArrayAccess, Contract
{
/**
* The repository cache.
*/
protected array $cache = [];

public function __construct()
/**
* Get the setting model.
*/
public function model(): Setting
{
//
return Setting::proxy();
}

public function model(): Setting
/**
* Get the value for the given key.
*/
public function get(string $key, mixed $default = null, bool $fresh = false): mixed
{
return Setting::proxy();
if ($this->offsetExists($key) && ! $fresh) {
return $this->offsetGet($key);
}

$model = $this->model()->newQuery()->firstWhere('key', '=', $key);

if (! is_null($model)) {
$this->offsetSet($key, $model->value);
}

return $this->cache[$key] ?? $default;
}

/**
* Set the value for the given key.
*/
public function set(string $key, mixed $value): void
{
$model = $this->model()->newQuery()->updateOrCreate(
['key' => $key],
['value' => $value]
);

$this->offsetSet($key, $model->value);
}

/**
* Delete the given keys.
*/
public function delete(string|array $keys): void
{
foreach ((array) $keys as $key) {
$this->offsetUnset($key);
}

$this->model()->newQuery()->whereIn('key', (array) $keys)->delete();
}

/**
* Flush the cache.
*/
public function flush(): void
{
$this->cache = [];
}

/**
* Convert the repository to an array.
*/
public function toArray(): array
{
return $this->cache;
}

public function get()
/**
* Determine if an item exists at an offset.
*
* @param TKey $key
*/
public function offsetExists($key): bool
{
//
return isset($this->cache[$key]);
}

public function set()
/**
* Get an item at a given offset.
*
* @param TKey $key
*/
public function offsetGet($key): mixed
{
//
return $this->cache[$key];
}

public function delete()
/**
* Set the item at a given offset.
*
* @param TKey|null $key
* @param TValue $value
*/
public function offsetSet($key, $value): void
{
//
if (is_null($key)) {
$this->cache[] = $value;
} else {
$this->cache[$key] = $value;
}
}

public function cast(string $key, string $type)
/**
* Unset the item at a given offset.
*
* @param TKey $key
*/
public function offsetUnset($key): void
{
//
unset($this->cache[$key]);
}
}

0 comments on commit a1a690a

Please sign in to comment.