You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/web/app/Models/Shop.php

106 lines
4.0 KiB
PHTML

<?php
namespace App\Models;
class Shop extends Model
{
public const GIB = 1073741824;
public const TIB = 1099511627776;
protected $connection = 'default';
protected $table = 'shop';
protected $casts = [
'cpu_cores' => 'int', 'memory_mib' => 'int', 'bandwidth_mbps' => 'int',
'disk_gib' => 'int', 'validity_days' => 'int', 'traffic_quota_bytes' => 'int',
'status' => 'int', 'sort' => 'int',
'created_at' => 'int', 'updated_at' => 'int',
];
public function content()
{
return sprintf('%d 核 CPU · %s GB 内存 · %d Mbps 带宽 · %d GB 磁盘 · %d 天 · %s',
$this->cpu_cores, $this->memoryGB(), $this->bandwidth_mbps, $this->disk_gib,
$this->validity_days, $this->trafficQuotaText());
}
public function memoryGB(): string
{
return self::formatUnit((int) $this->memory_mib, 1024, 1);
}
public function trafficGiB(): int
{
return intdiv(max(0, (int) $this->traffic_quota_bytes), self::GIB);
}
public function trafficTB(): string
{
return self::formatUnit(max(0, (int) $this->traffic_quota_bytes), self::TIB, 10);
}
public function trafficQuotaText(): string
{
return (int) $this->traffic_quota_bytes === 0 ? '不限流量' : $this->trafficTB() . ' TB 流量';
}
public static function validatePlan(array $input): array
{
$name = isset($input['name']) && is_string($input['name']) ? trim($input['name']) : '';
if ($name === '' || mb_strlen($name) > 128) {
throw new \InvalidArgumentException('套餐名称需为 1 至 128 个字符');
}
$data = ['name' => $name];
foreach ([
'cpu_cores' => [1, 64],
'bandwidth_mbps' => [1, 100000], 'disk_gib' => [1, 4096],
'validity_days' => [1, 3650],
'status' => [0, 1], 'sort' => [-1000000, 1000000],
] as $field => [$minimum, $maximum]) {
$raw = $input[$field] ?? (['status' => 1, 'sort' => 0][$field] ?? null);
$value = is_string($raw) || is_int($raw) ? filter_var($raw, FILTER_VALIDATE_INT) : false;
if ($value === false || $value < $minimum || $value > $maximum) {
throw new \InvalidArgumentException($field . ' 需为 ' . $minimum . ' 至 ' . $maximum . ' 的整数');
}
$data[$field] = $value;
}
$memoryGB = self::decimalInput($input['memory_gb'] ?? null, '内存', 0.5, 256, 1);
$memoryHalfGB = $memoryGB * 2;
if (abs($memoryHalfGB - round($memoryHalfGB)) > 0.0000001) {
throw new \InvalidArgumentException('内存必须以 0.5 GB 为步进');
}
$data['memory_mib'] = (int) round($memoryGB * 1024);
$trafficTB = self::decimalInput($input['traffic_tb'] ?? null, '每期流量额度', 0, 1024, 10);
$trafficGiB = $trafficTB * 1024;
if (abs($trafficGiB - round($trafficGiB)) > 0.0000001) {
throw new \InvalidArgumentException('每期流量额度的最小步进为 1 GiB(1/1024 TB)');
}
$data['traffic_quota_bytes'] = (int) round($trafficGiB) * self::GIB;
return $data;
}
private static function decimalInput($raw, string $label, float $minimum, float $maximum, int $precision): float
{
if ((!is_string($raw) && !is_int($raw) && !is_float($raw))
|| preg_match('/^\d{1,4}(?:\.\d{1,' . $precision . '})?$/D', (string) $raw) !== 1) {
throw new \InvalidArgumentException($label . '格式无效');
}
$value = (float) $raw;
if (!is_finite($value) || $value < $minimum || $value > $maximum) {
throw new \InvalidArgumentException($label . ' 需为 ' . $minimum . ' 至 ' . $maximum);
}
return $value;
}
private static function formatUnit(int $value, int $unit, int $precision): string
{
$formatted = number_format($value / $unit, $precision, '.', '');
$formatted = rtrim(rtrim($formatted, '0'), '.');
return $formatted === '' ? '0' : $formatted;
}
}