1
0
Files
breadcrumb-the-shire/core/Repository/CustomField/UserCustomFieldValueOptionRepository.php
fs 1c779b8eeb refactor(support): consolidate remaining ID normalization via toIntIds()
Extends the toIntIds() rollout to service and repository layers:

- Drops a third private duplicate (UserCustomFieldValueService::normalizeTenantIds)
- Collapses the two-line intval+filter pattern inside UserAssignmentService,
  UserAuthorizationPolicy, SsoUserLinkService, DepartmentService, and
  UserCustomFieldValueService
- Replaces inline patterns in 4 repositories (RolePermissionRepository,
  RoleAssignableRoleRepository, UserWriteRepository, DepartmentRepository,
  UserCustomFieldValueRepository, UserCustomFieldValueOptionRepository)
- Simplifies the notifications sanitizeTenantIds trait body

Tenant-area files deliberately untouched per parallel ongoing work on
the tenant-logo refactor.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-24 19:18:45 +02:00

78 lines
2.1 KiB
PHP

<?php
namespace MintyPHP\Repository\CustomField;
use MintyPHP\DB;
/** Manages selected option links for user custom field values (atomic replace). */
class UserCustomFieldValueOptionRepository implements UserCustomFieldValueOptionRepositoryInterface
{
public function replaceForValueId(int $valueId, array $optionIds): bool
{
if ($valueId <= 0) {
return false;
}
$deleted = DB::delete('delete from user_custom_field_value_options where value_id = ?', (string) $valueId);
if ($deleted === false) {
return false;
}
$optionIds = toIntIds($optionIds);
if (!$optionIds) {
return true;
}
foreach ($optionIds as $optionId) {
$inserted = DB::insert(
'insert into user_custom_field_value_options (value_id, option_id, created) values (?,?,NOW())',
(string) $valueId,
(string) $optionId
);
if ($inserted === false) {
return false;
}
}
return true;
}
public function listOptionIdsByValueIds(array $valueIds): array
{
$valueIds = toIntIds($valueIds);
if (!$valueIds) {
return [];
}
$rows = DB::select(
'select value_id, option_id from user_custom_field_value_options where value_id in (???)',
$valueIds
);
if (!is_array($rows)) {
return [];
}
$map = [];
foreach ($rows as $row) {
$data = $row['user_custom_field_value_options'] ?? $row;
if (!is_array($data)) {
continue;
}
$valueId = (int) ($data['value_id'] ?? 0);
$optionId = (int) ($data['option_id'] ?? 0);
if ($valueId <= 0 || $optionId <= 0) {
continue;
}
$map[$valueId] ??= [];
$map[$valueId][] = $optionId;
}
foreach ($map as &$ids) {
$ids = toIntIds($ids);
sort($ids, SORT_NUMERIC);
}
unset($ids);
return $map;
}
}