1
0

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,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