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 [];
}

View File

@@ -4,60 +4,28 @@ namespace MintyPHP\Repository\Org;
use MintyPHP\DB;
use MintyPHP\Repository\Support\RepoQuery;
use MintyPHP\Repository\Support\RepositoryArrayHelper;
class DepartmentRepository implements DepartmentRepositoryInterface
{
private function unwrap(?array $row): ?array
{
if (!$row) {
return null;
}
return $row['departments'] ?? null;
}
private function unwrapList($rows): array
{
if (!is_array($rows)) {
return [];
}
$list = [];
foreach ($rows as $row) {
$department = $row['departments'] ?? null;
if (is_array($department)) {
$list[] = $department;
}
}
return $list;
}
public function list(): array
{
$rows = DB::select(
'select id, uuid, description, tenant_id, code, cost_center, active, created_by, modified_by, created, modified from departments order by id desc'
);
return $this->unwrapList($rows);
return RepositoryArrayHelper::unwrapList($rows, 'departments');
}
public function listIds(): array
{
$rows = DB::select('select id from departments');
if (!is_array($rows)) {
return [];
}
$ids = [];
foreach ($rows as $row) {
$data = $row['departments'] ?? $row;
if (is_array($data) && isset($data['id'])) {
$ids[] = (int) $data['id'];
}
}
return array_values(array_unique($ids));
return RepositoryArrayHelper::extractIds($rows, 'departments');
}
public function listActiveIdsByTenantIds(array $tenantIds): array
{
$tenantIds = array_values(array_unique(array_map('intval', $tenantIds)));
$tenantIds = array_filter($tenantIds, static fn ($id) => $id > 0);
$tenantIds = RepositoryArrayHelper::sanitizePositiveIds($tenantIds);
if (!$tenantIds) {
return [];
}
@@ -66,23 +34,13 @@ class DepartmentRepository implements DepartmentRepositoryInterface
'select id from departments where active = 1 and tenant_id in (' . $placeholders . ')',
...array_map('strval', $tenantIds)
);
if (!is_array($rows)) {
return [];
}
$ids = [];
foreach ($rows as $row) {
$data = $row['departments'] ?? $row;
if (is_array($data) && isset($data['id'])) {
$ids[] = (int) $data['id'];
}
}
return array_values(array_unique($ids));
return RepositoryArrayHelper::extractIds($rows, 'departments');
}
public function listByTenantIds(array $tenantIds): array
{
$tenantIds = array_values(array_unique(array_map('intval', $tenantIds)));
$tenantIds = array_filter($tenantIds, static fn ($id) => $id > 0);
$tenantIds = RepositoryArrayHelper::sanitizePositiveIds($tenantIds);
if (!$tenantIds) {
return [];
}
@@ -94,13 +52,12 @@ class DepartmentRepository implements DepartmentRepositoryInterface
'order by departments.description asc',
...array_map('strval', $tenantIds)
);
return $this->unwrapList($rows);
return RepositoryArrayHelper::unwrapList($rows, 'departments');
}
public function listByIds(array $departmentIds, bool $includeInactive = false): array
{
$departmentIds = array_values(array_unique(array_map('intval', $departmentIds)));
$departmentIds = array_filter($departmentIds, static fn ($id) => $id > 0);
$departmentIds = RepositoryArrayHelper::sanitizePositiveIds($departmentIds);
if (!$departmentIds) {
return [];
}
@@ -110,7 +67,7 @@ class DepartmentRepository implements DepartmentRepositoryInterface
'select id, uuid, description, tenant_id, code, cost_center, active, created_by, modified_by, created, modified from departments where ' . $activeSql . 'id in (' . $placeholders . ') order by description asc',
...array_map('strval', $departmentIds)
);
return $this->unwrapList($rows);
return RepositoryArrayHelper::unwrapList($rows, 'departments');
}
public function listPaged(array $options): array
@@ -171,7 +128,7 @@ class DepartmentRepository implements DepartmentRepositoryInterface
$queryParams = array_merge($params, [(string) $limit, (string) $offset]);
$rows = call_user_func_array(['MintyPHP\\DB', 'select'], array_merge([$query], $queryParams));
$list = $this->unwrapList($rows);
$list = RepositoryArrayHelper::unwrapList($rows, 'departments');
$ids = [];
foreach ($list as $department) {
if (isset($department['id'])) {
@@ -223,7 +180,7 @@ class DepartmentRepository implements DepartmentRepositoryInterface
'select id, uuid, description, tenant_id, code, cost_center, active, created_by, modified_by, created, modified from departments where id = ? limit 1',
(string) $id
);
return $this->unwrap($row);
return RepositoryArrayHelper::unwrap($row, 'departments');
}
public function findByUuid(string $uuid): ?array
@@ -232,7 +189,7 @@ class DepartmentRepository implements DepartmentRepositoryInterface
'select id, uuid, description, tenant_id, code, cost_center, active, created_by, modified_by, created, modified from departments where uuid = ? limit 1',
$uuid
);
return $this->unwrap($row);
return RepositoryArrayHelper::unwrap($row, 'departments');
}
public function existsByCode(string $code, int $excludeId = 0): bool

