1
0
Files
breadcrumb-the-shire/lib/Repository/Support/RepositoryArrayHelper.php
fs c609753570 refactor(tests): remove redundant tests and fix assertion style
Remove subset/duplicate architecture tests already covered by broader
checks, and replace assertTrue(true) with self::addToAssertionCount(1)
for explicit no-exception-thrown intent.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-25 18:46:49 +01:00

97 lines
2.4 KiB
PHP

<?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));
}
/**
* Flatten a MintyPHP DB row that can contain nested table-key arrays.
*
* @return array<string, mixed>
*/
public static function flattenRow(mixed $row): array
{
if (!is_array($row)) {
return [];
}
$flat = [];
foreach ($row as $key => $value) {
if (is_array($value)) {
$flat = array_merge($flat, $value);
} elseif (is_string($key)) {
$flat[$key] = $value;
}
}
return $flat;
}
}