$dto->name, 'creator_id' => $dto->creator->getId(), ]); return new Set( id: $model->id, name: $model->name, creator: $dto->creator, ); } public function find(int $id): ?Set { $model = SetModel::find($id); return $model === null ? null : $this->toDomain($model); } public function all(): array { $models = SetModel::query() ->orderBy('name') ->orderBy('id') ->get(); $sets = []; foreach ($models as $model) { $sets[] = $this->toDomain($model); } return $sets; } private function toDomain(SetModel $model): Set { $creator = $this->userRepository->find($model->creator_id); if ($creator === null) { throw new RuntimeException('set creator not found'); } return new Set( id: $model->id, name: $model->name, creator: $creator, ); } }