add element persistence

This commit is contained in:
Yisroel Baum 2026-08-08 22:21:25 +03:00
parent aaab0e5379
commit fcc6ade8f3
Signed by: yisroelbaum
GPG key ID: 0FA60884F75520A9
7 changed files with 289 additions and 0 deletions

View file

@ -0,0 +1,15 @@
<?php
namespace App\Element;
use App\Set\Set;
final readonly class CreateElementDto
{
public function __construct(
public Set $set,
public string $name,
public string $kind,
public ?Element $parentElement,
) {}
}

View file

@ -0,0 +1,47 @@
<?php
namespace App\Element;
use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
/**
* @property int $id
* @property int $set_id
* @property string $name
* @property string $kind
* @property int|null $parent_element_id
* @property int $position
*
* @method static Builder<static>|ElementModel newModelQuery()
* @method static Builder<static>|ElementModel newQuery()
* @method static Builder<static>|ElementModel query()
*
* @mixin \Eloquent
*/
#[Fillable([
'set_id',
'name',
'kind',
'parent_element_id',
'position',
])]
class ElementModel extends Model
{
protected $table = 'elements';
public $timestamps = false;
/**
* @return array<string, string>
*/
protected function casts(): array
{
return [
'set_id' => 'integer',
'parent_element_id' => 'integer',
'position' => 'integer',
];
}
}

View file

@ -0,0 +1,10 @@
<?php
namespace App\Element;
interface ElementRepository
{
public function create(CreateElementDto $dto): Element;
public function find(int $id): ?Element;
}

View file

@ -0,0 +1,98 @@
<?php
namespace App\Element;
use App\Set\Set;
use App\Set\SetRepository;
use RuntimeException;
class EloquentElementRepository implements ElementRepository
{
public function __construct(
private SetRepository $setRepository,
) {}
public function create(CreateElementDto $dto): Element
{
$position = $this->nextPosition(
$dto->set,
$dto->parentElement,
);
$model = ElementModel::create([
'set_id' => $dto->set->getId(),
'name' => $dto->name,
'kind' => $dto->kind,
'parent_element_id' => $dto->parentElement?->getId(),
'position' => $position,
]);
return new Element(
id: $model->id,
name: $model->name,
kind: $model->kind,
set: $dto->set,
parentElement: $dto->parentElement,
position: $model->position,
);
}
public function find(int $id): ?Element
{
$model = ElementModel::find($id);
return $model === null ? null : $this->toDomain($model);
}
private function nextPosition(
Set $set,
?Element $parentElement,
): int {
$query = ElementModel::query()
->where('set_id', $set->getId());
if ($parentElement === null) {
$query->whereNull('parent_element_id');
} else {
$query->where('parent_element_id', $parentElement->getId());
}
$currentMaximum = $query->max('position');
if ($currentMaximum === null) {
return 1;
}
return (int) $currentMaximum + 1;
}
private function toDomain(ElementModel $model): Element
{
$set = $this->findSet($model->set_id);
$parentElement = null;
if ($model->parent_element_id !== null) {
$parentElement = $this->find($model->parent_element_id);
if ($parentElement === null) {
throw new RuntimeException('element parent not found');
}
}
return new Element(
id: $model->id,
name: $model->name,
kind: $model->kind,
set: $set,
parentElement: $parentElement,
position: $model->position,
);
}
private function findSet(int $id): Set
{
foreach ($this->setRepository->all() as $set) {
if ($set->getId() === $id) {
return $set;
}
}
throw new RuntimeException('element set not found');
}
}

View file

@ -16,6 +16,8 @@ use App\Email\Emailer;
use App\Email\EmailFactory;
use App\Email\LaravelEmailer;
use App\Email\LaravelEmailFactory;
use App\Element\ElementRepository;
use App\Element\EloquentElementRepository;
use App\Set\EloquentSetRepository;
use App\Set\SetRepository;
use App\User\EloquentUserRepository;
@ -51,6 +53,10 @@ class AppServiceProvider extends ServiceProvider
SetRepository::class,
EloquentSetRepository::class,
);
$this->app->bind(
ElementRepository::class,
EloquentElementRepository::class,
);
$this->app->bind(PasswordHasher::class, BcryptPasswordHasher::class);
$this->app->bind(TokenGenerator::class, RandomTokenGenerator::class);
$this->app->bind(Clock::class, SystemClock::class);

View file

@ -0,0 +1,35 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::create('elements', function (Blueprint $table): void {
$table->id();
$table->foreignId('set_id')
->constrained('sets')
->restrictOnDelete();
$table->string('name');
$table->string('kind');
$table->foreignId('parent_element_id')
->nullable()
->constrained('elements')
->restrictOnDelete();
$table->unsignedInteger('position');
$table->index([
'set_id',
'parent_element_id',
'position',
]);
});
}
public function down(): void
{
Schema::dropIfExists('elements');
}
};

View file

@ -0,0 +1,78 @@
<?php
namespace Tests\Fakes;
use App\Element\CreateElementDto;
use App\Element\Element;
use App\Element\ElementRepository;
class FakeElementRepository implements ElementRepository
{
/**
* @var array<int, Element>
*/
private array $elements = [];
public function create(CreateElementDto $dto): Element
{
$id = count($this->elements) + 1;
$element = new Element(
id: $id,
name: $dto->name,
kind: $dto->kind,
set: $dto->set,
parentElement: $dto->parentElement,
position: $this->nextPosition($dto),
);
$this->elements[$id] = $element;
return $this->copy($element);
}
public function find(int $id): ?Element
{
$element = $this->elements[$id] ?? null;
return $element === null ? null : $this->copy($element);
}
private function nextPosition(CreateElementDto $dto): int
{
$requestedParentId = $dto->parentElement?->getId();
$maximumPosition = 0;
foreach ($this->elements as $element) {
if ($element->getSet()->getId() !== $dto->set->getId()) {
continue;
}
$elementParentId = $element->getParentElement()?->getId();
if ($elementParentId !== $requestedParentId) {
continue;
}
$maximumPosition = max(
$maximumPosition,
$element->getPosition(),
);
}
return $maximumPosition + 1;
}
private function copy(Element $element): Element
{
$parentElement = $element->getParentElement();
return new Element(
id: $element->getId(),
name: $element->getName(),
kind: $element->getKind(),
set: $element->getSet(),
parentElement: $parentElement === null
? null
: $this->copy($parentElement),
position: $element->getPosition(),
);
}
}