diff --git a/src/Command/IngestBibleCommand.php b/src/Command/IngestBibleCommand.php index 0474864..fe9a3c5 100644 --- a/src/Command/IngestBibleCommand.php +++ b/src/Command/IngestBibleCommand.php @@ -12,13 +12,13 @@ use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Style\SymfonyStyle; #[AsCommand( - name: 'IngestBible', - aliases: ['app:ingest-bible'], + name: 'ImportBible', + aliases: ['app:import-bible'], description: 'Script used to crawl a directory and ingest Bible passages and save them to the database.', )] class IngestBibleCommand extends Command { - private array $files = []; + private ?object $json = null; private SymfonyStyle $io; @@ -54,43 +54,40 @@ class IngestBibleCommand extends Command $this->io->note("Crawling {$this->dir} and ingesting Bible passages..."); - $json = json_decode(file_get_contents("{$this->dir}/esv-bible.json")); - foreach ($json as $book => $data) { + $this->json = json_decode(file_get_contents("{$this->dir}/esv-bible.json")); + foreach ($this->json as $book => $data) { $index = $data->index; $chapters = $data->chapters; $label = $data->label; foreach ($data->text as $chapter => $verses) { - foreach ($verses as $idx => $verse) { - print "$book $chapter:$idx\n"; + foreach ($verses as $idx => $verseText) { + print "$book $chapter:".($idx + 1).PHP_EOL; + $bible = $this->processFile($this->json, $book, $chapter, $idx); + + $this->emi->persist($bible); } + $this->emi->flush(); } } return Command::SUCCESS; } - private function processFile(string $file) + private function processFile(object &$json, string $book, int $chapter, int $verse): Bible { $bible = new Bible(); + $bible->setChapter((int) $chapter); + $bible->setVerse((int) ($verse + 1)); + $bible->setLabel($json->{$book}->label); + $bible->setBookIndex((int) $json->{$book}->index); + $bible->setContent($json->{$book}->text->$chapter[$verse]); if (preg_match("/(\d)([^\d]+)/", $book, $match)) { $book = $match[1]." ".$match[2]; } $bible->setBook($book); - $bible->setBookIndex($ndx); - $bible->setChapter($chapter); - $content = file_get_contents($file); - foreach(explode("\n", $content) as $verse) { - $match = []; - if (preg_match("/^([\d]+). ([^\d]+)/", $verse, $match)) { - $bible->setVerse($match[1]); - $bible->setContent($match[2]); - } - - $this->emi->persist($bible); - $this->emi->flush(); - } + return $bible; } }