refactor(repository): deduplicate unwrap and id array helpers

This commit is contained in:
2026-03-13 13:12:51 +01:00
parent efa031ea5f
commit d28f85ac9e
13 changed files with 465 additions and 240 deletions

View File

@@ -4,23 +4,15 @@ namespace MintyPHP\Repository\Access;
use MintyPHP\DB;
use MintyPHP\Repository\Support\RepoQuery;
use MintyPHP\Repository\Support\RepositoryArrayHelper;
class PermissionRepository implements PermissionRepositoryInterface
{
public function list(): array
{
$rows = DB::select('select id, `key`, description, active, is_system, 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;
return RepositoryArrayHelper::unwrapList($rows, 'permissions');
}
public function listActive(): array
@@ -28,17 +20,8 @@ class PermissionRepository implements PermissionRepositoryInterface
$rows = DB::select(
'select id, `key`, description, active, is_system, created from permissions where active = 1 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;
return RepositoryArrayHelper::unwrapList($rows, 'permissions');
}
public function listPaged(array $options): array
@@ -69,14 +52,8 @@ class PermissionRepository implements PermissionRepositoryInterface
' 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 ['rows' => $list, 'total' => (int) $total];
return ['rows' => RepositoryArrayHelper::unwrapList($rows, 'permissions'), 'total' => (int) $total];
}
public function find(int $id): ?array
@@ -85,8 +62,8 @@ class PermissionRepository implements PermissionRepositoryInterface
'select id, `key`, description, active, is_system, created from permissions where id = ? limit 1',
(string) $id
);
$data = $row['permissions'] ?? $row;
return is_array($data) ? $data : null;
return RepositoryArrayHelper::unwrap($row, 'permissions');
}
public function findByKey(string $key): ?array
@@ -95,8 +72,8 @@ class PermissionRepository implements PermissionRepositoryInterface
'select id, `key`, description, active, is_system, created from permissions where `key` = ? limit 1',
$key
);
$data = $row['permissions'] ?? $row;
return is_array($data) ? $data : null;
return RepositoryArrayHelper::unwrap($row, 'permissions');
}
public function create(array $data): ?int

View File

@@ -4,38 +4,16 @@ namespace MintyPHP\Repository\Access;
use MintyPHP\DB;
use MintyPHP\Repository\Support\RepoQuery;
use MintyPHP\Repository\Support\RepositoryArrayHelper;
class RoleRepository implements RoleRepositoryInterface
{
private function unwrap(?array $row): ?array
{
if (!$row) {
return null;
}
return $row['roles'] ?? null;
}
private 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 function list(): array
{
$rows = DB::select(
'select id, uuid, description, code, active, created_by, modified_by, created, modified from roles order by id desc'
);
return $this->unwrapList($rows);
return RepositoryArrayHelper::unwrapList($rows, 'roles');
}
public function listActive(): array
@@ -43,13 +21,12 @@ class RoleRepository implements RoleRepositoryInterface
$rows = DB::select(
'select id, uuid, description, code, active, created_by, modified_by, created, modified from roles where active = 1 order by description asc'
);
return $this->unwrapList($rows);
return RepositoryArrayHelper::unwrapList($rows, 'roles');
}
public 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));
$roleIds = RepositoryArrayHelper::sanitizePositiveIds($roleIds);
if (!$roleIds) {
return [];
}
@@ -59,39 +36,21 @@ class RoleRepository implements RoleRepositoryInterface
'select id, uuid, description, code, active, created_by, modified_by, created, modified from roles where id in (' . $placeholders . ')',
...array_map('strval', $roleIds)
);
return $this->unwrapList($rows);
return RepositoryArrayHelper::unwrapList($rows, 'roles');
}
public 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));
return RepositoryArrayHelper::extractIds($rows, 'roles');
}
public function listActiveIds(): array
{
$rows = DB::select('select id from roles where active = 1');
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));
return RepositoryArrayHelper::extractIds($rows, 'roles');
}
public function listPaged(array $options): array
@@ -123,7 +82,7 @@ class RoleRepository implements RoleRepositoryInterface
return [
'total' => $total,
'rows' => $this->unwrapList($rows),
'rows' => RepositoryArrayHelper::unwrapList($rows, 'roles'),
];
}
@@ -133,7 +92,7 @@ class RoleRepository implements RoleRepositoryInterface
'select id, uuid, description, code, active, created_by, modified_by, created, modified from roles where id = ? limit 1',
(string) $id
);
return $this->unwrap($row);
return RepositoryArrayHelper::unwrap($row, 'roles');
}
public function findByUuid(string $uuid): ?array
@@ -142,7 +101,7 @@ class RoleRepository implements RoleRepositoryInterface
'select id, uuid, description, code, active, created_by, modified_by, created, modified from roles where uuid = ? limit 1',
$uuid
);
return $this->unwrap($row);
return RepositoryArrayHelper::unwrap($row, 'roles');
}
public function existsByCode(string $code, int $excludeId = 0): bool

View File

@@ -3,23 +3,15 @@
namespace MintyPHP\Repository\Access;
use MintyPHP\DB;
use MintyPHP\Repository\Support\RepositoryArrayHelper;
class UserRoleRepository implements UserRoleRepositoryInterface
{
public function listRoleIdsByUserId(int $userId): array
{
$rows = DB::select('select role_id from user_roles where user_id = ?', (string) $userId);
if (!is_array($rows)) {
return [];
}
$ids = [];
foreach ($rows as $row) {
$data = $row['user_roles'] ?? $row;
if (is_array($data) && isset($data['role_id'])) {
$ids[] = (int) $data['role_id'];
}
}
return array_values(array_unique($ids));
return RepositoryArrayHelper::extractIds($rows, 'user_roles', 'role_id');
}
public function replaceForUser(int $userId, array $roleIds): bool
@@ -42,8 +34,7 @@ class UserRoleRepository implements UserRoleRepositoryInterface
public function listUserIdsByRoleIds(array $roleIds): array
{
$ids = array_values(array_unique(array_map('intval', $roleIds)));
$ids = array_values(array_filter($ids, static fn ($id) => $id > 0));
$ids = RepositoryArrayHelper::sanitizePositiveIds($roleIds);
if (!$ids) {
return [];
}
@@ -52,20 +43,10 @@ class UserRoleRepository implements UserRoleRepositoryInterface
'select distinct user_id from user_roles where role_id in (???)',
array_map('strval', $ids)
);
if (!is_array($rows)) {
return [];
}
$userIds = [];
foreach ($rows as $row) {
$data = $row['user_roles'] ?? $row;
if (!is_array($data) || !isset($data['user_id'])) {
continue;
}
$userIds[] = (int) $data['user_id'];
}
return array_values(array_unique(array_filter($userIds, static fn ($id) => $id > 0)));
return RepositoryArrayHelper::sanitizePositiveIds(
RepositoryArrayHelper::extractIds($rows, 'user_roles', 'user_id')
);
}
public function countUsersByRoleIds(array $roleIds): array
@@ -80,8 +61,7 @@ class UserRoleRepository implements UserRoleRepositoryInterface
private function countByRoleIds(array $roleIds, bool $activeOnly): array
{
$roleIds = array_values(array_unique(array_map('intval', $roleIds)));
$roleIds = array_values(array_filter($roleIds, static fn ($id) => $id > 0));
$roleIds = RepositoryArrayHelper::sanitizePositiveIds($roleIds);
if (!$roleIds) {
return [];
}