62 خطوط
2.1 KiB
PHP
62 خطوط
2.1 KiB
PHP
<?php
|
|
|
|
namespace App\Modules\Courses\Application;
|
|
|
|
final readonly class BlockDefinition
|
|
{
|
|
/**
|
|
* @param array<string, mixed> $defaultData
|
|
* @param array<string, list<mixed>> $rules
|
|
* @param list<string> $capabilities
|
|
* @param list<string> $webBehavior
|
|
* @param array<string, string> $exportCompatibility
|
|
* @param array<int, callable(array<string, mixed>): array<string, mixed>> $migrations
|
|
*/
|
|
public function __construct(
|
|
public string $type,
|
|
public string $category,
|
|
public string $label,
|
|
public string $icon,
|
|
public int $schemaVersion,
|
|
public array $defaultData,
|
|
public array $rules,
|
|
public array $capabilities = [],
|
|
public array $webBehavior = ['responsive'],
|
|
public array $exportCompatibility = ['pdf' => 'native', 'scorm' => 'native', 'mp4' => 'static'],
|
|
public array $migrations = [],
|
|
) {}
|
|
|
|
/** @return array<string, mixed> */
|
|
public function metadata(): array
|
|
{
|
|
return [
|
|
'type' => $this->type,
|
|
'category' => $this->category,
|
|
'label' => $this->label,
|
|
'icon' => $this->icon,
|
|
'schemaVersion' => $this->schemaVersion,
|
|
'defaultData' => $this->defaultData,
|
|
'capabilities' => $this->capabilities,
|
|
'webBehavior' => $this->webBehavior,
|
|
'exportCompatibility' => $this->exportCompatibility,
|
|
'latestSchemaVersion' => $this->schemaVersion,
|
|
];
|
|
}
|
|
|
|
/** @param array<string, mixed> $data @return array{version: int, data: array<string, mixed>} */
|
|
public function migrate(int $fromVersion, array $data): array
|
|
{
|
|
$version = $fromVersion;
|
|
while ($version < $this->schemaVersion) {
|
|
$migration = $this->migrations[$version] ?? null;
|
|
if (! $migration) {
|
|
throw new \LogicException("Missing {$this->type} schema migration from version {$version}.");
|
|
}
|
|
$data = $migration($data);
|
|
$version++;
|
|
}
|
|
|
|
return ['version' => $version, 'data' => $data];
|
|
}
|
|
}
|