refactor: rename lib/ to core/ for clearer core-module separation
Rename the top-level lib/ directory to core/ so the project root immediately communicates which code is core platform and which lives in modules/. PHP namespaces (MintyPHP\*) are unchanged — only the Composer PSR-4 path mapping moves from lib/ to core/. Module-internal lib/ directories (modules/*/lib/) are untouched. Updated across the full stack: - composer.json PSR-4 mapping - bootstrap entry points (web/index.php, bin/*, tests/bootstrap.php) - tooling configs (phpstan.neon, phpunit.xml, php-cs-fixer) - 26 architecture contract tests - enforcement-policy, guard-catalog, quality-gates - all documentation (CLAUDE.md, README, 25 docs/, .agents/skills/) - bin/qa-extended.sh search paths - .claude/settings.local.json permission paths Workflow: .agents/runs/CORE-LIB-RENAME-001/ (Analyst → Planner → Executor → Code Review (4 findings fixed) → Security Review → Acceptance Test → Finalizer) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,175 @@
|
||||
<?php
|
||||
|
||||
namespace MintyPHP\Repository\CustomField;
|
||||
|
||||
use MintyPHP\DB;
|
||||
use MintyPHP\Repository\Support\RepoQuery;
|
||||
|
||||
/** Reads and writes tenant-scoped custom field definitions with type and pagination filtering. */
|
||||
class TenantCustomFieldDefinitionRepository
|
||||
{
|
||||
private static function unwrap(?array $row): ?array
|
||||
{
|
||||
if (!$row) {
|
||||
return null;
|
||||
}
|
||||
return $row['tenant_custom_field_definitions'] ?? null;
|
||||
}
|
||||
|
||||
private static function unwrapList($rows): array
|
||||
{
|
||||
if (!is_array($rows)) {
|
||||
return [];
|
||||
}
|
||||
$list = [];
|
||||
foreach ($rows as $row) {
|
||||
$item = $row['tenant_custom_field_definitions'] ?? null;
|
||||
if (is_array($item)) {
|
||||
$list[] = $item;
|
||||
}
|
||||
}
|
||||
return $list;
|
||||
}
|
||||
|
||||
public static function listByTenantId(int $tenantId, bool $onlyActive = true): array
|
||||
{
|
||||
if ($tenantId <= 0) {
|
||||
return [];
|
||||
}
|
||||
$query = 'select id, uuid, tenant_id, field_key, label, type, is_required, is_filterable, active, sort_order, created_by, modified_by, created, modified ' .
|
||||
'from tenant_custom_field_definitions where tenant_id = ?';
|
||||
$params = [(string) $tenantId];
|
||||
if ($onlyActive) {
|
||||
$query .= ' and active = 1';
|
||||
}
|
||||
$query .= ' order by sort_order asc, label asc, id asc';
|
||||
return self::unwrapList(DB::select($query, ...$params));
|
||||
}
|
||||
|
||||
public static function listByTenantIds(array $tenantIds, bool $onlyActive = true): array
|
||||
{
|
||||
$tenantIds = array_values(array_unique(array_map('intval', $tenantIds)));
|
||||
$tenantIds = array_values(array_filter($tenantIds, static fn ($id) => $id > 0));
|
||||
if (!$tenantIds) {
|
||||
return [];
|
||||
}
|
||||
$query = 'select id, uuid, tenant_id, field_key, label, type, is_required, is_filterable, active, sort_order, created_by, modified_by, created, modified ' .
|
||||
'from tenant_custom_field_definitions where tenant_id in (???)';
|
||||
$params = [$tenantIds];
|
||||
if ($onlyActive) {
|
||||
$query .= ' and active = 1';
|
||||
}
|
||||
$query .= ' order by tenant_id asc, sort_order asc, label asc, id asc';
|
||||
return self::unwrapList(call_user_func_array(['MintyPHP\\DB', 'select'], array_merge([$query], $params)));
|
||||
}
|
||||
|
||||
public static function listFilterableByTenantIds(array $tenantIds): array
|
||||
{
|
||||
$tenantIds = array_values(array_unique(array_map('intval', $tenantIds)));
|
||||
$tenantIds = array_values(array_filter($tenantIds, static fn ($id) => $id > 0));
|
||||
if (!$tenantIds) {
|
||||
return [];
|
||||
}
|
||||
$query = 'select id, uuid, tenant_id, field_key, label, type, is_required, is_filterable, active, sort_order, created_by, modified_by, created, modified ' .
|
||||
'from tenant_custom_field_definitions ' .
|
||||
'where tenant_id in (???) and active = 1 and is_filterable = 1 ' .
|
||||
"and type in ('select', 'multiselect', 'boolean', 'date') " .
|
||||
'order by tenant_id asc, sort_order asc, label asc, id asc';
|
||||
return self::unwrapList(call_user_func_array(['MintyPHP\\DB', 'select'], [$query, $tenantIds]));
|
||||
}
|
||||
|
||||
public static function findByUuid(string $uuid): ?array
|
||||
{
|
||||
$row = DB::selectOne(
|
||||
'select id, uuid, tenant_id, field_key, label, type, is_required, is_filterable, active, sort_order, created_by, modified_by, created, modified ' .
|
||||
'from tenant_custom_field_definitions where uuid = ? limit 1',
|
||||
$uuid
|
||||
);
|
||||
return self::unwrap($row);
|
||||
}
|
||||
|
||||
public static function findById(int $id): ?array
|
||||
{
|
||||
$row = DB::selectOne(
|
||||
'select id, uuid, tenant_id, field_key, label, type, is_required, is_filterable, active, sort_order, created_by, modified_by, created, modified ' .
|
||||
'from tenant_custom_field_definitions where id = ? limit 1',
|
||||
(string) $id
|
||||
);
|
||||
return self::unwrap($row);
|
||||
}
|
||||
|
||||
public static function findByTenantIdAndKey(int $tenantId, string $fieldKey): ?array
|
||||
{
|
||||
if ($tenantId <= 0 || $fieldKey === '') {
|
||||
return null;
|
||||
}
|
||||
$row = DB::selectOne(
|
||||
'select id, uuid, tenant_id, field_key, label, type, is_required, is_filterable, active, sort_order, created_by, modified_by, created, modified ' .
|
||||
'from tenant_custom_field_definitions where tenant_id = ? and field_key = ? limit 1',
|
||||
(string) $tenantId,
|
||||
$fieldKey
|
||||
);
|
||||
return self::unwrap($row);
|
||||
}
|
||||
|
||||
public static function create(array $data): int|false
|
||||
{
|
||||
return DB::insert(
|
||||
'insert into tenant_custom_field_definitions ' .
|
||||
'(uuid, tenant_id, field_key, label, type, is_required, is_filterable, active, sort_order, created_by, created) ' .
|
||||
'values (?,?,?,?,?,?,?,?,?,?,NOW())',
|
||||
$data['uuid'] ?? RepoQuery::uuidV4(),
|
||||
(string) ($data['tenant_id'] ?? 0),
|
||||
(string) ($data['field_key'] ?? ''),
|
||||
(string) ($data['label'] ?? ''),
|
||||
(string) ($data['type'] ?? ''),
|
||||
(string) ((int) ($data['is_required'] ?? 0)),
|
||||
(string) ((int) ($data['is_filterable'] ?? 0)),
|
||||
(string) ((int) ($data['active'] ?? 1)),
|
||||
(string) ((int) ($data['sort_order'] ?? 100)),
|
||||
$data['created_by'] ?? null
|
||||
);
|
||||
}
|
||||
|
||||
public static function update(int $id, array $data): bool
|
||||
{
|
||||
if ($id <= 0) {
|
||||
return false;
|
||||
}
|
||||
$fields = [
|
||||
'field_key' => (string) ($data['field_key'] ?? ''),
|
||||
'label' => (string) ($data['label'] ?? ''),
|
||||
'type' => (string) ($data['type'] ?? ''),
|
||||
'is_required' => (int) ($data['is_required'] ?? 0),
|
||||
'is_filterable' => (int) ($data['is_filterable'] ?? 0),
|
||||
'active' => (int) ($data['active'] ?? 1),
|
||||
'sort_order' => (int) ($data['sort_order'] ?? 100),
|
||||
];
|
||||
if (array_key_exists('modified_by', $data)) {
|
||||
$fields['modified_by'] = $data['modified_by'];
|
||||
}
|
||||
|
||||
$set = [];
|
||||
$params = [];
|
||||
foreach ($fields as $field => $value) {
|
||||
$set[] = sprintf('`%s` = ?', $field);
|
||||
$params[] = (string) $value;
|
||||
}
|
||||
$params[] = (string) $id;
|
||||
|
||||
$result = DB::update(
|
||||
'update tenant_custom_field_definitions set ' . implode(', ', $set) . ' where id = ?',
|
||||
...$params
|
||||
);
|
||||
return $result !== false;
|
||||
}
|
||||
|
||||
public static function delete(int $id): bool
|
||||
{
|
||||
if ($id <= 0) {
|
||||
return false;
|
||||
}
|
||||
$result = DB::delete('delete from tenant_custom_field_definitions where id = ?', (string) $id);
|
||||
return $result !== false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
<?php
|
||||
|
||||
namespace MintyPHP\Repository\CustomField;
|
||||
|
||||
use MintyPHP\DB;
|
||||
|
||||
/** Retrieves selectable options for tenant-scoped custom field definitions. */
|
||||
class TenantCustomFieldOptionRepository
|
||||
{
|
||||
private static function unwrapList($rows): array
|
||||
{
|
||||
if (!is_array($rows)) {
|
||||
return [];
|
||||
}
|
||||
$list = [];
|
||||
foreach ($rows as $row) {
|
||||
$item = $row['tenant_custom_field_options'] ?? null;
|
||||
if (is_array($item)) {
|
||||
$list[] = $item;
|
||||
}
|
||||
}
|
||||
return $list;
|
||||
}
|
||||
|
||||
public static function listByDefinitionIds(array $definitionIds, bool $onlyActive = true): array
|
||||
{
|
||||
$definitionIds = array_values(array_unique(array_map('intval', $definitionIds)));
|
||||
$definitionIds = array_values(array_filter($definitionIds, static fn ($id) => $id > 0));
|
||||
if (!$definitionIds) {
|
||||
return [];
|
||||
}
|
||||
$query = 'select id, definition_id, option_key, label, active, sort_order, created, modified ' .
|
||||
'from tenant_custom_field_options where definition_id in (???)';
|
||||
$params = [$definitionIds];
|
||||
if ($onlyActive) {
|
||||
$query .= ' and active = 1';
|
||||
}
|
||||
$query .= ' order by definition_id asc, sort_order asc, label asc, id asc';
|
||||
return self::unwrapList(call_user_func_array(['MintyPHP\\DB', 'select'], array_merge([$query], $params)));
|
||||
}
|
||||
|
||||
public static function replaceForDefinition(int $definitionId, array $options): bool
|
||||
{
|
||||
if ($definitionId <= 0) {
|
||||
return false;
|
||||
}
|
||||
$existing = self::listByDefinitionIds([$definitionId], false);
|
||||
$existingByKey = [];
|
||||
foreach ($existing as $row) {
|
||||
$key = (string) ($row['option_key'] ?? '');
|
||||
if ($key !== '') {
|
||||
$existingByKey[$key] = $row;
|
||||
}
|
||||
}
|
||||
|
||||
$keepIds = [];
|
||||
foreach ($options as $option) {
|
||||
if (!is_array($option)) {
|
||||
continue;
|
||||
}
|
||||
$optionKey = trim((string) ($option['option_key'] ?? ''));
|
||||
$label = trim((string) ($option['label'] ?? ''));
|
||||
if ($optionKey === '' || $label === '') {
|
||||
continue;
|
||||
}
|
||||
$active = !empty($option['active']) ? 1 : 0;
|
||||
$sortOrder = (int) ($option['sort_order'] ?? 100);
|
||||
|
||||
if (isset($existingByKey[$optionKey]['id'])) {
|
||||
$optionId = (int) $existingByKey[$optionKey]['id'];
|
||||
$keepIds[] = $optionId;
|
||||
$updated = DB::update(
|
||||
'update tenant_custom_field_options set label = ?, active = ?, sort_order = ? where id = ?',
|
||||
$label,
|
||||
(string) $active,
|
||||
(string) $sortOrder,
|
||||
(string) $optionId
|
||||
);
|
||||
if ($updated === false) {
|
||||
return false;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
$insertId = DB::insert(
|
||||
'insert into tenant_custom_field_options (definition_id, option_key, label, active, sort_order, created) values (?,?,?,?,?,NOW())',
|
||||
(string) $definitionId,
|
||||
$optionKey,
|
||||
$label,
|
||||
(string) $active,
|
||||
(string) $sortOrder
|
||||
);
|
||||
if ($insertId === false) {
|
||||
return false;
|
||||
}
|
||||
if ($insertId) {
|
||||
$keepIds[] = (int) $insertId;
|
||||
}
|
||||
}
|
||||
|
||||
if (!$keepIds) {
|
||||
$deleted = DB::delete('delete from tenant_custom_field_options where definition_id = ?', (string) $definitionId);
|
||||
if ($deleted === false) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
$deleted = DB::delete(
|
||||
'delete from tenant_custom_field_options where definition_id = ? and id not in (???)',
|
||||
(string) $definitionId,
|
||||
$keepIds
|
||||
);
|
||||
if ($deleted === false) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static function deleteByDefinitionId(int $definitionId): bool
|
||||
{
|
||||
if ($definitionId <= 0) {
|
||||
return false;
|
||||
}
|
||||
$result = DB::delete('delete from tenant_custom_field_options where definition_id = ?', (string) $definitionId);
|
||||
return $result !== false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
|
||||
namespace MintyPHP\Repository\CustomField;
|
||||
|
||||
use MintyPHP\DB;
|
||||
|
||||
/** Manages selected option links for user custom field values (atomic replace). */
|
||||
class UserCustomFieldValueOptionRepository
|
||||
{
|
||||
public static 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 = array_values(array_unique(array_map('intval', $optionIds)));
|
||||
$optionIds = array_values(array_filter($optionIds, static fn ($id) => $id > 0));
|
||||
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 static function listOptionIdsByValueIds(array $valueIds): array
|
||||
{
|
||||
$valueIds = array_values(array_unique(array_map('intval', $valueIds)));
|
||||
$valueIds = array_values(array_filter($valueIds, static fn ($id) => $id > 0));
|
||||
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 = array_values(array_unique(array_map('intval', $ids)));
|
||||
sort($ids, SORT_NUMERIC);
|
||||
}
|
||||
unset($ids);
|
||||
|
||||
return $map;
|
||||
}
|
||||
}
|
||||
121
core/Repository/CustomField/UserCustomFieldValueRepository.php
Normal file
121
core/Repository/CustomField/UserCustomFieldValueRepository.php
Normal file
@@ -0,0 +1,121 @@
|
||||
<?php
|
||||
|
||||
namespace MintyPHP\Repository\CustomField;
|
||||
|
||||
use MintyPHP\DB;
|
||||
|
||||
/** Reads and writes custom field values attached to individual users. */
|
||||
class UserCustomFieldValueRepository
|
||||
{
|
||||
private static function unwrapList($rows): array
|
||||
{
|
||||
if (!is_array($rows)) {
|
||||
return [];
|
||||
}
|
||||
$list = [];
|
||||
foreach ($rows as $row) {
|
||||
$item = $row['user_custom_field_values'] ?? null;
|
||||
if (is_array($item)) {
|
||||
$list[] = $item;
|
||||
}
|
||||
}
|
||||
return $list;
|
||||
}
|
||||
|
||||
public static function listByUserAndDefinitionIds(int $userId, array $definitionIds): array
|
||||
{
|
||||
if ($userId <= 0) {
|
||||
return [];
|
||||
}
|
||||
$definitionIds = array_values(array_unique(array_map('intval', $definitionIds)));
|
||||
$definitionIds = array_values(array_filter($definitionIds, static fn ($id) => $id > 0));
|
||||
if (!$definitionIds) {
|
||||
return [];
|
||||
}
|
||||
$rows = DB::select(
|
||||
'select id, user_id, definition_id, value_text, value_bool, value_date, option_id, created, modified ' .
|
||||
'from user_custom_field_values where user_id = ? and definition_id in (???)',
|
||||
(string) $userId,
|
||||
$definitionIds
|
||||
);
|
||||
return self::unwrapList($rows);
|
||||
}
|
||||
|
||||
public static function upsertScalarValue(int $userId, int $definitionId, array $typedValue): int|false
|
||||
{
|
||||
if ($userId <= 0 || $definitionId <= 0) {
|
||||
return false;
|
||||
}
|
||||
$existingId = DB::selectValue(
|
||||
'select id from user_custom_field_values where user_id = ? and definition_id = ? limit 1',
|
||||
(string) $userId,
|
||||
(string) $definitionId
|
||||
);
|
||||
|
||||
$valueText = array_key_exists('value_text', $typedValue) ? $typedValue['value_text'] : null;
|
||||
$valueBool = array_key_exists('value_bool', $typedValue) ? $typedValue['value_bool'] : null;
|
||||
$valueDate = array_key_exists('value_date', $typedValue) ? $typedValue['value_date'] : null;
|
||||
$optionId = array_key_exists('option_id', $typedValue) ? $typedValue['option_id'] : null;
|
||||
|
||||
if ($existingId) {
|
||||
$updated = DB::update(
|
||||
'update user_custom_field_values set value_text = ?, value_bool = ?, value_date = ?, option_id = ? where id = ?',
|
||||
$valueText,
|
||||
$valueBool !== null ? (string) ((int) $valueBool) : null,
|
||||
$valueDate,
|
||||
$optionId !== null ? (string) ((int) $optionId) : null,
|
||||
(string) ((int) $existingId)
|
||||
);
|
||||
return $updated !== false ? (int) $existingId : false;
|
||||
}
|
||||
|
||||
return DB::insert(
|
||||
'insert into user_custom_field_values (user_id, definition_id, value_text, value_bool, value_date, option_id, created) values (?,?,?,?,?,?,NOW())',
|
||||
(string) $userId,
|
||||
(string) $definitionId,
|
||||
$valueText,
|
||||
$valueBool !== null ? (string) ((int) $valueBool) : null,
|
||||
$valueDate,
|
||||
$optionId !== null ? (string) ((int) $optionId) : null
|
||||
);
|
||||
}
|
||||
|
||||
public static function deleteByUserAndDefinitionIds(int $userId, array $definitionIds): bool
|
||||
{
|
||||
if ($userId <= 0) {
|
||||
return false;
|
||||
}
|
||||
$definitionIds = array_values(array_unique(array_map('intval', $definitionIds)));
|
||||
$definitionIds = array_values(array_filter($definitionIds, static fn ($id) => $id > 0));
|
||||
if (!$definitionIds) {
|
||||
return true;
|
||||
}
|
||||
$result = DB::delete(
|
||||
'delete from user_custom_field_values where user_id = ? and definition_id in (???)',
|
||||
(string) $userId,
|
||||
$definitionIds
|
||||
);
|
||||
return $result !== false;
|
||||
}
|
||||
|
||||
public static function deleteByUserOutsideTenantIds(int $userId, array $tenantIds): bool
|
||||
{
|
||||
if ($userId <= 0) {
|
||||
return false;
|
||||
}
|
||||
$tenantIds = array_values(array_unique(array_map('intval', $tenantIds)));
|
||||
$tenantIds = array_values(array_filter($tenantIds, static fn ($id) => $id > 0));
|
||||
if (!$tenantIds) {
|
||||
$result = DB::delete('delete from user_custom_field_values where user_id = ?', (string) $userId);
|
||||
return $result !== false;
|
||||
}
|
||||
$result = DB::delete(
|
||||
'delete ucfv from user_custom_field_values ucfv ' .
|
||||
'join tenant_custom_field_definitions d on d.id = ucfv.definition_id ' .
|
||||
'where ucfv.user_id = ? and d.tenant_id not in (???)',
|
||||
(string) $userId,
|
||||
$tenantIds
|
||||
);
|
||||
return $result !== false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user