[ [ 'name' => 'Genesis', 'kind' => 'book', 'children' => [ [ 'name' => 'Creation', 'kind' => 'portion', 'children' => [ [ 'name' => 'Chapter 1', 'kind' => 'chapter', 'children' => [], ], [ 'name' => 'Chapter 2', 'kind' => 'chapter', 'children' => [], ], ], ], [ 'name' => 'Noah', 'kind' => 'portion', 'children' => [], ], ], ], [ 'name' => 'Exodus', 'kind' => 'book', 'children' => [ [ 'name' => 'Shemot', 'kind' => 'portion', 'children' => [], ], ], ], ], 'Course' => [ [ 'name' => 'Foundations', 'kind' => 'module', 'children' => [ [ 'name' => 'Welcome', 'kind' => 'lesson', 'children' => [], ], [ 'name' => 'Core Concepts', 'kind' => 'lesson', 'children' => [], ], ], ], [ 'name' => 'Applied Practice', 'kind' => 'module', 'children' => [ [ 'name' => 'Guided Exercise', 'kind' => 'lesson', 'children' => [], ], [ 'name' => 'Final Review', 'kind' => 'lesson', 'children' => [], ], ], ], ], 'Fitness Program' => [ [ 'name' => 'Foundation Phase', 'kind' => 'phase', 'children' => [ [ 'name' => 'Strength Day', 'kind' => 'workout', 'children' => [ [ 'name' => 'Squat', 'kind' => 'exercise', 'children' => [], ], [ 'name' => 'Push-up', 'kind' => 'exercise', 'children' => [], ], ], ], [ 'name' => 'Mobility Day', 'kind' => 'workout', 'children' => [ [ 'name' => 'Hip Flow', 'kind' => 'exercise', 'children' => [], ], ], ], ], ], [ 'name' => 'Build Phase', 'kind' => 'phase', 'children' => [ [ 'name' => 'Full Body Circuit', 'kind' => 'workout', 'children' => [], ], ], ], ], ]; public function run(): void { $elementRepository = app(ElementRepository::class); foreach (app(SetRepository::class)->all() as $set) { $definitions = self::ELEMENTS_BY_SET[$set->getName()] ?? null; if ($definitions === null) { continue; } $this->seedChildren( repository: $elementRepository, set: $set, parentElement: null, definitions: $definitions, ); } } /** * @param list * }> $definitions */ private function seedChildren( ElementRepository $repository, Set $set, ?Element $parentElement, array $definitions, ): void { $siblings = $parentElement === null ? $repository->findTopLevelBySet($set) : $repository->findByParentElement($parentElement); foreach ($definitions as $definition) { $element = $this->findSibling( siblings: $siblings, name: $definition['name'], kind: $definition['kind'], ); if ($element === null) { $element = $repository->create(new CreateElementDto( set: $set, name: $definition['name'], kind: $definition['kind'], parentElement: $parentElement, )); $siblings[] = $element; } $this->seedChildren( repository: $repository, set: $set, parentElement: $element, definitions: $definition['children'], ); } } /** * @param list $siblings */ private function findSibling( array $siblings, string $name, string $kind, ): ?Element { foreach ($siblings as $sibling) { if ( $sibling->getName() === $name && $sibling->getKind() === $kind ) { return $sibling; } } return null; } }