4
0

update 2023-06-21 06:10:03

This commit is contained in:
Robot 2023-06-21 06:10:03 +02:00
parent 48a894ff7a
commit 552101bc1a
Signed by: Robot
GPG Key ID: 14DECD44E7E1BB95
7 changed files with 237 additions and 19 deletions

View File

@ -92,7 +92,8 @@ class App implements ServiceProviderInterface
{ {
return new DailyScripture( return new DailyScripture(
$container->get('GetBible.Config'), $container->get('GetBible.Config'),
$container->get('GetBible.Utilities.Http') $container->get('GetBible.Utilities.Http'),
$container->get('GetBible.Load')
); );
} }

View File

@ -62,7 +62,8 @@
{ {
return new DailyScripture( return new DailyScripture(
$container->get('GetBible.Config'), $container->get('GetBible.Config'),
$container->get('GetBible.Utilities.Http') $container->get('GetBible.Utilities.Http'),
$container->get('GetBible.Load')
); );
} }

View File

@ -20,15 +20,15 @@
"use": "ff8d5fdb-2d1f-4178-bd18-a43b8efd1068", "use": "ff8d5fdb-2d1f-4178-bd18-a43b8efd1068",
"as": "default" "as": "default"
}, },
"use_selection4": { "use_selection2": {
"use": "90f2ee7a-c041-4316-ad54-af4f97fa3003", "use": "90f2ee7a-c041-4316-ad54-af4f97fa3003",
"as": "default" "as": "default"
}, },
"use_selection2": { "use_selection3": {
"use": "8336e3c4-f11b-41bc-a2b1-976f99442a84", "use": "8336e3c4-f11b-41bc-a2b1-976f99442a84",
"as": "default" "as": "default"
}, },
"use_selection3": { "use_selection4": {
"use": "f815fb33-f721-48a5-a84e-53f1986e8881", "use": "f815fb33-f721-48a5-a84e-53f1986e8881",
"as": "default" "as": "default"
} }

View File

@ -14,17 +14,33 @@ class DailyScripture << (F,LightGreen) >> #Green {
# ?int $book # ?int $book
# ?int $chapter # ?int $chapter
# ?string $verses # ?string $verses
+ __construct(Config $config, Http $http) # ?string $reference
# Load $load
+ __construct(Config $config, Http $http, ...)
+ load(string $reference)
+ book() : ?int + book() : ?int
+ chapter() : ?int + chapter() : ?int
+ verses() : ?string + verses() : ?string
# parse(string $reference) - parse(string $reference)
- extract(string $reference) : ?string
- replace(string $reference, string $name, ...) : string
} }
note right of DailyScripture::__construct note right of DailyScripture::__construct
Constructor Constructor
since: 2.0.1 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 end note
note right of DailyScripture::book note right of DailyScripture::book
@ -34,7 +50,7 @@ note right of DailyScripture::book
return: ?int return: ?int
end note end note
note right of DailyScripture::chapter note left of DailyScripture::chapter
Get the chapter from the reference Get the chapter from the reference
since: 2.0.1 since: 2.0.1
@ -48,12 +64,31 @@ note right of DailyScripture::verses
return: ?string return: ?string
end note end note
note right of DailyScripture::parse note left of DailyScripture::parse
Parse the scriptural reference Parse the scriptural reference
since: 2.0.1 since: 2.0.1
end note 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 @enduml
``` ```

View File

@ -14,6 +14,7 @@ namespace VDM\Joomla\GetBible;
use VDM\Joomla\GetBible\Config; use VDM\Joomla\GetBible\Config;
use VDM\Joomla\GetBible\Utilities\Http; use VDM\Joomla\GetBible\Utilities\Http;
use VDM\Joomla\GetBible\Database\Load;
/** /**
@ -47,22 +48,73 @@ final class DailyScripture
*/ */
protected ?string $verses = null; 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 * Constructor
* *
* @param Config $config The config object. * @param Config $config The config object.
* @param Http $http The http object. * @param Http $http The http object.
* @param Load $load The load object.
* *
* @since 2.0.1 * @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); $response = $http->get($config->daily_scripture_url);
// make sure we got the correct response // make sure we got the correct response
if ($response->code == 200 && isset($response->body) && is_string($response->body)) 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 * @since 2.0.1
*/ */
protected function parse(string $reference) private function parse(string $reference)
{ {
$parts = explode(' ', $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]; $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; $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);
}
} }

View File

@ -22,22 +22,73 @@
*/ */
protected ?string $verses = null; 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 * Constructor
* *
* @param Config $config The config object. * @param Config $config The config object.
* @param Http $http The http object. * @param Http $http The http object.
* @param Load $load The load object.
* *
* @since 2.0.1 * @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); $response = $http->get($config->daily_scripture_url);
// make sure we got the correct response // make sure we got the correct response
if ($response->code == 200 && isset($response->body) && is_string($response->body)) 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 * @since 2.0.1
*/ */
protected function parse(string $reference) private function parse(string $reference)
{ {
$parts = explode(' ', $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]; $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; $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);
}

View File

@ -17,6 +17,10 @@
"use_selection1": { "use_selection1": {
"use": "b8c66089-735e-4081-825c-8fe36b28e4a6", "use": "b8c66089-735e-4081-825c-8fe36b28e4a6",
"as": "default" "as": "default"
},
"use_selection2": {
"use": "c03b9c61-17d3-4774-a335-783903719f83",
"as": "default"
} }
}, },
"namespace": "VDM\\Joomla\\GetBible\\DailyScripture", "namespace": "VDM\\Joomla\\GetBible\\DailyScripture",