Files
pf2e-builder/src/Command/Pf2eSchemaGenerator/SchemaItem.php
T
2024-12-10 00:27:24 +04:00

72 lines
1.6 KiB
PHP

<?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 (!!is_null($this->elements)) {
if (str_contains($this->type, "[]")) {
foreach ($value as $element) {
if (!in_array($element, $this->elements)) {
$this->elements[] = $element;
}
}
}
else if (!in_array($value, $this->elements)) {
$this->elements[] = $value;
}
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;
}
}