78 lines
1.6 KiB
PHP
78 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Repositories\Tool;
|
|
|
|
use Exception;
|
|
|
|
class Tool
|
|
{
|
|
public string $name;
|
|
|
|
public string $homepage_url;
|
|
|
|
public string $callback_url;
|
|
|
|
public string $description;
|
|
|
|
public int $tool_id;
|
|
|
|
public array $tool_functions;
|
|
|
|
private array $data;
|
|
|
|
/**
|
|
* @throws Exception
|
|
*/
|
|
public function __construct(int $tool_id, array $data)
|
|
{
|
|
$this->data = $data;
|
|
$this->tool_id = $tool_id;
|
|
$this->parse();
|
|
}
|
|
|
|
/**
|
|
* @throws Exception
|
|
*/
|
|
private function parse(): void
|
|
{
|
|
// 验证数据
|
|
if (! $this->validate()) {
|
|
throw new Exception('Invalid data');
|
|
}
|
|
|
|
$this->name = $this->data['name'];
|
|
$this->description = $this->data['description'];
|
|
$this->homepage_url = $this->data['homepage_url'];
|
|
$this->callback_url = $this->data['callback_url'];
|
|
$this->fetchFunctions();
|
|
}
|
|
|
|
private function validate(): bool
|
|
{
|
|
// all fields are required
|
|
if (empty($this->data['name']) ||
|
|
empty($this->data['description']) ||
|
|
empty($this->data['homepage_url']) ||
|
|
empty($this->data['callback_url'])) {
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* @throws Exception
|
|
*/
|
|
private function fetchFunctions(): void
|
|
{
|
|
foreach ($this->data['functions'] as $f) {
|
|
$a = [
|
|
'type' => 'function',
|
|
'function' => new ToolFunction($f),
|
|
];
|
|
|
|
$this->tool_functions[] = $a;
|
|
}
|
|
}
|
|
}
|