diff --git a/src/c03b9c61-17d3-4774-a335-783903719f83/README.md b/src/c03b9c61-17d3-4774-a335-783903719f83/README.md index 96a262f..ae12754 100644 --- a/src/c03b9c61-17d3-4774-a335-783903719f83/README.md +++ b/src/c03b9c61-17d3-4774-a335-783903719f83/README.md @@ -16,6 +16,7 @@ class Load << (F,LightGreen) >> #Green { # Database $load + __construct(Table $table, Model $model, ...) + value(array $keys, string $field, ...) : mixed + + max(array $keys, string $field, ...) : mixed + item(array $keys, string $table) : ?object + items(array $keys, string $table) : ?array - prefix(array $keys) : array @@ -52,6 +53,26 @@ Example: $this->value( string $table end note +note right of Load::max + Get the max value from a given table +Example: $this->value( +[ +'abbreviation' => 'kjv', +'book_nr' => 62, +'chapter' => 3, +'verse' => 16 +], 'value_key', 'table_name' +); + + since: 2.0.1 + return: mixed + + arguments: + array $keys + string $field + string $table +end note + note right of Load::item Get values from a given table Example: $this->item( diff --git a/src/c03b9c61-17d3-4774-a335-783903719f83/code.php b/src/c03b9c61-17d3-4774-a335-783903719f83/code.php index dd0acc0..7bc07b5 100644 --- a/src/c03b9c61-17d3-4774-a335-783903719f83/code.php +++ b/src/c03b9c61-17d3-4774-a335-783903719f83/code.php @@ -101,6 +101,43 @@ final class Load return null; } + /** + * Get the max value from a given table + * Example: $this->value( + * [ + * 'abbreviation' => 'kjv', + * 'book_nr' => 62, + * 'chapter' => 3, + * 'verse' => 16 + * ], 'value_key', 'table_name' + * ); + * + * @param array $keys The item keys + * @param string $field The field key + * @param string $table The table + * + * @return mixed + * @since 2.0.1 + */ + public function max(array $keys, string $field, string $table) + { + // check if this is a valid table + if ($this->table->exist($table, $field)) + { + return $this->model->value( + $this->load->value( + ["all" => "MAX(`$field`)"], + ['a' => $table], + $this->prefix($keys) + ), + $field, + $table + ); + } + + return null; + } + /** * Get values from a given table * Example: $this->item( diff --git a/src/c03b9c61-17d3-4774-a335-783903719f83/code.power b/src/c03b9c61-17d3-4774-a335-783903719f83/code.power index dfb8cea..b448145 100644 --- a/src/c03b9c61-17d3-4774-a335-783903719f83/code.power +++ b/src/c03b9c61-17d3-4774-a335-783903719f83/code.power @@ -75,6 +75,43 @@ return null; } + /** + * Get the max value from a given table + * Example: $this->value( + * [ + * 'abbreviation' => 'kjv', + * 'book_nr' => 62, + * 'chapter' => 3, + * 'verse' => 16 + * ], 'value_key', 'table_name' + * ); + * + * @param array $keys The item keys + * @param string $field The field key + * @param string $table The table + * + * @return mixed + * @since 2.0.1 + */ + public function max(array $keys, string $field, string $table) + { + // check if this is a valid table + if ($this->table->exist($table, $field)) + { + return $this->model->value( + $this->load->value( + ["all" => "MAX(`$field`)"], + ['a' => $table], + $this->prefix($keys) + ), + $field, + $table + ); + } + + return null; + } + /** * Get values from a given table * Example: $this->item( diff --git a/src/f815fb33-f721-48a5-a84e-53f1986e8881/README.md b/src/f815fb33-f721-48a5-a84e-53f1986e8881/README.md index ad27e64..ffe2af1 100644 --- a/src/f815fb33-f721-48a5-a84e-53f1986e8881/README.md +++ b/src/f815fb33-f721-48a5-a84e-53f1986e8881/README.md @@ -25,6 +25,9 @@ class Watcher << (F,LightGreen) >> #Green { + api(string $translation, int $book, ...) : bool + getNextChapter(string $translation, int $book, ...) : ?int + getPreviousChapter(int $chapter, bool $force = false) : ?int + + getLastChapter(string $translation, int $book) : ?int + + getNextBook(string $translation, int $book, ...) : ?int + + getPreviousBook(string $translation, int $book, ...) : ?int - translation(string $translation) : ?string - book(string $translation, int $book) : ?string - chapter(string $translation, int $book, ...) : ?string @@ -83,21 +86,52 @@ note left of Watcher::getPreviousChapter return: ?int end note -note right of Watcher::translation +note right of Watcher::getLastChapter + Get the last chapter of a book + + since: 2.0.1 + return: ?int +end note + +note left of Watcher::getNextBook + Get the next book + + since: 2.0.1 + return: ?int + + arguments: + string $translation + int $book + int $try +end note + +note right of Watcher::getPreviousBook + Get the previous book + + since: 2.0.1 + return: ?int + + arguments: + string $translation + int $book + int $try +end note + +note left of Watcher::translation Get Translation Hash Value since: 2.0.1 return: ?string end note -note left of Watcher::book +note right of Watcher::book Get Book Hash Value since: 2.0.1 return: ?string end note -note right of Watcher::chapter +note left of Watcher::chapter Get Chapter Hash Value since: 2.0.1 @@ -109,7 +143,7 @@ note right of Watcher::chapter int $chapter end note -note left of Watcher::verses +note right of Watcher::verses Load verses if not in local database since: 2.0.1 @@ -121,7 +155,7 @@ note left of Watcher::verses int $chapter end note -note right of Watcher::update +note left of Watcher::update Trigger the update of the verses of a translation-book-chapter since: 2.0.1 @@ -133,14 +167,14 @@ note right of Watcher::update int $chapter end note -note left of Watcher::getVerse +note right of Watcher::getVerse Get a verse text from the API array of verses since: 2.0.1 return: ?object end note -note right of Watcher::updateHash +note left of Watcher::updateHash Trigger the update of a chapter hash value since: 2.0.1 @@ -153,14 +187,14 @@ note right of Watcher::updateHash string $hash end note -note left of Watcher::hold +note right of Watcher::hold Check if its time to match the API hash since: 2.0.1 return: bool end note -note right of Watcher::bump +note left of Watcher::bump Bump the checking time 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 8671033..ee66c27 100644 --- a/src/f815fb33-f721-48a5-a84e-53f1986e8881/code.php +++ b/src/f815fb33-f721-48a5-a84e-53f1986e8881/code.php @@ -248,6 +248,106 @@ final class Watcher return null; } + /** + * Get the last chapter of a book + * + * @param string $translation The translation. + * @param int $book The book number. + * + * @return int|null Number if there is a previous, else null + * @since 2.0.1 + */ + public function getLastChapter(string $translation, int $book): ?int + { + // we load the last chapter + return $this->load->max( + ['abbreviation' => $translation, 'book_nr' => $book], + 'chapter', 'chapter' + ); + } + + /** + * Get the next book + * + * @param string $translation The translation. + * @param int $book The book number. + * @param int $try The number of tries + * + * @return int|null Number if there is a next, else null + * @since 2.0.1 + */ + public function getNextBook(string $translation, int $book, int $try = 0): ?int + { + // we load the next chapter + $next = $book + 1; + + // if we already looked over 66 + if ($next >= 66) + { + $next = 1; + } + + // check if this book exist + if ($this->load->value( + ['abbreviation' => $translation, 'nr' => $next], + 'sha', 'book' + )) + { + return $next; + } + + $try++; + + // could not be found :( + if ($try >= 65) + { + return null; + } + + return $this->getNextBook($translation, $next, $try); + } + + /** + * Get the previous book + * + * @param string $translation The translation. + * @param int $book The book number. + * @param int $try The number of tries + * + * @return int|null Number if there is a previous, else null + * @since 2.0.1 + */ + public function getPreviousBook(string $translation, int $book, int $try = 0): ?int + { + // we load the previous book + $previous = $book - 1; + + // if we already looked over 66 + if ($previous <= 0) + { + $previous = 66; + } + + // check if this book exist + if ($this->load->value( + ['abbreviation' => $translation, 'nr' => $previous], + 'sha', 'book' + )) + { + return $previous; + } + + $try++; + + // could not be found :( + if ($try >= 65) + { + return null; + } + + return $this->getPreviousBook($translation, $previous, $try); + } + /** * Get Translation Hash Value * diff --git a/src/f815fb33-f721-48a5-a84e-53f1986e8881/code.power b/src/f815fb33-f721-48a5-a84e-53f1986e8881/code.power index 6ba61bb..52c1539 100644 --- a/src/f815fb33-f721-48a5-a84e-53f1986e8881/code.power +++ b/src/f815fb33-f721-48a5-a84e-53f1986e8881/code.power @@ -217,6 +217,106 @@ return null; } + /** + * Get the last chapter of a book + * + * @param string $translation The translation. + * @param int $book The book number. + * + * @return int|null Number if there is a previous, else null + * @since 2.0.1 + */ + public function getLastChapter(string $translation, int $book): ?int + { + // we load the last chapter + return $this->load->max( + ['abbreviation' => $translation, 'book_nr' => $book], + 'chapter', 'chapter' + ); + } + + /** + * Get the next book + * + * @param string $translation The translation. + * @param int $book The book number. + * @param int $try The number of tries + * + * @return int|null Number if there is a next, else null + * @since 2.0.1 + */ + public function getNextBook(string $translation, int $book, int $try = 0): ?int + { + // we load the next chapter + $next = $book + 1; + + // if we already looked over 66 + if ($next >= 66) + { + $next = 1; + } + + // check if this book exist + if ($this->load->value( + ['abbreviation' => $translation, 'nr' => $next], + 'sha', 'book' + )) + { + return $next; + } + + $try++; + + // could not be found :( + if ($try >= 65) + { + return null; + } + + return $this->getNextBook($translation, $next, $try); + } + + /** + * Get the previous book + * + * @param string $translation The translation. + * @param int $book The book number. + * @param int $try The number of tries + * + * @return int|null Number if there is a previous, else null + * @since 2.0.1 + */ + public function getPreviousBook(string $translation, int $book, int $try = 0): ?int + { + // we load the previous book + $previous = $book - 1; + + // if we already looked over 66 + if ($previous <= 0) + { + $previous = 66; + } + + // check if this book exist + if ($this->load->value( + ['abbreviation' => $translation, 'nr' => $previous], + 'sha', 'book' + )) + { + return $previous; + } + + $try++; + + // could not be found :( + if ($try >= 65) + { + return null; + } + + return $this->getPreviousBook($translation, $previous, $try); + } + /** * Get Translation Hash Value *