Files
breadcrumb-the-shire/lib/Repository/Access/RoleRepository.php

204 lines
6.6 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\Access;
2026-02-04 23:31:53 +01:00
use MintyPHP\DB;
2026-02-11 19:28:12 +01:00
use MintyPHP\Repository\Support\RepoQuery;
2026-02-04 23:31:53 +01:00
class RoleRepository
{
private static function unwrap(?array $row): ?array
{
if (!$row) {
return null;
}
return $row['roles'] ?? null;
}
private static function unwrapList($rows): array
{
if (!is_array($rows)) {
return [];
}
$list = [];
foreach ($rows as $row) {
$role = $row['roles'] ?? null;
if (is_array($role)) {
$list[] = $role;
}
}
return $list;
}
public static function list(): array
{
$rows = DB::select(
2026-02-11 19:28:12 +01:00
'select id, uuid, description, code, active, created_by, modified_by, created, modified from roles order by id desc'
);
return self::unwrapList($rows);
}
public static function listActive(): array
{
$rows = DB::select(
'select id, uuid, description, code, active, created_by, modified_by, created, modified from roles where active = 1 order by description asc'
2026-02-04 23:31:53 +01:00
);
return self::unwrapList($rows);
}
public static function listByIds(array $roleIds): array
{
$roleIds = array_values(array_unique(array_map('intval', $roleIds)));
$roleIds = array_values(array_filter($roleIds, static fn ($id) => $id > 0));
if (!$roleIds) {
return [];
}
$placeholders = implode(',', array_fill(0, count($roleIds), '?'));
$rows = DB::select(
'select id, uuid, description, code, active, created_by, modified_by, created, modified from roles where id in (' . $placeholders . ')',
...array_map('strval', $roleIds)
);
return self::unwrapList($rows);
}
2026-02-04 23:31:53 +01:00
public static function listIds(): array
{
$rows = DB::select('select id from roles');
if (!is_array($rows)) {
return [];
}
$ids = [];
foreach ($rows as $row) {
$data = $row['roles'] ?? $row;
if (is_array($data) && isset($data['id'])) {
$ids[] = (int) $data['id'];
}
}
return array_values(array_unique($ids));
}
2026-02-11 19:28:12 +01:00
public static function listActiveIds(): array
2026-02-04 23:31:53 +01:00
{
2026-02-11 19:28:12 +01:00
$rows = DB::select('select id from roles where active = 1');
if (!is_array($rows)) {
return [];
2026-02-04 23:31:53 +01:00
}
2026-02-11 19:28:12 +01:00
$ids = [];
foreach ($rows as $row) {
$data = $row['roles'] ?? $row;
if (is_array($data) && isset($data['id'])) {
$ids[] = (int) $data['id'];
}
2026-02-04 23:31:53 +01:00
}
2026-02-11 19:28:12 +01:00
return array_values(array_unique($ids));
}
2026-02-04 23:31:53 +01:00
2026-02-11 19:28:12 +01:00
public static function listPaged(array $options): array
{
2026-02-04 23:31:53 +01:00
$search = trim((string) ($options['search'] ?? ''));
2026-02-11 19:28:12 +01:00
$allowedOrder = ['id', 'uuid', 'description', 'code', 'active', 'created', 'modified'];
[$limit, $offset] = RepoQuery::sanitizeLimitOffset($options);
[$order, $dir] = RepoQuery::sanitizeOrder($options, $allowedOrder);
2026-02-04 23:31:53 +01:00
$where = [];
$params = [];
2026-02-11 19:28:12 +01:00
RepoQuery::addLikeFilter($where, $params, ['description', 'uuid', 'code'], $search);
$activeValue = array_key_exists('active', $options) ? $options['active'] : null;
RepoQuery::addEnumFilter($where, $params, $activeValue, [
['aliases' => ['1', 'true', 'active'], 'sql' => 'roles.active = 1'],
['aliases' => ['0', 'false', 'inactive'], 'sql' => 'roles.active = 0'],
]);
2026-02-04 23:31:53 +01:00
$whereSql = $where ? (' where ' . implode(' and ', $where)) : '';
$count = DB::selectValue('select count(*) from roles' . $whereSql, ...$params);
$total = $count ? (int) $count : 0;
2026-02-11 19:28:12 +01:00
$query = 'select id, uuid, description, code, active, created_by, modified_by, created, modified from roles' .
2026-02-04 23:31:53 +01:00
$whereSql .
sprintf(' order by `%s` %s limit ? offset ?', $order, $dir);
$queryParams = array_merge($params, [(string) $limit, (string) $offset]);
$rows = call_user_func_array(['MintyPHP\\DB', 'select'], array_merge([$query], $queryParams));
return [
'total' => $total,
'rows' => self::unwrapList($rows),
];
}
public static function find(int $id): ?array
{
$row = DB::selectOne(
2026-02-11 19:28:12 +01:00
'select id, uuid, description, code, active, created_by, modified_by, created, modified from roles where id = ? limit 1',
2026-02-04 23:31:53 +01:00
(string) $id
);
return self::unwrap($row);
}
public static function findByUuid(string $uuid): ?array
{
$row = DB::selectOne(
2026-02-11 19:28:12 +01:00
'select id, uuid, description, code, active, created_by, modified_by, created, modified from roles where uuid = ? limit 1',
2026-02-04 23:31:53 +01:00
$uuid
);
return self::unwrap($row);
}
2026-02-11 19:28:12 +01:00
public static function existsByCode(string $code, int $excludeId = 0): bool
{
$code = trim($code);
if ($code === '') {
return false;
}
if ($excludeId > 0) {
$count = DB::selectValue('select count(*) from roles where code = ? and id != ?', $code, (string) $excludeId);
} else {
$count = DB::selectValue('select count(*) from roles where code = ?', $code);
}
return $count ? ((int) $count > 0) : false;
}
2026-02-04 23:31:53 +01:00
public static function create(array $data)
{
return DB::insert(
2026-02-11 19:28:12 +01:00
'insert into roles (uuid, description, code, active, created_by, created) values (?,?,?,?,?,NOW())',
$data['uuid'] ?? RepoQuery::uuidV4(),
2026-02-04 23:31:53 +01:00
$data['description'],
2026-02-11 19:28:12 +01:00
$data['code'] ?? null,
$data['active'] ?? 1,
2026-02-04 23:31:53 +01:00
$data['created_by'] ?? null
);
}
public static function update(int $id, array $data): bool
{
$fields = [
'description' => $data['description'],
2026-02-11 19:28:12 +01:00
'code' => $data['code'] ?? null,
'active' => $data['active'] ?? 1,
2026-02-04 23:31:53 +01:00
];
if (array_key_exists('modified_by', $data)) {
$fields['modified_by'] = $data['modified_by'];
}
$setParts = [];
$params = [];
foreach ($fields as $field => $value) {
$setParts[] = sprintf('`%s` = ?', $field);
$params[] = $value;
}
$params[] = (string) $id;
$query = 'update roles set ' . implode(', ', $setParts) . ' where id = ?';
$result = DB::update($query, ...$params);
return $result !== false;
}
public static function delete(int $id): bool
{
$result = DB::delete('delete from roles where id = ?', (string) $id);
return $result !== false;
}
}