50 lines
991 B
PHP
50 lines
991 B
PHP
|
<?php
|
||
|
|
||
|
namespace App\Repositories\Tool;
|
||
|
|
||
|
use Exception;
|
||
|
|
||
|
class Tool
|
||
|
{
|
||
|
public string $name;
|
||
|
|
||
|
public string $url;
|
||
|
|
||
|
public string $description;
|
||
|
|
||
|
public string $api_key;
|
||
|
|
||
|
public string $user_id;
|
||
|
|
||
|
/**
|
||
|
* @throws Exception
|
||
|
*/
|
||
|
public function parse(array $data): void
|
||
|
{
|
||
|
// 验证数据
|
||
|
if (! $this->validate($data)) {
|
||
|
throw new Exception('Invalid data');
|
||
|
}
|
||
|
|
||
|
$this->name = $data['name'];
|
||
|
$this->url = $data['url'];
|
||
|
$this->description = $data['description'];
|
||
|
$this->api_key = $data['api_key'];
|
||
|
$this->user_id = $data['user_id'];
|
||
|
}
|
||
|
|
||
|
public function validate(array $data): bool
|
||
|
{
|
||
|
// all fields are required
|
||
|
if (empty($data['name']) ||
|
||
|
empty($data['url']) ||
|
||
|
empty($data['description']) ||
|
||
|
empty($data['api_key']) ||
|
||
|
empty($data['user_id'])) {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
}
|