4
0

update 2023-07-13 12:31:27

This commit is contained in:
Robot 2023-07-13 12:31:27 +02:00
parent b7cba4643e
commit 1f6cb785ee
Signed by: Robot
GPG Key ID: 14DECD44E7E1BB95
7 changed files with 247 additions and 42 deletions

View File

@ -11,11 +11,13 @@
```uml ```uml
@startuml @startuml
class Response << (F,LightGreen) >> #Green { class Response << (F,LightGreen) >> #Green {
# Translation $translation
# Chapter $chapter
# Prompt $prompt # Prompt $prompt
# Load $load # Load $load
# Config $config # Config $config
# array $responses # array $responses
+ __construct(Prompt $prompt, Load $load, ...) + __construct(Translation $translation, Chapter $chapter, ...)
+ get(?string $guid = null) : ?object + get(?string $guid = null) : ?object
} }
@ -25,6 +27,8 @@ note right of Response::__construct
since: 2.0.1 since: 2.0.1
arguments: arguments:
Translation $translation
Chapter $chapter
Prompt $prompt Prompt $prompt
Load $load Load $load
Config $config Config $config

View File

@ -12,6 +12,9 @@
namespace VDM\Joomla\GetBible\Data; namespace VDM\Joomla\GetBible\Data;
use VDM\Joomla\GetBible\Data\Translation;
use VDM\Joomla\GetBible\Data\Chapter;
use VDM\Joomla\GetBible\Data\Prompt;
use VDM\Joomla\GetBible\Database\Load; use VDM\Joomla\GetBible\Database\Load;
use VDM\Joomla\GetBible\Openai\Config; use VDM\Joomla\GetBible\Openai\Config;
@ -23,6 +26,22 @@ use VDM\Joomla\GetBible\Openai\Config;
*/ */
final class Response final class Response
{ {
/**
* The Translation class
*
* @var Translation
* @since 2.0.1
*/
protected Translation $translation;
/**
* The Chapter class
*
* @var Chapter
* @since 2.0.1
*/
protected Chapter $chapter;
/** /**
* The Prompt class * The Prompt class
* *
@ -58,14 +77,18 @@ final class Response
/** /**
* Constructor * Constructor
* *
* @param Prompt $prompt The prompt object. * @param Translation $translation The translation object.
* @param Load $load The load object. * @param Chapter $chapter The chapter object.
* @param Config $config The config object. * @param Prompt $prompt The prompt object.
* @param Load $load The load object.
* @param Config $config The config object.
* *
* @since 2.0.1 * @since 2.0.1
*/ */
public function __construct(Prompt $prompt, Load $load, Config $config) public function __construct(Translation $translation, Chapter $chapter, Prompt $prompt, Load $load, Config $config)
{ {
$this->translation = $translation;
$this->chapter = $chapter;
$this->prompt = $prompt; $this->prompt = $prompt;
$this->load = $load; $this->load = $load;
$this->config = $config; $this->config = $config;
@ -84,7 +107,7 @@ final class Response
// get the cache behaviour // get the cache behaviour
$cache = $this->prompt->getCacheBehaviour(); $cache = $this->prompt->getCacheBehaviour();
if (empty($cache)) if (empty($cache) || ($cache != 1 && $cache != 2))
{ {
return null; return null;
} }
@ -103,6 +126,29 @@ final class Response
$query = ['prompt' => $guid]; $query = ['prompt' => $guid];
// Basic Caching - Words/Language
if ($cache == 1)
{
// any empty string so cause no value to be returned
$query['language'] = $this->translation->getLanguage() ?? 'none__found';
$query['selected_words'] = $this->chapter->getWordText() ?? 'none__found';
}
// Advanced Caching - Verse/Contex
else
{
// any empty string so cause no value to be returned
$query['abbreviation'] = $this->translation->getAbbreviation() ?? 'none__found';
$query['book'] = $this->chapter->getBookNumber() ?? 'none__found';
$query['chapter'] = $this->chapter->getChapterNumber() ?? 'none__found';
$query['verse'] = $this->chapter->getVerseNumber() ?? 'none__found';
// get the integration
$integration = $this->prompt->getIntegration();
if ($integration == 1 || $integration == 3)
{
$query['words'] = $this->chapter->getWordNumber() ?? 'none__found';
}
}
$bucket = $this->load->items($query, 'open_ai_response'); $bucket = $this->load->items($query, 'open_ai_response');
if (is_array($bucket) && $bucket !== []) if (is_array($bucket) && $bucket !== [])

View File

@ -1,3 +1,19 @@
/**
* The Translation class
*
* @var Translation
* @since 2.0.1
*/
protected Translation $translation;
/**
* The Chapter class
*
* @var Chapter
* @since 2.0.1
*/
protected Chapter $chapter;
/** /**
* The Prompt class * The Prompt class
* *
@ -33,14 +49,18 @@
/** /**
* Constructor * Constructor
* *
* @param Prompt $prompt The prompt object. * @param Translation $translation The translation object.
* @param Load $load The load object. * @param Chapter $chapter The chapter object.
* @param Config $config The config object. * @param Prompt $prompt The prompt object.
* @param Load $load The load object.
* @param Config $config The config object.
* *
* @since 2.0.1 * @since 2.0.1
*/ */
public function __construct(Prompt $prompt, Load $load, Config $config) public function __construct(Translation $translation, Chapter $chapter, Prompt $prompt, Load $load, Config $config)
{ {
$this->translation = $translation;
$this->chapter = $chapter;
$this->prompt = $prompt; $this->prompt = $prompt;
$this->load = $load; $this->load = $load;
$this->config = $config; $this->config = $config;
@ -59,7 +79,7 @@
// get the cache behaviour // get the cache behaviour
$cache = $this->prompt->getCacheBehaviour(); $cache = $this->prompt->getCacheBehaviour();
if (empty($cache)) if (empty($cache) || ($cache != 1 && $cache != 2))
{ {
return null; return null;
} }
@ -78,6 +98,29 @@
$query = ['prompt' => $guid]; $query = ['prompt' => $guid];
// Basic Caching - Words/Language
if ($cache == 1)
{
// any empty string so cause no value to be returned
$query['language'] = $this->translation->getLanguage() ?? 'none__found';
$query['selected_words'] = $this->chapter->getWordText() ?? 'none__found';
}
// Advanced Caching - Verse/Contex
else
{
// any empty string so cause no value to be returned
$query['abbreviation'] = $this->translation->getAbbreviation() ?? 'none__found';
$query['book'] = $this->chapter->getBookNumber() ?? 'none__found';
$query['chapter'] = $this->chapter->getChapterNumber() ?? 'none__found';
$query['verse'] = $this->chapter->getVerseNumber() ?? 'none__found';
// get the integration
$integration = $this->prompt->getIntegration();
if ($integration == 1 || $integration == 3)
{
$query['words'] = $this->chapter->getWordNumber() ?? 'none__found';
}
}
$bucket = $this->load->items($query, 'open_ai_response'); $bucket = $this->load->items($query, 'open_ai_response');
if (is_array($bucket) && $bucket !== []) if (is_array($bucket) && $bucket !== [])

View File

@ -11,10 +11,22 @@
"type": "final class", "type": "final class",
"use_selection": { "use_selection": {
"use_selection0": { "use_selection0": {
"use": "c03b9c61-17d3-4774-a335-783903719f83", "use": "c4098e1a-46dd-4d60-9277-b3668a09edc8",
"as": "default" "as": "default"
}, },
"use_selection1": { "use_selection1": {
"use": "4a4c786d-51f4-421a-aa61-262dfd071880",
"as": "default"
},
"use_selection2": {
"use": "3f785d63-a592-463d-9f5f-b2b5a8edd561",
"as": "default"
},
"use_selection3": {
"use": "c03b9c61-17d3-4774-a335-783903719f83",
"as": "default"
},
"use_selection4": {
"use": "3af7864b-f1f3-491e-b16f-0504f890086d", "use": "3af7864b-f1f3-491e-b16f-0504f890086d",
"as": "default" "as": "default"
} }

View File

@ -32,6 +32,7 @@ class Chapter << (F,LightGreen) >> #Green {
- processVerses(?string $abbreviation, ?int $book, ...) : string - processVerses(?string $abbreviation, ?int $book, ...) : string
- processVersesArray(?string $abbreviation, ?int $book, ...) : ?array - processVersesArray(?string $abbreviation, ?int $book, ...) : ?array
- extractVerseNumbers(string $verseNumber) : array - extractVerseNumbers(string $verseNumber) : array
- validateSelectedVerses(array $verseNumbers) : string
- getTextFromVerses(array $verses, array $verseNumber) : array - getTextFromVerses(array $verses, array $verseNumber) : array
- getArrayFromVerses(array $verses, array $verseNumber) : array - getArrayFromVerses(array $verses, array $verseNumber) : array
- getWordNumberArray(?string $abbreviation = null, ?int $book = null, ...) : ?array - getWordNumberArray(?string $abbreviation = null, ?int $book = null, ...) : ?array
@ -240,21 +241,28 @@ note right of Chapter::extractVerseNumbers
return: array return: array
end note end note
note left of Chapter::getTextFromVerses note left of Chapter::validateSelectedVerses
Validate that these verse numbers exist in chapter
since: 2.0.1
return: string
end note
note right of Chapter::getTextFromVerses
Get the verse text from the verses array based on the verse numbers Get the verse text from the verses array based on the verse numbers
since: 2.0.1 since: 2.0.1
return: array return: array
end note end note
note right of Chapter::getArrayFromVerses note left of Chapter::getArrayFromVerses
Get the verses array based on the verse numbers Get the verses array based on the verse numbers
since: 2.0.1 since: 2.0.1
return: array return: array
end note end note
note left of Chapter::getWordNumberArray note right of Chapter::getWordNumberArray
Get the word number/s Get the word number/s
since: 2.0.1 since: 2.0.1
@ -266,35 +274,35 @@ note left of Chapter::getWordNumberArray
?int $chapter = null ?int $chapter = null
end note end note
note right of Chapter::splitAndTrim note left of Chapter::splitAndTrim
Split verse or words by '-' and trim each element. Split verse or words by '-' and trim each element.
since: 2.0.1 since: 2.0.1
return: array return: array
end note end note
note left of Chapter::buildWordNumberArray note right of Chapter::buildWordNumberArray
Build word number array from verse and words. Build word number array from verse and words.
since: 2.0.1 since: 2.0.1
return: array return: array
end note end note
note right of Chapter::wordNumberArrayToString note left of Chapter::wordNumberArrayToString
Converts word number array to string. Converts word number array to string.
since: 2.0.1 since: 2.0.1
return: string return: string
end note end note
note left of Chapter::buildWordArray note right of Chapter::buildWordArray
Build word array from verse and words. Build word array from verse and words.
since: 2.0.1 since: 2.0.1
return: array return: array
end note end note
note right of Chapter::wordArrayToString note left of Chapter::wordArrayToString
Converts word array to string. Converts word array to string.
since: 2.0.1 since: 2.0.1

View File

@ -196,26 +196,27 @@ final class Chapter
return ''; return '';
} }
$verse_bucket = [];
if (strpos($verse, '-') !== false) if (strpos($verse, '-') !== false)
{ {
$verses = $this->splitAndTrim($verse); $verses = $this->splitAndTrim($verse);
$minVerse = min($verses); $min_verse = min($verses);
$maxVerse = max($verses); $max_verse = max($verses);
if ($minVerse == $maxVerse) $verse_bucket[] = $min_verse;
if ($min_verse != $max_verse)
{ {
$verse_number = $minVerse; $verse_bucket[] = $max_verse;
}
else
{
$verse_number = $minVerse . '-' . $maxVerse;
} }
} }
else else
{ {
$verse_number = trim($verse); $verse_bucket[] = trim($verse);
} }
// validate that this chapter has these verses
$verse_number = $this->validateSelectedVerses($verse_bucket);
$this->get($abbreviation, $book, $chapter)->verse_number = $verse_number; $this->get($abbreviation, $book, $chapter)->verse_number = $verse_number;
return $verse_number; return $verse_number;
@ -596,6 +597,44 @@ final class Chapter
return [trim($verseNumber)]; return [trim($verseNumber)];
} }
/**
* Validate that these verse numbers exist in chapter
*
* @param array $verseNumbers The selected verse numbers
*
* @return string The the valid verse numbers or empty string
* @since 2.0.1
*/
private function validateSelectedVerses(array $verseNumbers): string
{
$verses = $this->get()->verses ?? null;
if (empty($verses) || !is_array($verses))
{
return '';
}
$valid = [];
foreach ($verses as $verse)
{
if (isset($verseNumbers[0]) && $verse->verse == $verseNumbers[0])
{
$valid[] = $verseNumbers[0];
}
elseif (isset($verseNumbers[1]) && $verse->verse == $verseNumbers[1])
{
$valid[] = $verseNumbers[1];
}
}
if ($valid !== [])
{
return implode('-', $valid);
}
return '';
}
/** /**
* Get the verse text from the verses array based on the verse numbers * Get the verse text from the verses array based on the verse numbers
* *
@ -777,9 +816,16 @@ final class Chapter
{ {
$word_number = []; $word_number = [];
foreach ($wordNumberArray as $verse => $words) if (count($wordNumberArray) == 1)
{ {
$word_number[] = $verse . ':' . implode(',', $words); $word_number[] = implode(',', array_values($wordNumberArray)[0]);
}
else
{
foreach ($wordNumberArray as $verse => $words)
{
$word_number[] = $verse . ':' . implode(',', $words);
}
} }
return implode(';', $word_number); return implode(';', $word_number);

View File

@ -170,26 +170,27 @@
return ''; return '';
} }
$verse_bucket = [];
if (strpos($verse, '-') !== false) if (strpos($verse, '-') !== false)
{ {
$verses = $this->splitAndTrim($verse); $verses = $this->splitAndTrim($verse);
$minVerse = min($verses); $min_verse = min($verses);
$maxVerse = max($verses); $max_verse = max($verses);
if ($minVerse == $maxVerse) $verse_bucket[] = $min_verse;
if ($min_verse != $max_verse)
{ {
$verse_number = $minVerse; $verse_bucket[] = $max_verse;
}
else
{
$verse_number = $minVerse . '-' . $maxVerse;
} }
} }
else else
{ {
$verse_number = trim($verse); $verse_bucket[] = trim($verse);
} }
// validate that this chapter has these verses
$verse_number = $this->validateSelectedVerses($verse_bucket);
$this->get($abbreviation, $book, $chapter)->verse_number = $verse_number; $this->get($abbreviation, $book, $chapter)->verse_number = $verse_number;
return $verse_number; return $verse_number;
@ -570,6 +571,44 @@
return [trim($verseNumber)]; return [trim($verseNumber)];
} }
/**
* Validate that these verse numbers exist in chapter
*
* @param array $verseNumbers The selected verse numbers
*
* @return string The the valid verse numbers or empty string
* @since 2.0.1
*/
private function validateSelectedVerses(array $verseNumbers): string
{
$verses = $this->get()->verses ?? null;
if (empty($verses) || !is_array($verses))
{
return '';
}
$valid = [];
foreach ($verses as $verse)
{
if (isset($verseNumbers[0]) && $verse->verse == $verseNumbers[0])
{
$valid[] = $verseNumbers[0];
}
elseif (isset($verseNumbers[1]) && $verse->verse == $verseNumbers[1])
{
$valid[] = $verseNumbers[1];
}
}
if ($valid !== [])
{
return implode('-', $valid);
}
return '';
}
/** /**
* Get the verse text from the verses array based on the verse numbers * Get the verse text from the verses array based on the verse numbers
* *
@ -751,9 +790,16 @@
{ {
$word_number = []; $word_number = [];
foreach ($wordNumberArray as $verse => $words) if (count($wordNumberArray) == 1)
{ {
$word_number[] = $verse . ':' . implode(',', $words); $word_number[] = implode(',', array_values($wordNumberArray)[0]);
}
else
{
foreach ($wordNumberArray as $verse => $words)
{
$word_number[] = $verse . ':' . implode(',', $words);
}
} }
return implode(';', $word_number); return implode(';', $word_number);