116 lines
3.9 KiB
PHP
116 lines
3.9 KiB
PHP
<?php
|
|
|
|
namespace Toolbox;
|
|
|
|
final class Component
|
|
{
|
|
/**
|
|
* @param list<string> $files
|
|
* @param array<string, string> $requires
|
|
* @param array<string, string>|null $destinations
|
|
*/
|
|
public function __construct(
|
|
public readonly string $name,
|
|
public readonly array $files,
|
|
public readonly array $requires = [],
|
|
public readonly ?string $coreRequires = null,
|
|
public readonly ?string $coreTested = null,
|
|
public readonly ?string $description = null,
|
|
public readonly ?array $destinations = null,
|
|
public readonly bool $override = false,
|
|
public readonly bool $coreDeclared = false,
|
|
public readonly ?bool $coreIndependent = null,
|
|
) {
|
|
}
|
|
|
|
public static function fromArray(string $name, array $data): self
|
|
{
|
|
if (!isset($data['files']) || !is_array($data['files']) || $data['files'] === []) {
|
|
throw new ToolboxException("El componente '{$name}' debe declarar 'files' no vacío");
|
|
}
|
|
|
|
foreach ($data['files'] as $file) {
|
|
if (!is_string($file) || $file === '') {
|
|
throw new ToolboxException("El componente '{$name}' tiene un archivo inválido");
|
|
}
|
|
}
|
|
|
|
$requires = [];
|
|
foreach (($data['require'] ?? []) as $id => $constraint) {
|
|
if (!is_string($id) || !is_string($constraint)) {
|
|
throw new ToolboxException("El componente '{$name}' tiene un 'require' inválido");
|
|
}
|
|
$requires[$id] = $constraint;
|
|
}
|
|
|
|
$destinations = null;
|
|
if (isset($data['destinations'])) {
|
|
if (!is_array($data['destinations'])) {
|
|
throw new ToolboxException("El componente '{$name}' tiene 'destinations' inválido");
|
|
}
|
|
$destinations = [];
|
|
foreach ($data['destinations'] as $from => $to) {
|
|
$destinations[$from] = $to;
|
|
}
|
|
}
|
|
|
|
$description = null;
|
|
if (isset($data['description'])) {
|
|
if (!is_string($data['description'])) {
|
|
throw new ToolboxException("El componente '{$name}' tiene un 'description' inválido");
|
|
}
|
|
|
|
$description = $data['description'];
|
|
}
|
|
|
|
$coreDeclared = array_key_exists('core', $data);
|
|
$coreIndependent = null;
|
|
$coreRequires = null;
|
|
$coreTested = null;
|
|
if ($coreDeclared) {
|
|
[$coreRequires, $coreTested, $coreIndependent] = self::parseCore($data['core'], "el componente '{$name}'");
|
|
}
|
|
|
|
return new self(
|
|
name: $name,
|
|
files: array_values($data['files']),
|
|
requires: $requires,
|
|
coreRequires: $coreRequires,
|
|
coreTested: $coreTested,
|
|
description: $description,
|
|
destinations: $destinations,
|
|
override: (bool) ($data['override'] ?? false),
|
|
coreDeclared: $coreDeclared,
|
|
coreIndependent: $coreIndependent,
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Bloque `core`: objeto de rango ({requires?, tested?}) o la cadena
|
|
* "none" (independencia afirmativa, excluyente con el rango).
|
|
*
|
|
* @return array{0: ?string, 1: ?string, 2: ?bool}
|
|
*/
|
|
public static function parseCore(mixed $core, string $origin): array
|
|
{
|
|
if ($core === 'none') {
|
|
return [null, null, true];
|
|
}
|
|
|
|
if (!is_array($core)) {
|
|
throw new ToolboxException("{$origin} tiene un 'core' inválido");
|
|
}
|
|
|
|
$requires = $core['requires'] ?? null;
|
|
$tested = $core['tested'] ?? null;
|
|
|
|
foreach (['requires' => $requires, 'tested' => $tested] as $key => $value) {
|
|
if ($value !== null && !is_string($value)) {
|
|
throw new ToolboxException("{$origin} tiene un 'core.{$key}' inválido");
|
|
}
|
|
}
|
|
|
|
return [$requires, $tested, null];
|
|
}
|
|
}
|