Files
breadcrumb-the-shire/lib/Repository/Settings/SettingRepository.php

37 lines
1.1 KiB
PHP
Raw Normal View History

2026-02-04 23:31:53 +01:00
<?php
2026-02-11 19:28:12 +01:00
namespace MintyPHP\Repository\Settings;
2026-02-04 23:31:53 +01:00
use MintyPHP\DB;
/** Reads and upserts application settings as key-value pairs in the database. */
2026-03-05 08:26:51 +01:00
class SettingRepository implements SettingRepositoryInterface
2026-02-04 23:31:53 +01:00
{
2026-02-23 12:58:19 +01:00
public function find(string $key): ?array
2026-02-04 23:31:53 +01:00
{
$row = DB::selectOne('select `key`, `value`, `description`, `updated_at` from settings where `key` = ? limit 1', $key);
if (!$row) {
return null;
}
return $row['settings'] ?? $row;
}
2026-02-23 12:58:19 +01:00
public function getValue(string $key): ?string
2026-02-04 23:31:53 +01:00
{
2026-02-23 12:58:19 +01:00
$row = $this->find($key);
2026-02-04 23:31:53 +01:00
if (!$row) {
return null;
}
return array_key_exists('value', $row) ? (string) $row['value'] : null;
}
2026-02-23 12:58:19 +01:00
public function set(string $key, ?string $value, ?string $description = null): bool
2026-02-04 23:31:53 +01:00
{
$query = 'insert into settings (`key`, `value`, `description`) values (?, ?, ?) '
. 'on duplicate key update `value` = values(`value`), '
. '`description` = ifnull(values(`description`), `description`)';
$result = DB::update($query, $key, $value, $description);
return $result !== false;
}
}