refactor(repository): deduplicate unwrap and id array helpers
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user