<?php
function filter_module_values(array $raw): array {
    $data = array_filter([
        'voltage_mv' => $raw['voltage_mv'] ?? null,
        'current_ma' => $raw['current_ma'] ?? null,
        'soc_percent' => $raw['soc_percent'] ?? null,
    ], fn($v) => $v !== null);

    $cellVoltages = array_values(array_filter(
        $raw['cells'] ?? [],
        fn($value) => $value >= 1000 && $value <= 5000
    ));

    $data['cell_count'] = count($cellVoltages);
    return $data;
}

print_r(filter_module_values([
    'voltage_mv' => 52000,
    'current_ma' => null,
    'soc_percent' => 87,
    'cells' => [999, 3301, 3302, 5001],
]));
