Files
sam-api/app/Models/Design/ModelFormula.php

122 lines
3.6 KiB
PHP
Raw Normal View History

<?php
namespace App\Models\Design;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\Traits\BelongsToTenant;
class ModelFormula extends Model
{
use SoftDeletes, BelongsToTenant;
protected $table = 'model_formulas';
protected $fillable = [
'tenant_id',
'model_id',
'formula_name',
'formula_expression',
'unit',
'description',
'calculation_order',
'dependencies',
];
protected $casts = [
'calculation_order' => 'integer',
'dependencies' => 'array',
];
/**
* 공식이 속한 모델
*/
public function designModel()
{
return $this->belongsTo(DesignModel::class, 'model_id');
}
/**
* 공식에서 변수 추출
*/
public function extractVariables(): array
{
// 간단한 변수 추출 (영문자로 시작하는 단어들)
preg_match_all('/\b[a-zA-Z][a-zA-Z0-9_]*\b/', $this->formula_expression, $matches);
// 수학 함수 제외
$mathFunctions = ['sin', 'cos', 'tan', 'log', 'exp', 'sqrt', 'pow', 'abs', 'ceil', 'floor', 'round', 'max', 'min'];
$variables = array_diff($matches[0], $mathFunctions);
return array_unique($variables);
}
/**
* 공식 계산 (안전한 eval 대신 간단한 파서 사용)
*/
public function calculate(array $values): float
{
$expression = $this->formula_expression;
// 변수를 값으로 치환
foreach ($values as $variable => $value) {
$expression = str_replace($variable, (string) $value, $expression);
}
// 간단한 수식 계산 (보안상 eval 사용 금지)
return $this->evaluateSimpleExpression($expression);
}
/**
* 간단한 수식 계산기 (덧셈, 뺄셈, 곱셈, 나눗셈)
*/
private function evaluateSimpleExpression(string $expression): float
{
// 공백 제거
$expression = preg_replace('/\s+/', '', $expression);
// 간단한 사칙연산만 허용
if (!preg_match('/^[0-9+\-*\/\(\)\.]+$/', $expression)) {
throw new \InvalidArgumentException('Invalid expression: ' . $expression);
}
// 안전한 계산을 위해 제한된 연산만 허용
try {
// 실제 프로덕션에서는 더 안전한 수식 파서 라이브러리 사용 권장
return (float) eval("return $expression;");
} catch (\Throwable $e) {
throw new \InvalidArgumentException('Formula calculation error: ' . $e->getMessage());
}
}
/**
* 의존성 순환 체크
*/
public function hasCircularDependency(array $allFormulas): bool
{
$visited = [];
$recursionStack = [];
return $this->dfsCheckCircular($allFormulas, $visited, $recursionStack);
}
private function dfsCheckCircular(array $allFormulas, array &$visited, array &$recursionStack): bool
{
$visited[$this->formula_name] = true;
$recursionStack[$this->formula_name] = true;
foreach ($this->dependencies as $dependency) {
if (!isset($visited[$dependency])) {
$dependentFormula = collect($allFormulas)->firstWhere('formula_name', $dependency);
if ($dependentFormula && $dependentFormula->dfsCheckCircular($allFormulas, $visited, $recursionStack)) {
return true;
}
} elseif (isset($recursionStack[$dependency])) {
return true; // 순환 의존성 발견
}
}
unset($recursionStack[$this->formula_name]);
return false;
}
}