From 552101bc1ad61f3faa1885ee5a22ab36b5dcbb58 Mon Sep 17 00:00:00 2001 From: aB0t Date: Wed, 21 Jun 2023 06:10:03 +0200 Subject: [PATCH] update 2023-06-21 06:10:03 --- .../code.php | 3 +- .../code.power | 3 +- .../settings.json | 6 +- .../README.md | 43 +++++++- .../code.php | 99 ++++++++++++++++++- .../code.power | 98 +++++++++++++++++- .../settings.json | 4 + 7 files changed, 237 insertions(+), 19 deletions(-) diff --git a/src/56465044-94ed-4e00-b6db-160c67163df8/code.php b/src/56465044-94ed-4e00-b6db-160c67163df8/code.php index f361fa5..944b3ae 100644 --- a/src/56465044-94ed-4e00-b6db-160c67163df8/code.php +++ b/src/56465044-94ed-4e00-b6db-160c67163df8/code.php @@ -92,7 +92,8 @@ class App implements ServiceProviderInterface { return new DailyScripture( $container->get('GetBible.Config'), - $container->get('GetBible.Utilities.Http') + $container->get('GetBible.Utilities.Http'), + $container->get('GetBible.Load') ); } diff --git a/src/56465044-94ed-4e00-b6db-160c67163df8/code.power b/src/56465044-94ed-4e00-b6db-160c67163df8/code.power index d79f014..bf10e79 100644 --- a/src/56465044-94ed-4e00-b6db-160c67163df8/code.power +++ b/src/56465044-94ed-4e00-b6db-160c67163df8/code.power @@ -62,7 +62,8 @@ { return new DailyScripture( $container->get('GetBible.Config'), - $container->get('GetBible.Utilities.Http') + $container->get('GetBible.Utilities.Http'), + $container->get('GetBible.Load') ); } diff --git a/src/56465044-94ed-4e00-b6db-160c67163df8/settings.json b/src/56465044-94ed-4e00-b6db-160c67163df8/settings.json index 60144f0..9f528c0 100644 --- a/src/56465044-94ed-4e00-b6db-160c67163df8/settings.json +++ b/src/56465044-94ed-4e00-b6db-160c67163df8/settings.json @@ -20,15 +20,15 @@ "use": "ff8d5fdb-2d1f-4178-bd18-a43b8efd1068", "as": "default" }, - "use_selection4": { + "use_selection2": { "use": "90f2ee7a-c041-4316-ad54-af4f97fa3003", "as": "default" }, - "use_selection2": { + "use_selection3": { "use": "8336e3c4-f11b-41bc-a2b1-976f99442a84", "as": "default" }, - "use_selection3": { + "use_selection4": { "use": "f815fb33-f721-48a5-a84e-53f1986e8881", "as": "default" } diff --git a/src/90f2ee7a-c041-4316-ad54-af4f97fa3003/README.md b/src/90f2ee7a-c041-4316-ad54-af4f97fa3003/README.md index 82742d9..19bbe39 100644 --- a/src/90f2ee7a-c041-4316-ad54-af4f97fa3003/README.md +++ b/src/90f2ee7a-c041-4316-ad54-af4f97fa3003/README.md @@ -14,17 +14,33 @@ class DailyScripture << (F,LightGreen) >> #Green { # ?int $book # ?int $chapter # ?string $verses - + __construct(Config $config, Http $http) + # ?string $reference + # Load $load + + __construct(Config $config, Http $http, ...) + + load(string $reference) + book() : ?int + chapter() : ?int + verses() : ?string - # parse(string $reference) + - parse(string $reference) + - extract(string $reference) : ?string + - replace(string $reference, string $name, ...) : string } note right of DailyScripture::__construct Constructor since: 2.0.1 + + arguments: + Config $config + Http $http + Load $load +end note + +note left of DailyScripture::load + An option to load another reference + + since: 2.0.1 end note note right of DailyScripture::book @@ -34,7 +50,7 @@ note right of DailyScripture::book return: ?int end note -note right of DailyScripture::chapter +note left of DailyScripture::chapter Get the chapter from the reference since: 2.0.1 @@ -48,11 +64,30 @@ note right of DailyScripture::verses return: ?string end note -note right of DailyScripture::parse +note left of DailyScripture::parse Parse the scriptural reference since: 2.0.1 end note + +note right of DailyScripture::extract + Extract the book name from the reference + + since: 2.0.1 + return: ?string +end note + +note left of DailyScripture::replace + Replace the book name with a number in the reference + + since: 2.0.1 + return: string + + arguments: + string $reference + string $name + int $number +end note @enduml ``` diff --git a/src/90f2ee7a-c041-4316-ad54-af4f97fa3003/code.php b/src/90f2ee7a-c041-4316-ad54-af4f97fa3003/code.php index 63a93ce..a4714e8 100644 --- a/src/90f2ee7a-c041-4316-ad54-af4f97fa3003/code.php +++ b/src/90f2ee7a-c041-4316-ad54-af4f97fa3003/code.php @@ -14,6 +14,7 @@ namespace VDM\Joomla\GetBible; use VDM\Joomla\GetBible\Config; use VDM\Joomla\GetBible\Utilities\Http; +use VDM\Joomla\GetBible\Database\Load; /** @@ -47,22 +48,73 @@ final class DailyScripture */ protected ?string $verses = null; + /** + * The reference string + * + * @var string|null + * @since 2.0.1 + */ + protected ?string $reference = null; + + /** + * The load object + * + * @var Load + * @since 2.0.1 + */ + protected Load $load; + /** * Constructor * * @param Config $config The config object. * @param Http $http The http object. + * @param Load $load The load object. * * @since 2.0.1 */ - public function __construct(Config $config, Http $http) + public function __construct(Config $config, Http $http, Load $load) { $response = $http->get($config->daily_scripture_url); // make sure we got the correct response if ($response->code == 200 && isset($response->body) && is_string($response->body)) { - $this->parse($response->body); + $this->reference = $response->body; + + $this->parse($this->reference); + } + + $this->load = $load; + } + + /** + * An option to load another reference + * + * @param string $reference The scriptural reference. + * + * @since 2.0.1 + */ + public function load(string $reference) + { + // convert book name to book number + if (($name = $this->extract($reference)) !== null) + { + if (($number = $this->load->value( + ['name' => $name], 'nr', 'book')) === null) + { + // the book number could not be found + return; + } + + $reference = $this->replace($reference, $name, $number); + } + + $this->parse($reference); + + if ($this->book === null) + { + $this->parse($this->reference); } } @@ -106,15 +158,52 @@ final class DailyScripture * * @since 2.0.1 */ - protected function parse(string $reference) + private function parse(string $reference) { $parts = explode(' ', $reference); - $this->book = isset($parts[0]) ? intval($parts[0]) : null; + $this->book = (isset($parts[0]) && is_numeric($parts[0])) ? intval($parts[0]) : null; $chapterVerses = isset($parts[1]) ? explode(':', $parts[1]) : [null, null]; - $this->chapter = isset($chapterVerses[0]) ? intval($chapterVerses[0]) : null; + $this->chapter = (isset($chapterVerses[0]) && is_numeric($chapterVerses[0])) ? intval($chapterVerses[0]) : null; $this->verses = isset($chapterVerses[1]) ? trim($chapterVerses[1]) : null; + } + + /** + * Extract the book name from the reference + * + * @return string|null Book name + * @since 2.0.1 + */ + private function extract(string $reference): ?string + { + // Use regex to match everything before the chapter:verse part + preg_match('/^(.*?)\s\d+:\d+/', $reference, $matches); + $bookName = trim($matches[1]) ?? ''; + + // Check if the book name is a number + if (is_numeric($bookName) || $bookName === '') + { + return null; + } + + return $bookName; + } + + /** + * Replace the book name with a number in the reference + * + * @param string $reference Original reference + * @param string $name Book name + * @param int $number Book number + * + * @return string New reference with the book number instead of the name + * @since 2.0.1 + */ + private function replace(string $reference, string $name, int $number): string + { + // Use regex to replace the book name with the book number + return preg_replace('/^' . preg_quote($name, '/') . '\b/', $number, $reference); } } diff --git a/src/90f2ee7a-c041-4316-ad54-af4f97fa3003/code.power b/src/90f2ee7a-c041-4316-ad54-af4f97fa3003/code.power index 450c2a5..5d669af 100644 --- a/src/90f2ee7a-c041-4316-ad54-af4f97fa3003/code.power +++ b/src/90f2ee7a-c041-4316-ad54-af4f97fa3003/code.power @@ -22,22 +22,73 @@ */ protected ?string $verses = null; + /** + * The reference string + * + * @var string|null + * @since 2.0.1 + */ + protected ?string $reference = null; + + /** + * The load object + * + * @var Load + * @since 2.0.1 + */ + protected Load $load; + /** * Constructor * * @param Config $config The config object. * @param Http $http The http object. + * @param Load $load The load object. * * @since 2.0.1 */ - public function __construct(Config $config, Http $http) + public function __construct(Config $config, Http $http, Load $load) { $response = $http->get($config->daily_scripture_url); // make sure we got the correct response if ($response->code == 200 && isset($response->body) && is_string($response->body)) { - $this->parse($response->body); + $this->reference = $response->body; + + $this->parse($this->reference); + } + + $this->load = $load; + } + + /** + * An option to load another reference + * + * @param string $reference The scriptural reference. + * + * @since 2.0.1 + */ + public function load(string $reference) + { + // convert book name to book number + if (($name = $this->extract($reference)) !== null) + { + if (($number = $this->load->value( + ['name' => $name], 'nr', 'book')) === null) + { + // the book number could not be found + return; + } + + $reference = $this->replace($reference, $name, $number); + } + + $this->parse($reference); + + if ($this->book === null) + { + $this->parse($this->reference); } } @@ -81,13 +132,50 @@ * * @since 2.0.1 */ - protected function parse(string $reference) + private function parse(string $reference) { $parts = explode(' ', $reference); - $this->book = isset($parts[0]) ? intval($parts[0]) : null; + $this->book = (isset($parts[0]) && is_numeric($parts[0])) ? intval($parts[0]) : null; $chapterVerses = isset($parts[1]) ? explode(':', $parts[1]) : [null, null]; - $this->chapter = isset($chapterVerses[0]) ? intval($chapterVerses[0]) : null; + $this->chapter = (isset($chapterVerses[0]) && is_numeric($chapterVerses[0])) ? intval($chapterVerses[0]) : null; $this->verses = isset($chapterVerses[1]) ? trim($chapterVerses[1]) : null; + } + + /** + * Extract the book name from the reference + * + * @return string|null Book name + * @since 2.0.1 + */ + private function extract(string $reference): ?string + { + // Use regex to match everything before the chapter:verse part + preg_match('/^(.*?)\s\d+:\d+/', $reference, $matches); + $bookName = trim($matches[1]) ?? ''; + + // Check if the book name is a number + if (is_numeric($bookName) || $bookName === '') + { + return null; + } + + return $bookName; + } + + /** + * Replace the book name with a number in the reference + * + * @param string $reference Original reference + * @param string $name Book name + * @param int $number Book number + * + * @return string New reference with the book number instead of the name + * @since 2.0.1 + */ + private function replace(string $reference, string $name, int $number): string + { + // Use regex to replace the book name with the book number + return preg_replace('/^' . preg_quote($name, '/') . '\b/', $number, $reference); } \ No newline at end of file diff --git a/src/90f2ee7a-c041-4316-ad54-af4f97fa3003/settings.json b/src/90f2ee7a-c041-4316-ad54-af4f97fa3003/settings.json index e3134b2..f27881c 100644 --- a/src/90f2ee7a-c041-4316-ad54-af4f97fa3003/settings.json +++ b/src/90f2ee7a-c041-4316-ad54-af4f97fa3003/settings.json @@ -17,6 +17,10 @@ "use_selection1": { "use": "b8c66089-735e-4081-825c-8fe36b28e4a6", "as": "default" + }, + "use_selection2": { + "use": "c03b9c61-17d3-4774-a335-783903719f83", + "as": "default" } }, "namespace": "VDM\\Joomla\\GetBible\\DailyScripture",