blessing-skin-server/app/Services/Option.php

77 lines
1.7 KiB
PHP
Raw Normal View History

2019-03-23 15:44:16 +08:00
<?php
namespace App\Services;
use DB;
use Illuminate\Support\Arr;
use Illuminate\Database\QueryException;
class Option
{
protected $items;
public function __construct()
{
try {
$this->items = DB::table('options')->get()->mapWithKeys(function ($item) {
return [$item->option_name => $item->option_value];
});
} catch (QueryException $e) {
$this->items = collect();
}
}
public function get($key, $default = null, $raw = false)
{
if (! $this->items->has($key) && Arr::has(config('options'), $key)) {
$this->set($key, config("options.$key"));
}
$value = $this->items->get($key, $default);
if ($raw) {
return $value;
}
switch (strtolower($value)) {
case 'true':
case '(true)':
return true;
case 'false':
case '(false)':
return false;
case 'null':
case '(null)':
return null;
default:
return $value;
}
}
public function set($key, $value = null)
{
if (is_array($key)) {
foreach ($key as $k => $v) {
$this->set($k, $v);
}
} else {
$this->items->put($key, $value);
try {
DB::table('options')->updateOrInsert(
['option_name' => $key],
['option_value' => $value]
);
} catch (QueryException $e) {
//
}
}
}
public function has($key)
{
return $this->items->has($key);
}
}