X-Git-Url: https://git.mxchange.org/?p=core.git;a=blobdiff_plain;f=framework%2Fmain%2Fclasses%2Fdatabase%2Fresult%2Fclass_CachedDatabaseResult.php;h=c79eabfdb90077856adf610c06f29e7c38febb67;hp=747660386c1e29384599f21e9ca3e84102d43e16;hb=HEAD;hpb=fc83e6b1ac6fe3a5e09a3e4f8bad20fa2240cae4 diff --git a/framework/main/classes/database/result/class_CachedDatabaseResult.php b/framework/main/classes/database/result/class_CachedDatabaseResult.php index 74766038..4296044a 100644 --- a/framework/main/classes/database/result/class_CachedDatabaseResult.php +++ b/framework/main/classes/database/result/class_CachedDatabaseResult.php @@ -14,6 +14,7 @@ use Org\Mxchange\CoreFramework\Result\Update\UpdateableResult; // Import SPL stuff use \InvalidArgumentException; +use \OutOfBoundsException; use \SeekableIterator; /** @@ -21,7 +22,7 @@ use \SeekableIterator; * * @author Roland Haeder * @version 0.0.0 - * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2023 Core Developer Team * @license GNU GPL 3.0 or any newer version * @link http://www.shipsimu.org * @@ -78,9 +79,13 @@ class CachedDatabaseResult extends BaseDatabaseResult implements SearchableResul * * @return void */ - protected function __construct () { + private function __construct () { // Call parent constructor + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('CACHED-DATABASE-RESULT: CONSTRUCTED!'); parent::__construct(__CLASS__); + + // Trace message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('CACHED-DATABASE-RESULT: EXIT!'); } /** @@ -90,27 +95,37 @@ class CachedDatabaseResult extends BaseDatabaseResult implements SearchableResul * @return $resultInstance An instance of this class * @throws InvalidArgumentException If a parameter is invalid */ - public static final function createCachedDatabaseResult (array $resultArray) { + public static final function createCachedDatabaseResult (array $resultArray): CachedDatabaseResult { // Misses an element? + //* DEBUG-DIE: */ die(sprintf('[%s:%d]: resultArray=%s', __METHOD__, __LINE__, print_r($resultArray, true))); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('CACHED-DATABASE-RESULT: resultArray()=%d - CALLED!', count($resultArray))); if (count($resultArray) == 0) { // Cannot be empty - throw new InvalidArgumentException('Array "resultArray" cannot be empty.'); + throw new InvalidArgumentException('Array "resultArray" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT); } elseif (!array_key_exists(BaseDatabaseResult::RESULT_NAME_ROWS, $resultArray)) { // Yes, then abort here throw new InvalidArgumentException(sprintf('resultArray(%d)=%s has no element "%s".', count($resultArray), print_r($resultArray, TRUE), BaseDatabaseResult::RESULT_NAME_ROWS)); + } elseif (!array_key_exists(BaseDatabaseResult::RESULT_NAME_STATUS, $resultArray)) { + // Yes, then abort here + throw new InvalidArgumentException(sprintf('resultArray(%d)=%s has no element "%s".', count($resultArray), print_r($resultArray, TRUE), BaseDatabaseResult::RESULT_NAME_STATUS)); } // Get a new instance $resultInstance = new CachedDatabaseResult(); - // Set the result array and reset current position + // Set the result array + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('CACHED-DATABASE-RESULT: Setting resultArray()=%d ...', count($resultArray))); $resultInstance->setResultArray($resultArray); + + // Reset current position + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('CACHED-DATABASE-RESULT: Invoking resultInstance->resetCurrentPosition() ...'); $resultInstance->resetCurrentPosition(); // Set affected rows $resultInstance->setAffectedRows(count($resultArray[BaseDatabaseResult::RESULT_NAME_ROWS])); // Return the instance + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('CACHED-DATABASE-RESULT: resultInstance=%s - EXIT!', $resultInstance->__toString())); return $resultInstance; } @@ -120,7 +135,7 @@ class CachedDatabaseResult extends BaseDatabaseResult implements SearchableResul * @param $resultArray The array holding the result from query * @return void */ - protected final function setResultArray (array $resultArray) { + protected final function setResultArray (array $resultArray): void { $this->resultArray = $resultArray; } @@ -130,18 +145,23 @@ class CachedDatabaseResult extends BaseDatabaseResult implements SearchableResul * @param $updateInstance An instance of an Updateable criteria * @return void */ - private function updateCurrentEntryByCriteria (LocalUpdateCriteria $updateInstance) { + private function updateCurrentEntryByCriteria (LocalUpdateCriteria $updateInstance): void { // Get the current entry key + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('CACHED-DATABASE-RESULT: updateInstance=%s - CALLED!', $updateInstance->__toString())); $entryKey = $this->key(); // Now get the update criteria array and update all entries foreach ($updateInstance->getUpdateCriteria() as $criteriaKey => $criteriaValue) { // Update data + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('CACHED-DATABASE-RESULT: criteriaKey=%s,criteriaValue[%s]=%s', $criteriaKey, gettype($criteriaValue), $criteriaValue)); $this->resultArray[BaseDatabaseResult::RESULT_NAME_ROWS][$entryKey][$criteriaKey] = $criteriaValue; // Mark it as out-dated $this->outDated[$criteriaKey] = 1; } + + // Trace message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('CACHED-DATABASE-RESULT: EXIT!'); } /** @@ -150,39 +170,48 @@ class CachedDatabaseResult extends BaseDatabaseResult implements SearchableResul * * @return $nextValid Whether the next entry is valid */ - public function next () { - // Default is not valid - $nextValid = false; - + public function next (): void { // Increase position + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('CACHED-DATABASE-RESULT: CALLED!'); $this->currentPos++; // Is the result valid? if ($this->valid()) { // Next entry found, so cache it $this->currentRow = $this->resultArray[BaseDatabaseResult::RESULT_NAME_ROWS][$this->currentPos]; - $nextValid = true; } - // Return the result - return $nextValid; + // Trace message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('CACHED-DATABASE-RESULT: EXIT!'); } /** * Seeks for to a specified position * - * @param $index Index to seek for + * @param $seekPosition Position to seek to * @return void + * @throws OutOfBoundsException If the position is not seekable */ - public function seek (int $index) { + public function seek (int $seekPosition): void { + // Validate parameter + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('CACHED-DATABASE-RESULT: seekPosition=%d - CALLED!', $seekPosition)); + if ($seekPosition < 0) { + // Throw exception + throw new OutOfBoundsException(sprintf('seekPositon=%d is not seekable', $seekPosition)); + } + // Rewind to beginning $this->rewind(); // Search for the entry - while (($this->currentPos < $index) && ($this->valid())) { + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('CACHED-DATABASE-RESULT: this->currentPos=%d,seekPosition=%d', $this->currentPos, $seekPosition)); + while (($this->currentPos < $seekPosition) && ($this->valid())) { // Continue on $this->next(); } + + // Trace message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('CACHED-DATABASE-RESULT: EXIT!'); } /** @@ -190,8 +219,9 @@ class CachedDatabaseResult extends BaseDatabaseResult implements SearchableResul * * @return $current Current element to give back */ - public function current () { + public function current (): mixed { // Default is not found + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('CACHED-DATABASE-RESULT: CALLED!'); $current = NULL; // Does the current enty exist? @@ -201,6 +231,7 @@ class CachedDatabaseResult extends BaseDatabaseResult implements SearchableResul } // Return the result + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('CACHED-DATABASE-RESULT: current[]=%s - EXIT!', gettype($current))); return $current; } @@ -209,28 +240,28 @@ class CachedDatabaseResult extends BaseDatabaseResult implements SearchableResul * * @return $isValid Whether the next/rewind entry is valid */ - public function valid () { + public function valid (): bool { // Check if all is fine ... - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-DATABASE-RESULT: this->currentPos=%d - CALLED!', $this->currentPos)); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('CACHED-DATABASE-RESULT: this->currentPos=%d - CALLED!', $this->currentPos)); $isValid = ($this->ifStatusIsOkay() && isset($this->resultArray[BaseDatabaseResult::RESULT_NAME_ROWS][$this->currentPos]) && isset($this->resultArray[BaseDatabaseResult::RESULT_NAME_ROWS][0])); // Return the result - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-DATABASE-RESULT: isValid=%d - EXIT!', intval($isValid))); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('CACHED-DATABASE-RESULT: isValid=%d - EXIT!', intval($isValid))); return $isValid; } /** * Returns count of entries * - * @return $isValid Whether the next/rewind entry is valid + * @return $count Count of total rows */ - public function count () { + public function count (): int { // Count rows - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-DATABASE-RESULT: CALLED!'); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('CACHED-DATABASE-RESULT: CALLED!'); $count = count($this->resultArray[BaseDatabaseResult::RESULT_NAME_ROWS]); // Return it - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-DATABASE-RESULT: count=%d - EXIT!', $count)); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('CACHED-DATABASE-RESULT: count=%d - EXIT!', $count)); return $count; } @@ -239,13 +270,13 @@ class CachedDatabaseResult extends BaseDatabaseResult implements SearchableResul * * @return $ifStatusOkay Whether the status of the query was okay */ - public function ifStatusIsOkay () { + public function ifStatusIsOkay (): bool { // Check all conditions - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-DATABASE-RESULT: this->currentPos=%d - CALLED!', $this->currentPos)); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('CACHED-DATABASE-RESULT: this->currentPos=%d - CALLED!', $this->currentPos)); $ifStatusOkay = (isset($this->resultArray[BaseDatabaseResult::RESULT_NAME_STATUS]) && $this->resultArray[BaseDatabaseResult::RESULT_NAME_STATUS] === BaseDatabaseBackend::RESULT_OKAY); // Return status - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-DATABASE-RESULT: ifStatusOkay=%s - EXIT!', intval($ifStatusOkay))); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('CACHED-DATABASE-RESULT: ifStatusOkay=%s - EXIT!', intval($ifStatusOkay))); return $ifStatusOkay; } @@ -254,9 +285,9 @@ class CachedDatabaseResult extends BaseDatabaseResult implements SearchableResul * * @return $currentPos Key from iterator */ - public function key () { + public function key (): int { // Return current array position - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-DATABASE-RESULT: this->currentPos=%d - CALLED!', $this->currentPos)); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('CACHED-DATABASE-RESULT: this->currentPos=%d - CALLED!', $this->currentPos)); return $this->currentPos; } @@ -265,14 +296,14 @@ class CachedDatabaseResult extends BaseDatabaseResult implements SearchableResul * * @return void */ - public function rewind () { + public function rewind (): void { // Reset both current array position and current row - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-DATABASE-RESULT: this->currentPos=%d - CALLED!', $this->currentPos)); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('CACHED-DATABASE-RESULT: this->currentPos=%d - CALLED!', $this->currentPos)); $this->resetCurrentPosition(); $this->currentRow = []; // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-DATABASE-RESULT: EXIT!'); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('CACHED-DATABASE-RESULT: EXIT!'); } /** @@ -281,13 +312,13 @@ class CachedDatabaseResult extends BaseDatabaseResult implements SearchableResul * * @return void */ - private function resetCurrentPosition () { + private function resetCurrentPosition (): void { // Reset position - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-DATABASE-RESULT: CALLED!'); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('CACHED-DATABASE-RESULT: CALLED!'); $this->currentPos = ($this->count() > 0 ? 0 : -1); // Trace message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHED-DATABASE-RESULT: EXIT!'); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('CACHED-DATABASE-RESULT: this->currentPos=%d - EXIT!', $this->currentPos)); } /** @@ -310,41 +341,59 @@ class CachedDatabaseResult extends BaseDatabaseResult implements SearchableResul * @return void * @throws ResultUpdateException If no result was updated */ - public function add2UpdateQueue (LocalUpdateCriteria $updateInstance) { + public function add2UpdateQueue (LocalUpdateCriteria $updateInstance): void { // Rewind the pointer + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('CACHED-DATABASE-RESULT: updateInstance=%s - CALLED!', $updateInstance->__toString())); $this->rewind(); // Get search criteria $searchInstance = $updateInstance->getSearchInstance(); - // And start looking for the result + // Get affected rows + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('CACHED-DATABASE-RESULT: searchInstance=%s', $searchInstance->__toString())); $foundEntries = $this->getAffectedRows(); + + // And start looking for the result + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('CACHED-DATABASE-RESULT: foundEntries=%d,searchInstance->limit=%d', $foundEntries, $searchInstance->getLimit())); while (($this->valid()) && ($foundEntries < $searchInstance->getLimit())) { // Get next entry + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('CACHED-DATABASE-RESULT: Invoking this->next() ...'); $this->next(); + + // Get current entry + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('CACHED-DATABASE-RESULT: Invoking this->current() ...'); $currentEntry = $this->current(); // Is this entry found? + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('CACHED-DATABASE-RESULT: currentEntry[]=%s - EXIT!', gettype($currentEntry))); if ($searchInstance->ifEntryMatches($currentEntry)) { // Update this entry + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('CACHED-DATABASE-RESULT: Invoking this->updateCurrentEntryByCriteria(%s) ...', $updateInstance->__toString())); $this->updateCurrentEntryByCriteria($updateInstance); // Count one up $foundEntries++; + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('CACHED-DATABASE-RESULT: foundEntries=%d', $foundEntries)); } } // If no entry is found/updated throw an exception + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('CACHED-DATABASE-RESULT: foundEntries=%d', $foundEntries)); if ($foundEntries == 0) { // Throw an exception here throw new ResultUpdateException($this, self::EXCEPTION_RESULT_UPDATE_FAILED); } // Set affected rows + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('CACHED-DATABASE-RESULT: Invoking this->setAffectedRows(%d) ...', $foundEntries)); $this->setAffectedRows($foundEntries); // Set update instance + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('CACHED-DATABASE-RESULT: Invoking this->setUpdateInstance(%s) ...', $updateInstance->__toString())); $this->setUpdateInstance($updateInstance); + + // Trace message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('CACHED-DATABASE-RESULT: EXIT!'); } /** @@ -353,7 +402,7 @@ class CachedDatabaseResult extends BaseDatabaseResult implements SearchableResul * @param $rows Number of affected rows * @return void */ - public final function setAffectedRows (int $rows) { + public final function setAffectedRows (int $rows): void { $this->affectedRows = $rows; } @@ -362,7 +411,7 @@ class CachedDatabaseResult extends BaseDatabaseResult implements SearchableResul * * @return $rows Number of affected rows */ - public final function getAffectedRows () { + public final function getAffectedRows (): int { return $this->affectedRows; } @@ -380,8 +429,13 @@ class CachedDatabaseResult extends BaseDatabaseResult implements SearchableResul * * @return $needsUpdate Whether we have out-dated entries */ - public function ifDataNeedsFlush () { + public function ifDataNeedsFlush (): bool { + // Check if records are out-dated + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('CACHED-DATABASE-RESULT: CALLED!'); $needsUpdate = (count($this->outDated) > 0); + + // Return it + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('CACHED-DATABASE-RESULT: needsUpdate=%d - EXIT!', intval($needsUpdate))); return $needsUpdate; } @@ -391,15 +445,21 @@ class CachedDatabaseResult extends BaseDatabaseResult implements SearchableResul * @param $criteriaInstance An instance of a StoreableCriteria class * @return void */ - public function addElementsToDataSet (StoreableCriteria $criteriaInstance) { + public function addElementsToDataSet (StoreableCriteria $criteriaInstance): void { // Walk only through out-dated columns + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('CACHED-DATABASE-RESULT: criteriaInstance=%s - CALLED!', $criteriaInstance->__toString())); foreach ($this->outDated as $key => $dummy) { // Does this key exist? + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('CACHED-DATABASE-RESULT: key=%s,dummy[]=%s', $key, gettype($dummy))); if ($this->find($key)) { // Then update it + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('CACHED-DATABASE-RESULT: Invoking criteriaInstance->addCriteria(%s,foundValue[]=%s) ...', $key, gettype($this->getFoundValue()))); $criteriaInstance->addCriteria($key, $this->getFoundValue()); } } + + // Trace message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('CACHED-DATABASE-RESULT: EXIT!'); } /** @@ -407,36 +467,51 @@ class CachedDatabaseResult extends BaseDatabaseResult implements SearchableResul * * @param $key The key we shall find * @return $found Whether the key was found or not + * @throws InvalidArgumentException If a parameter is invalid */ - public function find (string $key) { + public function find (string $key): bool { + // Check parameter + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('CACHED-DATABASE-RESULT: key=%s - CALLED!', $key)); + if (empty($key)) { + // Throw IAE + throw new InvalidArgumentException('Parameter "key" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT); + } + // By default nothing is found $found = false; // Rewind the pointer + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('CACHED-DATABASE-RESULT: Invoking this->rewind() ...'); $this->rewind(); // Walk through all entries while ($this->valid()) { // Advance to next entry + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('CACHED-DATABASE-RESULT: Invoking this->next() ...'); $this->next(); // Get the whole array $currentEntry = $this->current(); // Is the element there? + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('CACHED-DATABASE-RESULT: currentEntry[]=%s - EXIT!', gettype($currentEntry))); if (isset($currentEntry[$key])) { // Okay, found! + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('CACHED-DATABASE-RESULT: key=%s is found in currentEntry', $key)); $found = true; // So "cache" it + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('CACHED-DATABASE-RESULT: Setting this->foundValue to currentEntry[%s][]=%s is found in currentEntry', $key, gettype($currentEntry[$key]))); $this->foundValue = $currentEntry[$key]; // And stop searching + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage('CACHED-DATABASE-RESULT: BREAK!'); break; } } // Return the result + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('CACHED-DATABASE-RESULT: found=%d - EXIT!', intval($found))); return $found; } @@ -445,26 +520,47 @@ class CachedDatabaseResult extends BaseDatabaseResult implements SearchableResul * * @param $databaseColumn Database column where the index might be found * @param $frontendInstance The frontend instance to ask for array element - * @para $callBack Call-back object for setting the index; + * @para $callback Call-back object for setting the index; * 0=object instance,1=method name * @return void + * @throws InvalidArgumentException If a parameter is invalid * @todo Find a caching way without modifying the result array */ - public function solveResultIndex (string $databaseColumn, DatabaseFrontend $frontendInstance, array $callBack) { + public function solveResultIndex (string $databaseColumn, DatabaseFrontend $frontendInstance, array $callback): void { + // Check parameter + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('CACHED-DATABASE-RESULT: databaseColumn=%s,frontendInstance=%s,callback()=%d - CALLED!', $databaseColumn, $frontendInstance->__toString(), count($callback))); + if (empty($key)) { + // Throw IAE + throw new InvalidArgumentException('Parameter "key" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT); + } elseif (count($callback) != 2) { + // Throw it again + throw new InvalidArgumentException(sprintf('callback()=%d must be exactly 2', count($callback)), FrameworkInterface::EXCEPTION_INVALID_ARGUMENT); + } elseif (!is_callable($callback)) { + // Throw it again + throw new InvalidArgumentException(sprintf('%s:%s() is not callable', $callback[0], $callback[1]), FrameworkInterface::EXCEPTION_INVALID_ARGUMENT); + } + // By default nothing is found $indexValue = 0; // Is the element in result itself found? + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('CACHED-DATABASE-RESULT: Invoking this->find(%s),frontendInstance->indexKey=%s ...', $databaseColumn, $frontendInstance->getIndexKey())); if ($this->find($databaseColumn)) { // Use this value $indexValue = $this->getFoundValue(); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('CACHED-DATABASE-RESULT: indexValue[]=%s from this->find(%s) - databaseColumn', gettype($indexValue), $databaseColumn)); } elseif ($this->find($frontendInstance->getIndexKey())) { // Use this value $indexValue = $this->getFoundValue(); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('CACHED-DATABASE-RESULT: indexValue[]=%s from this->find(%s) - frontendInstance->indexKey', gettype($indexValue), $frontendInstance->getIndexKey())); } // Set the index - call_user_func_array($callBack, array($indexValue)); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('CACHED-DATABASE-RESULT: Invoking %s:%s(indexValue[]=%s) ...', $callback[0], $callback[1], gettype($indexValue))); + call_user_func_array($callback, [$indexValue]); + + // Trace message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('CACHED-DATABASE-RESULT: EXIT!'); } }