View File

@@ -3,23 +3,15 @@
namespace MintyPHP\Repository\Org;
use MintyPHP\DB;
use MintyPHP\Repository\Support\RepositoryArrayHelper;
class UserDepartmentRepository implements UserDepartmentRepositoryInterface
{
public function listDepartmentIdsByUserId(int $userId): array
{
$rows = DB::select('select department_id from user_departments where user_id = ?', (string) $userId);
if (!is_array($rows)) {
return [];
}
$ids = [];
foreach ($rows as $row) {
$data = $row['user_departments'] ?? $row;
if (is_array($data) && isset($data['department_id'])) {
$ids[] = (int) $data['department_id'];
}
}
return array_values(array_unique($ids));
return RepositoryArrayHelper::extractIds($rows, 'user_departments', 'department_id');
}
public function replaceForUser(int $userId, array $departmentIds): bool
@@ -64,8 +56,7 @@ class UserDepartmentRepository implements UserDepartmentRepositoryInterface
private function countByDepartmentIds(array $departmentIds, bool $activeOnly): array
{
$departmentIds = array_values(array_unique(array_map('intval', $departmentIds)));
$departmentIds = array_values(array_filter($departmentIds, static fn ($id) => $id > 0));
$departmentIds = RepositoryArrayHelper::sanitizePositiveIds($departmentIds);
if (!$departmentIds) {
return [];
}

View File

@@ -0,0 +1,73 @@
<?php
namespace MintyPHP\Repository\Support;
final class RepositoryArrayHelper
{
/**
* Unwrap a single DB row from its table-name wrapper.
*
* MintyPHP DB layer returns rows wrapped in table-name keys, e.g. ['tenants' => [...]].
* This extracts the inner array for the given table key.
*/
public static function unwrap(?array $row, string $tableKey): ?array
{
if (!$row) {
return null;
}
return $row[$tableKey] ?? null;
}
/**
* Unwrap a list of DB rows from their table-name wrappers.
*/
public static function unwrapList(mixed $rows, string $tableKey): array
{
if (!is_array($rows)) {
return [];
}
$list = [];
foreach ($rows as $row) {
$data = $row[$tableKey] ?? null;
if (is_array($data)) {
$list[] = $data;
}
}
return $list;
}
/**
* Extract integer IDs from DB rows, unwrapping the table-name key.
*
* Returns a deduplicated array of unique positive integer IDs.
*/
public static function extractIds(mixed $rows, string $tableKey, string $idField = 'id'): array
{
if (!is_array($rows)) {
return [];
}
$ids = [];
foreach ($rows as $row) {
$data = $row[$tableKey] ?? $row;
if (is_array($data) && isset($data[$idField])) {
$ids[] = (int) $data[$idField];
}
}
return array_values(array_unique($ids));
}
/**
* Sanitize an array of IDs to unique positive integers.
*/
public static function sanitizePositiveIds(array $ids): array
{
$ids = array_values(array_unique(array_map('intval', $ids)));
return array_values(array_filter($ids, static fn (int $id): bool => $id > 0));
}
}

View File

@@ -5,60 +5,28 @@ namespace MintyPHP\Repository\Tenant;
use MintyPHP\DB;
use MintyPHP\Domain\Taxonomy\TenantStatus;
use MintyPHP\Repository\Support\RepoQuery;
use MintyPHP\Repository\Support\RepositoryArrayHelper;
class TenantRepository implements TenantRepositoryInterface
{
private function unwrap(?array $row): ?array
{
if (!$row) {
return null;
}
return $row['tenants'] ?? null;
}
private function unwrapList($rows): array
{
if (!is_array($rows)) {
return [];
}
$list = [];
foreach ($rows as $row) {
$tenant = $row['tenants'] ?? null;
if (is_array($tenant)) {
$list[] = $tenant;
}
}
return $list;
}
public function list(): array
{
$rows = DB::select(
'select id, uuid, description, address, postal_code, city, country, region, vat_id, tax_number, phone, fax, email, support_email, support_phone, billing_email, website, privacy_url, imprint_url, primary_color, default_theme, allow_user_theme, status, status_changed_at, status_changed_by, created_by, modified_by, created, modified from tenants order by id desc'
);
return $this->unwrapList($rows);
return RepositoryArrayHelper::unwrapList($rows, 'tenants');
}
public function listIds(): array
{
$rows = DB::select('select id from tenants');
if (!is_array($rows)) {
return [];
}
$ids = [];
foreach ($rows as $row) {
$data = $row['tenants'] ?? $row;
if (is_array($data) && isset($data['id'])) {
$ids[] = (int) $data['id'];
}
}
return array_values(array_unique($ids));
return RepositoryArrayHelper::extractIds($rows, 'tenants');
}
public function listByIds(array $tenantIds): array
{
$tenantIds = array_values(array_unique(array_map('intval', $tenantIds)));
$tenantIds = array_values(array_filter($tenantIds, static fn ($id) => $id > 0));
$tenantIds = RepositoryArrayHelper::sanitizePositiveIds($tenantIds);
if (!$tenantIds) {
return [];
}
@@ -68,12 +36,12 @@ class TenantRepository implements TenantRepositoryInterface
'select id, uuid, description, address, postal_code, city, country, region, vat_id, tax_number, phone, fax, email, support_email, support_phone, billing_email, website, privacy_url, imprint_url, primary_color, default_theme, allow_user_theme, status, status_changed_at, status_changed_by, created_by, modified_by, created, modified from tenants where id in (' . $placeholders . ')',
...array_map('strval', $tenantIds)
);
return $this->unwrapList($rows);
return RepositoryArrayHelper::unwrapList($rows, 'tenants');
}
public function listActiveIdsByIds(array $tenantIds): array
{
$tenantIds = array_values(array_unique(array_map('intval', $tenantIds)));
$tenantIds = RepositoryArrayHelper::sanitizePositiveIds($tenantIds);
if (!$tenantIds) {
return [];
}
@@ -86,17 +54,7 @@ class TenantRepository implements TenantRepositoryInterface
$params
)
);
if (!is_array($rows)) {
return [];
}
$ids = [];
foreach ($rows as $row) {
$data = $row['tenants'] ?? $row;
if (is_array($data) && isset($data['id'])) {
$ids[] = (int) $data['id'];
}
}
return array_values(array_unique($ids));
return RepositoryArrayHelper::extractIds($rows, 'tenants');
}
public function listPaged(array $options): array
@@ -146,7 +104,7 @@ class TenantRepository implements TenantRepositoryInterface
return [
'total' => $total,
'rows' => $this->unwrapList($rows),
'rows' => RepositoryArrayHelper::unwrapList($rows, 'tenants'),
];
}
@@ -156,7 +114,7 @@ class TenantRepository implements TenantRepositoryInterface
'select id, uuid, description, address, postal_code, city, country, region, vat_id, tax_number, phone, fax, email, support_email, support_phone, billing_email, website, privacy_url, imprint_url, primary_color, default_theme, allow_user_theme, status, status_changed_at, status_changed_by, created_by, modified_by, created, modified from tenants where id = ? limit 1',
(string) $id
);
return $this->unwrap($row);
return RepositoryArrayHelper::unwrap($row, 'tenants');
}
public function findByUuid(string $uuid): ?array
@@ -165,7 +123,7 @@ class TenantRepository implements TenantRepositoryInterface
'select id, uuid, description, address, postal_code, city, country, region, vat_id, tax_number, phone, fax, email, support_email, support_phone, billing_email, website, privacy_url, imprint_url, primary_color, default_theme, allow_user_theme, status, status_changed_at, status_changed_by, created_by, modified_by, created, modified from tenants where uuid = ? limit 1',
$uuid
);
return $this->unwrap($row);
return RepositoryArrayHelper::unwrap($row, 'tenants');
}
public function create(array $data): mixed