'int', 'plan_id' => 'int', 'resource_pool_id' => 'int', 'price' => 'decimal:2', 'price_version' => 'int', 'revision' => 'int', 'status' => 'boolean', 'stock' => 'int', 'sort' => 'int', 'created_at' => 'int', 'updated_at' => 'int', ]; public static function validateOffer(array $input): array { $poolId = filter_var($input['resource_pool_id'] ?? null, FILTER_VALIDATE_INT); if ($poolId === false || $poolId < 1) { throw new \InvalidArgumentException('请选择有效的资源池'); } $code = isset($input['code']) && is_string($input['code']) ? trim($input['code']) : ''; if (preg_match('/^[a-z0-9][a-z0-9_-]{0,63}$/D', $code) !== 1) { throw new \InvalidArgumentException('销售方案代码需为 1 至 64 位小写字母、数字、下划线或连字符'); } $name = isset($input['name']) && is_string($input['name']) ? trim($input['name']) : ''; if ($name === '' || mb_strlen($name) > 128) { throw new \InvalidArgumentException('销售方案名称需为 1 至 128 个字符'); } $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); $whole = ltrim($parts[0], '0'); $normalizedPrice = ($whole === '' ? '0' : $whole) . '.' . str_pad($parts[1] ?? '', 2, '0'); $data = [ 'resource_pool_id' => (int) $poolId, 'code' => $code, 'name' => $name, 'price' => $normalizedPrice, 'currency' => 'USD', ]; foreach ([ 'status' => [0, 1, 1, '销售状态'], 'stock' => [-1, 2147483647, -1, '库存'], 'sort' => [-1000000, 1000000, 0, '排序'], ] as $field => [$minimum, $maximum, $default, $label]) { $raw = $input[$field] ?? $default; $value = is_string($raw) || is_int($raw) ? filter_var($raw, FILTER_VALIDATE_INT) : false; if ($value === false || $value < $minimum || $value > $maximum) { throw new \InvalidArgumentException( $label . ' 需为 ' . $minimum . ' 至 ' . $maximum . ' 的整数' ); } $data[$field] = (int) $value; } return $data; } }