From 2eb5a432d2229788ce2fdb09f36c6f4bebdea813 Mon Sep 17 00:00:00 2001 From: Laria Carolin Chabowski Date: Fri, 7 Feb 2020 09:44:59 +0100 Subject: Initial commit --- src/Tools/PopulateDevDb.php | 71 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 src/Tools/PopulateDevDb.php (limited to 'src/Tools') diff --git a/src/Tools/PopulateDevDb.php b/src/Tools/PopulateDevDb.php new file mode 100644 index 0000000..8f1b2b9 --- /dev/null +++ b/src/Tools/PopulateDevDb.php @@ -0,0 +1,71 @@ +words = $words; + } + + public function run(Env $env) + { + $this->readWords(); + + $db = $env->db(); + for ($i = 0; $i < self::NUM_NOTES; $i++) + $this->createTestNote($db); + } + + private function randomWords(int $min, int $max): array + { + $words = []; + $num = mt_rand($min, $max); + for ($i = 0; $i < $num; $i++) + $words[] = $this->words[mt_rand(0, count($this->words)-1)]; + + return $words; + } + + private static function byChance(float $chance): bool + { + return mt_rand() / mt_getrandmax() <= $chance; + } + + private function createTestNote(SQLite3 $db): void + { + $note = new Note(); + $tags = $this->randomWords(self::TAGS_MIN_RAND, self::TAGS_MAX_RAND); + if (self::byChance(self::CHANCE_INBOX)) + $tags[] = "inbox"; + $note->setTags($tags); + $note->setContent(implode(" ", $this->randomWords(self::CONTENT_MIN_WORDS, self::CONTENT_MAX_WORDS))); + $note->setTrash(self::byChance(self::CHANCE_TRASH)); + + $note->save($db); + } +} \ No newline at end of file -- cgit v1.2.3-54-g00ecf