forked from fa/breadcrumb-the-shire
105 lines
3.5 KiB
PHP
105 lines
3.5 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace MintyPHP\Repository;
|
||
|
|
|
||
|
|
use MintyPHP\DB;
|
||
|
|
|
||
|
|
class PermissionRepository
|
||
|
|
{
|
||
|
|
public static function list(): array
|
||
|
|
{
|
||
|
|
$rows = DB::select('select id, `key`, description, created from permissions order by `key` asc');
|
||
|
|
if (!is_array($rows)) {
|
||
|
|
return [];
|
||
|
|
}
|
||
|
|
$list = [];
|
||
|
|
foreach ($rows as $row) {
|
||
|
|
$data = $row['permissions'] ?? $row;
|
||
|
|
if (is_array($data)) {
|
||
|
|
$list[] = $data;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return $list;
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function listPaged(array $options): array
|
||
|
|
{
|
||
|
|
$limit = (int) ($options['limit'] ?? 10);
|
||
|
|
$offset = (int) ($options['offset'] ?? 0);
|
||
|
|
$search = trim((string) ($options['search'] ?? ''));
|
||
|
|
$order = (string) ($options['order'] ?? 'key');
|
||
|
|
$dir = strtolower((string) ($options['dir'] ?? 'asc')) === 'desc' ? 'desc' : 'asc';
|
||
|
|
|
||
|
|
$allowedOrder = ['key' => '`key`', 'description' => 'description', 'created' => 'created'];
|
||
|
|
$orderBy = $allowedOrder[$order] ?? $allowedOrder['key'];
|
||
|
|
|
||
|
|
$where = [];
|
||
|
|
$params = [];
|
||
|
|
if ($search !== '') {
|
||
|
|
$where[] = '(permissions.`key` like ? or permissions.description like ?)';
|
||
|
|
$params[] = '%' . $search . '%';
|
||
|
|
$params[] = '%' . $search . '%';
|
||
|
|
}
|
||
|
|
$whereSql = $where ? ' where ' . implode(' and ', $where) : '';
|
||
|
|
|
||
|
|
$total = DB::selectValue('select count(*) from permissions' . $whereSql, ...$params);
|
||
|
|
$query = 'select id, `key`, description, created from permissions' . $whereSql .
|
||
|
|
' order by ' . $orderBy . ' ' . $dir . ' limit ? offset ?';
|
||
|
|
$queryParams = array_merge($params, [(string) $limit, (string) $offset]);
|
||
|
|
$rows = DB::select($query, ...$queryParams);
|
||
|
|
$list = [];
|
||
|
|
foreach ($rows as $row) {
|
||
|
|
$data = $row['permissions'] ?? $row;
|
||
|
|
if (is_array($data)) {
|
||
|
|
$list[] = $data;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return ['data' => $list, 'total' => (int) $total];
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function find(int $id): ?array
|
||
|
|
{
|
||
|
|
$row = DB::selectOne('select id, `key`, description, created from permissions where id = ? limit 1', (string) $id);
|
||
|
|
$data = $row['permissions'] ?? $row;
|
||
|
|
return is_array($data) ? $data : null;
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function findByKey(string $key): ?array
|
||
|
|
{
|
||
|
|
$row = DB::selectOne('select id, `key`, description, created from permissions where `key` = ? limit 1', $key);
|
||
|
|
$data = $row['permissions'] ?? $row;
|
||
|
|
return is_array($data) ? $data : null;
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function create(array $data): ?int
|
||
|
|
{
|
||
|
|
$key = trim((string) ($data['key'] ?? ''));
|
||
|
|
$desc = trim((string) ($data['description'] ?? ''));
|
||
|
|
$result = DB::insert(
|
||
|
|
'insert into permissions (`key`, description, created) values (?,?,NOW())',
|
||
|
|
$key,
|
||
|
|
$desc !== '' ? $desc : null
|
||
|
|
);
|
||
|
|
return $result ? (int) $result : null;
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function update(int $id, array $data): bool
|
||
|
|
{
|
||
|
|
$key = trim((string) ($data['key'] ?? ''));
|
||
|
|
$desc = trim((string) ($data['description'] ?? ''));
|
||
|
|
$result = DB::update(
|
||
|
|
'update permissions set `key` = ?, description = ? where id = ?',
|
||
|
|
$key,
|
||
|
|
$desc !== '' ? $desc : null,
|
||
|
|
(string) $id
|
||
|
|
);
|
||
|
|
return $result !== false;
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function delete(int $id): bool
|
||
|
|
{
|
||
|
|
$result = DB::delete('delete from permissions where id = ?', (string) $id);
|
||
|
|
return (bool) $result;
|
||
|
|
}
|
||
|
|
}
|