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