type = $this->fixTypeName($type); if ($this->type === 'bool') { if ($this->default === '') { $this->default = 'yes'; } if (!in_array($this->default, array_merge(TRUE_LIST, FALSE_LIST))) { throw new InvalidArgumentException('CommandArgument参数 ' . $name . ' 类型传入类型应为布尔型,检测到非法的默认值 ' . $this->default); } } elseif ($this->type === 'number') { if ($this->default === '') { $this->default = '0'; } if (!is_numeric($this->default)) { throw new InvalidArgumentException('CommandArgument参数 ' . $name . ' 类型传入类型应为数字型,检测到非法的默认值 ' . $this->default); } } } public function getTypeErrorPrompt(): string { return '参数类型错误,请重新输入!'; } public function getErrorQuitPrompt(): string { return '参数类型错误,停止输入!'; } /** * @throws ZMKnownException */ protected function fixTypeName(string $type): string { $table = [ 'str' => 'string', 'string' => 'string', 'strings' => 'string', 'byte' => 'string', 'num' => 'number', 'number' => 'number', 'int' => 'number', 'float' => 'number', 'double' => 'number', 'boolean' => 'bool', 'bool' => 'bool', 'true' => 'bool', 'any' => 'any', 'all' => 'any', '*' => 'any', ]; if (array_key_exists($type, $table)) { return $table[$type]; } // 说明是自定义类型 if (mb_strpos($type, '.') === 0 && mb_strlen($type) >= 2) { return $type; } throw new ZMKnownException(zm_internal_errcode('E00077') . 'Invalid argument type: ' . $type . ', only support any, string, number and bool !'); } }