php-spark/plugins/com.noccy.apiclient/Api/Catalog.php

85 lines
1.9 KiB
PHP

<?php // "name":"Call on web APIs", "author":"Noccy"
namespace SparkPlug\Com\Noccy\ApiClient\Api;
use JsonSerializable;
class Catalog implements JsonSerializable
{
private array $properties = [];
private array $methods = [];
private ?string $name;
private ?string $info;
public function __construct(array $catalog=[])
{
$catalog = $catalog['catalog']??[];
$this->name = $catalog['name']??null;
$this->info = $catalog['info']??null;
foreach ($catalog['props']??[] as $k=>$v) {
$this->properties[$k] = $v;
}
foreach ($catalog['methods']??[] as $k=>$v) {
$this->methods[$k] = new Method($v);
}
}
public static function createFromFile(string $filename): Catalog
{
$json = file_get_contents($filename);
$catalog = json_decode($json, true);
$catalog['name'] = basename($filename, ".json");
return new Catalog($catalog);
}
public function getName(): ?string
{
return $this->name;
}
public function getInfo(): ?string
{
return $this->info;
}
public function getProperties(): array
{
return $this->properties;
}
public function applyProperties(array $props)
{
$this->properties = array_merge($this->properties, $props);
}
public function addMethod(string $name, Method $method)
{
$this->methods[$name] = $method;
}
public function getMethod(string $method): ?Method
{
return $this->methods[$method]??null;
}
public function getMethods(): array
{
return $this->methods;
}
public function jsonSerialize(): mixed
{
return [
'catalog' => [
'name' => $this->name,
'info' => $this->info,
'props' => $this->properties,
'methods' => $this->methods,
]
];
}
}