94 lines
2.2 KiB
PHP
94 lines
2.2 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Models\Design;
|
||
|
|
|
||
|
|
use Illuminate\Database\Eloquent\Model;
|
||
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
||
|
|
use App\Traits\BelongsToTenant;
|
||
|
|
|
||
|
|
class ModelParameter extends Model
|
||
|
|
{
|
||
|
|
use SoftDeletes, BelongsToTenant;
|
||
|
|
|
||
|
|
protected $table = 'model_parameters';
|
||
|
|
|
||
|
|
protected $fillable = [
|
||
|
|
'tenant_id',
|
||
|
|
'model_id',
|
||
|
|
'parameter_name',
|
||
|
|
'parameter_type',
|
||
|
|
'is_required',
|
||
|
|
'default_value',
|
||
|
|
'min_value',
|
||
|
|
'max_value',
|
||
|
|
'unit',
|
||
|
|
'options',
|
||
|
|
'description',
|
||
|
|
'sort_order',
|
||
|
|
];
|
||
|
|
|
||
|
|
protected $casts = [
|
||
|
|
'is_required' => 'boolean',
|
||
|
|
'min_value' => 'decimal:6',
|
||
|
|
'max_value' => 'decimal:6',
|
||
|
|
'options' => 'array',
|
||
|
|
'sort_order' => 'integer',
|
||
|
|
];
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 매개변수가 속한 모델
|
||
|
|
*/
|
||
|
|
public function designModel()
|
||
|
|
{
|
||
|
|
return $this->belongsTo(DesignModel::class, 'model_id');
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 매개변수 타입별 검증
|
||
|
|
*/
|
||
|
|
public function validateValue($value)
|
||
|
|
{
|
||
|
|
switch ($this->parameter_type) {
|
||
|
|
case 'NUMBER':
|
||
|
|
if (!is_numeric($value)) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
if ($this->min_value !== null && $value < $this->min_value) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
if ($this->max_value !== null && $value > $this->max_value) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
|
||
|
|
case 'SELECT':
|
||
|
|
return in_array($value, $this->options ?? []);
|
||
|
|
|
||
|
|
case 'BOOLEAN':
|
||
|
|
return is_bool($value) || in_array($value, [0, 1, '0', '1', 'true', 'false']);
|
||
|
|
|
||
|
|
case 'TEXT':
|
||
|
|
return is_string($value);
|
||
|
|
|
||
|
|
default:
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 매개변수 값 형변환
|
||
|
|
*/
|
||
|
|
public function castValue($value)
|
||
|
|
{
|
||
|
|
switch ($this->parameter_type) {
|
||
|
|
case 'NUMBER':
|
||
|
|
return (float) $value;
|
||
|
|
case 'BOOLEAN':
|
||
|
|
return (bool) $value;
|
||
|
|
case 'TEXT':
|
||
|
|
case 'SELECT':
|
||
|
|
default:
|
||
|
|
return (string) $value;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|