Files
componentsPC/app/Services/BuildValidator.php

83 lines
3.3 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Services;
use App\Models\Component;
class BuildValidator
{
/**
* Проверяет базовую совместимость компонентов в сборке.
*
* @param array $componentIds Массив ID компонентов
* @return array ['valid' => bool, 'errors' => array, 'warnings' => array]
*/
public function validateCompatibility(array $componentIds)
{
$components = Component::whereIn('id', $componentIds)->get();
if ($components->count() < 2) {
return [
'valid' => true,
'errors' => [],
'warnings' => []
];
}
$errors = [];
$warnings = [];
// Определяем типы компонентов по component_type_id (адаптируйте под вашу логику!)
$cpu = $components->firstWhere('component_type_id', 1); // 1 = CPU
$motherboard = $components->firstWhere('component_type_id', 3); // 3 = Motherboard
$ram = $components->firstWhere('component_type_id', 4); // 4 = RAM
$psu = $components->firstWhere('component_type_id', 5); // 5 = PSU
// 1. Проверка сокета (CPU ↔ Motherboard)
if ($cpu && $motherboard) {
$cpuSocket = $cpu->specifications['socket'] ?? null;
$mbSocket = $motherboard->specifications['socket'] ?? null;
if (!$cpuSocket || !$mbSocket) {
$warnings[] = "Не указан сокет для процессора или материнской платы.";
} elseif ($cpuSocket !== $mbSocket) {
$errors[] = "Сокет процессора '{$cpuSocket}' не совместим со сокетом материнской платы '{$mbSocket}'.";
}
}
// 2. Проверка типа ОЗУ (RAM ↔ Motherboard)
if ($ram && $motherboard) {
$ramType = $ram->specifications['type'] ?? null;
$mbRamType = $motherboard->specifications['memory_type'] ?? $motherboard->specifications['type'] ?? null;
if (!$ramType || !$mbRamType) {
$warnings[] = "Не указан тип памяти для ОЗУ или материнской платы.";
} elseif ($ramType !== $mbRamType) {
$errors[] = "Тип ОЗУ '{$ramType}' не совместим с типом памяти материнской платы '{$mbRamType}'.";
}
}
// 3. Проверка мощности БП (PSU ≥ сумма TDP)
if ($psu) {
$totalTdp = 0;
foreach ($components as $component) {
// Берём TDP из specifications, или 0 если нет
$tdp = $component->specifications['tdp'] ?? 0;
$totalTdp += (int) $tdp;
}
$psuWattage = $psu->specifications['wattage'] ?? 0;
if ($psuWattage < $totalTdp) {
$errors[] = "Мощность БП ({$psuWattage} Вт) < суммарного TDP ({$totalTdp} Вт). Рекомендуется ≥ {$totalTdp} Вт.";
}
}
return [
'valid' => empty($errors),
'errors' => $errors,
'warnings' => $warnings
];
}
}