amber-laravel/app/Repositories/Tool/Tool.php

68 lines
1.4 KiB
PHP
Raw Normal View History

2024-07-23 16:40:56 +00:00
<?php
namespace App\Repositories\Tool;
use Exception;
class Tool
{
public string $name;
2024-07-23 17:35:03 +00:00
public string $homepage_url;
public string $callback_url;
2024-07-23 16:40:56 +00:00
public string $description;
2024-07-23 17:35:03 +00:00
public array $toolFunctions;
2024-07-23 16:40:56 +00:00
2024-07-23 17:35:03 +00:00
private array $data;
2024-07-23 16:40:56 +00:00
/**
* @throws Exception
*/
2024-07-23 17:35:03 +00:00
public function __construct(array $data)
{
$this->data = $data;
$this->parse();
}
/**
* @throws Exception
*/
private function parse(): void
2024-07-23 16:40:56 +00:00
{
// 验证数据
2024-07-23 17:35:03 +00:00
if (! $this->validate()) {
2024-07-23 16:40:56 +00:00
throw new Exception('Invalid data');
}
2024-07-23 17:35:03 +00:00
$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();
2024-07-23 16:40:56 +00:00
}
2024-07-23 17:35:03 +00:00
private function validate(): bool
2024-07-23 16:40:56 +00:00
{
// all fields are required
2024-07-23 17:35:03 +00:00
if (empty($this->data['name']) ||
empty($this->data['description']) ||
empty($this->data['homepage_url']) ||
empty($this->data['callback_url'])) {
2024-07-23 16:40:56 +00:00
return false;
}
return true;
}
2024-07-23 17:35:03 +00:00
/**
* @throws Exception
*/
private function fetchFunctions(): void
{
foreach ($this->data['functions'] as $f) {
$this->toolFunctions[] = new ToolFunction($f);
}
}
2024-07-23 16:40:56 +00:00
}