|
|
<?php
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
class Shop extends Model
|
|
|
{
|
|
|
public const GIB = 1073741824;
|
|
|
public const TIB = 1099511627776;
|
|
|
|
|
|
protected $connection = 'default';
|
|
|
protected $table = 'shop';
|
|
|
|
|
|
protected $casts = [
|
|
|
'host_id' => 'int',
|
|
|
'cpu_cores' => 'int', 'memory_mib' => 'int', 'bandwidth_mbps' => 'int',
|
|
|
'disk_gib' => 'int', 'validity_days' => 'int', 'traffic_quota_bytes' => 'int',
|
|
|
'status' => 'int', 'stock' => 'int', 'sort' => 'int',
|
|
|
'price' => 'decimal:2',
|
|
|
];
|
|
|
|
|
|
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];
|
|
|
$host = $input['host_id'] ?? null;
|
|
|
if ($host === null || $host === '') {
|
|
|
$data['host_id'] = null;
|
|
|
} else {
|
|
|
$host = filter_var($host, FILTER_VALIDATE_INT);
|
|
|
if ($host === false || $host < 1) {
|
|
|
throw new \InvalidArgumentException('请选择有效的宿主机');
|
|
|
}
|
|
|
$data['host_id'] = $host;
|
|
|
}
|
|
|
foreach ([
|
|
|
'cpu_cores' => [1, 64],
|
|
|
'bandwidth_mbps' => [1, 100000], 'disk_gib' => [1, 4096],
|
|
|
'validity_days' => [1, 3650],
|
|
|
'status' => [0, 1], 'stock' => [-1, 2147483647], 'sort' => [-1000000, 1000000],
|
|
|
] as $field => [$minimum, $maximum]) {
|
|
|
$raw = $input[$field] ?? (['status' => 1, 'stock' => -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;
|
|
|
$price = $input['price'] ?? '';
|
|
|
if ((!is_string($price) && !is_int($price)) || preg_match('/^\d{1,10}(?:\.\d{1,2})?$/D', (string) $price) !== 1) {
|
|
|
throw new \InvalidArgumentException('价格需为非负金额,最多两位小数');
|
|
|
}
|
|
|
$parts = explode('.', (string) $price);
|
|
|
$data['price'] = ltrim($parts[0], '0');
|
|
|
if ($data['price'] === '') {
|
|
|
$data['price'] = '0';
|
|
|
}
|
|
|
$data['price'] .= '.' . str_pad($parts[1] ?? '', 2, '0');
|
|
|
|
|
|
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;
|
|
|
}
|
|
|
}
|