This commit is contained in:
2024-12-09 19:12:27 +04:00
parent 78094e41c1
commit a6f655d80f
10 changed files with 574 additions and 22 deletions
@@ -0,0 +1,68 @@
<?php
namespace App\Command\Pf2eSchemaGenerator;
class SchemaItem
{
public string $name;
public string $type = "";
public bool $required = true;
public ?array $elements = [];
public function __construct($name)
{
$this->name = $name;
}
public function disableRequired(): void
{
$this->required = false;
}
public function adapt(mixed $value): void
{
$type = Schema::resolveType($value);
$this->type = Schema::mergeTypes($this->type, $type);
if (str_contains($this->type, "[]") && !is_null($this->elements)) {
foreach ($value as $element) {
if (!in_array($element, $this->elements)) {
$this->elements[] = $element;
}
}
if (count($this->elements) > 20) {
$this->elements = null;
}
}
}
public function strings(): array {
$name = $this->name;
if (!$this->required) {
$name = "[{$name}]";
}
$rows = [
sprintf("%s: %s", $name, $this->type),
];
if (!empty($this->elements)) {
sort($this->elements);
$elements = [];
foreach ($this->elements as $element) {
$elements[] = is_string($element) ? "\"{$element}\"" : strval($element);
}
$rows[] = " Elements: [ " . implode(", ", $elements) . " ]";
}
return $rows;
}
}