From 82bd393f956880cea1c2698c710deed8ba8780b8 Mon Sep 17 00:00:00 2001 From: aB0t Date: Thu, 22 Jun 2023 09:54:56 +0200 Subject: [PATCH] update 2023-06-22 09:54:56 --- .../README.md | 9 ++--- .../code.php | 33 +++++++++++++------ .../code.power | 33 +++++++++++++------ 3 files changed, 51 insertions(+), 24 deletions(-) diff --git a/src/f815fb33-f721-48a5-a84e-53f1986e8881/README.md b/src/f815fb33-f721-48a5-a84e-53f1986e8881/README.md index 3145f75..ad27e64 100644 --- a/src/f815fb33-f721-48a5-a84e-53f1986e8881/README.md +++ b/src/f815fb33-f721-48a5-a84e-53f1986e8881/README.md @@ -23,8 +23,8 @@ class Watcher << (F,LightGreen) >> #Green { # ?object $verse + __construct(Load $load, Insert $insert, ...) + api(string $translation, int $book, ...) : bool - + next(string $translation, int $book, ...) : ?int - + previous(int $chapter) : ?int + + getNextChapter(string $translation, int $book, ...) : ?int + + getPreviousChapter(int $chapter, bool $force = false) : ?int - translation(string $translation) : ?string - book(string $translation, int $book) : ?string - chapter(string $translation, int $book, ...) : ?string @@ -63,7 +63,7 @@ note left of Watcher::api int $chapter end note -note right of Watcher::next +note right of Watcher::getNextChapter Get the next chapter since: 2.0.1 @@ -73,9 +73,10 @@ note right of Watcher::next string $translation int $book int $chapter + bool $force = false end note -note left of Watcher::previous +note left of Watcher::getPreviousChapter Get the previous chapter since: 2.0.1 diff --git a/src/f815fb33-f721-48a5-a84e-53f1986e8881/code.php b/src/f815fb33-f721-48a5-a84e-53f1986e8881/code.php index 91d57de..3857d56 100644 --- a/src/f815fb33-f721-48a5-a84e-53f1986e8881/code.php +++ b/src/f815fb33-f721-48a5-a84e-53f1986e8881/code.php @@ -206,16 +206,17 @@ final class Watcher * @param string $translation The translation. * @param int $book The book number. * @param int $chapter The chapter number. + * @param bool $force The switch to force the getting. * * @return int|null Number if there is a next, else null * @since 2.0.1 */ - public function next(string $translation, int $book, int $chapter): ?int + public function getNextChapter(string $translation, int $book, int $chapter, bool $force = false): ?int { // we load the next chapter $next = $chapter + 1; // check if this books has this next chapter - if (!$this->fresh && ($chapter_sha = $this->load->value( + if (($force || !$this->fresh) && ($chapter_sha = $this->load->value( ['abbreviation' => $translation, 'book_nr' => $book, 'chapter' => $next], 'sha', 'chapter' )) !== null) @@ -230,15 +231,16 @@ final class Watcher * Get the previous chapter * * @param int $chapter The chapter number. + * @param bool $force The switch to force the getting. * * @return int|null Number if there is a previous, else null * @since 2.0.1 */ - public function previous(int $chapter): ?int + public function getPreviousChapter(int $chapter, bool $force = false): ?int { // we load the previous chapter $previous = $chapter - 1; - if (!$this->fresh && $previous > 0) + if (($force || !$this->fresh) && $previous > 0) { return $previous; } @@ -408,18 +410,29 @@ final class Watcher return false; } - $api_verses = (array) $api->verses; $update = []; + $insert = []; // dynamic update all verse objects - foreach ($verses as $verse) + foreach ($api->verses as $verse) { - if (($object = $this->getVerse((int) $verse->verse, $api_verses)) !== null) + // check if the verse exist + if (($object = $this->getVerse((int) $verse->verse, $verses)) !== null) { - $object->id = $verse->id; - $object->created = $this->today; - $update[] = $object; + $verse->id = $object->id; + $verse->created = $this->today; + $update[] = $verse; } + else + { + $insert[] = $verse; + } + } + + // check if we have values to insert + if ($insert !== []) + { + $this->insert->items($insert, 'verse') } // update the local verses diff --git a/src/f815fb33-f721-48a5-a84e-53f1986e8881/code.power b/src/f815fb33-f721-48a5-a84e-53f1986e8881/code.power index abc307b..5ec8f24 100644 --- a/src/f815fb33-f721-48a5-a84e-53f1986e8881/code.power +++ b/src/f815fb33-f721-48a5-a84e-53f1986e8881/code.power @@ -175,16 +175,17 @@ * @param string $translation The translation. * @param int $book The book number. * @param int $chapter The chapter number. + * @param bool $force The switch to force the getting. * * @return int|null Number if there is a next, else null * @since 2.0.1 */ - public function next(string $translation, int $book, int $chapter): ?int + public function getNextChapter(string $translation, int $book, int $chapter, bool $force = false): ?int { // we load the next chapter $next = $chapter + 1; // check if this books has this next chapter - if (!$this->fresh && ($chapter_sha = $this->load->value( + if (($force || !$this->fresh) && ($chapter_sha = $this->load->value( ['abbreviation' => $translation, 'book_nr' => $book, 'chapter' => $next], 'sha', 'chapter' )) !== null) @@ -199,15 +200,16 @@ * Get the previous chapter * * @param int $chapter The chapter number. + * @param bool $force The switch to force the getting. * * @return int|null Number if there is a previous, else null * @since 2.0.1 */ - public function previous(int $chapter): ?int + public function getPreviousChapter(int $chapter, bool $force = false): ?int { // we load the previous chapter $previous = $chapter - 1; - if (!$this->fresh && $previous > 0) + if (($force || !$this->fresh) && $previous > 0) { return $previous; } @@ -377,18 +379,29 @@ return false; } - $api_verses = (array) $api->verses; $update = []; + $insert = []; // dynamic update all verse objects - foreach ($verses as $verse) + foreach ($api->verses as $verse) { - if (($object = $this->getVerse((int) $verse->verse, $api_verses)) !== null) + // check if the verse exist + if (($object = $this->getVerse((int) $verse->verse, $verses)) !== null) { - $object->id = $verse->id; - $object->created = $this->today; - $update[] = $object; + $verse->id = $object->id; + $verse->created = $this->today; + $update[] = $verse; } + else + { + $insert[] = $verse; + } + } + + // check if we have values to insert + if ($insert !== []) + { + $this->insert->items($insert, 'verse') } // update the local verses