*
* @param $key Key element to check
* @param $value Value to check
+ * @param $separator Separator for "exploding" $value (default: ',')
* @return $isMatching Whether the key/value is matching or excluded
*/
- function isCriteriaMatching ($key, $value);
+ function isCriteriaMatching ($key, $value, $separator = ',');
}
// [EOF]
* @param $doExit Whether exit the program (true is default)
* @return void
*/
- public function debugBackTrace ($message = '', $doExit = true) {
+ public function debugBackTrace ($message = '', $doExit = TRUE) {
// Sorry, there is no other way getting this nice backtrace
if (!empty($message)) {
// Output message
public final function addCriteria ($criteriaKey, $criteriaValue, $criteriaType = 'default') {
// Debug message
if (strpos($criteriaKey, 'my-') !== false) $this->debugBackTrace('criteriaKey=' . $criteriaKey);
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(strtoupper($criteriaType) . '-CRITERIA: criteriaKey=' . $criteriaKey . ',criteriaValue=' . $criteriaValue);
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(strtoupper($criteriaType) . '-CRITERIA[' . __LINE__ . ']: criteriaKey=' . $criteriaKey . ',criteriaValue=' . $criteriaValue);
// Convert dashes to underscore
$criteriaKey = $this->convertDashesToUnderscores($criteriaKey);
public final function addChoiceCriteria ($criteriaKey, $criteriaValue) {
// Debug message
if (strpos($criteriaKey, 'my-') !== false) $this->debugBackTrace('criteriaKey=' . $criteriaKey);
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CHOICE-CRITERIA: criteriaKey=' . $criteriaKey . ',criteriaValue=' . $criteriaValue);
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CHOICE-CRITERIA[' . __LINE__ . ']: criteriaKey=' . $criteriaKey . ',criteriaValue=' . $criteriaValue);
// Add it
$this->criteria['choice'][$this->convertDashesToUnderscores($criteriaKey)][] = (string) $criteriaValue;
$criteriaKey = $this->convertDashesToUnderscores($criteriaKey);
// Debug message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CRITERIA: criteriaKey=' . $criteriaKey . ',criteria()=' . count($this->criteria[$criteriaType]));
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(strtoupper($criteriaType) . '-CRITERIA[' . __LINE__ . ']: criteriaKey=' . $criteriaKey . ',criteria()=' . count($this->criteria[$criteriaType]));
// Default is not found
$value = NULL;
*
* @param $key Key element to check
* @param $value Value to check
+ * @param $separator Separator for "exploding" $value (default: ',')
* @return $isMatching Whether the key/value is matching or excluded
*/
- public function isCriteriaMatching ($key, $value) {
+ public function isCriteriaMatching ($key, $value, $separator = ',') {
+ // $value cannot be array
+ assert(!is_array($value));
+
+ // "Explode" value
+ $valueArray = explode($separator, $value);
+
+ // Debug message
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SEARCH-CRITERIA[' . __LINE__ . ']: key=' . $key . ',value=' . $value . ' - ENTERED!');
+
// Get 'default' search value
- $search = $this->getCriteriaElemnent($key);
+ $searchDefault = $this->getCriteriaElemnent($key);
+
+ // Debug message
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SEARCH-CRITERIA[' . __LINE__ . ']: getCriteriaElement(' . $key . ')[' . gettype($searchDefault) . ']=' . $searchDefault);
// 'default' check
- $isMatching = ((!is_null($search)) && ($search == $value));
+ $isMatching = ((!is_null($searchDefault)) && ($searchDefault == $value));
- // Get 'choice' search value (can be NULL or comma-separated string)
- $search = $this->getCriteriaChoiceElemnent($key);
+ // Debug message
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SEARCH-CRITERIA[' . __LINE__ . ']: getCriteriaElement(' . $key . ')[' . gettype($searchDefault) . ']=' . $searchDefault . ',isMatching=' . intval($isMatching));
+
+ // Get 'choice' search value (can be NULL or $separator-separated string)
+ $searchChoice = $this->getCriteriaChoiceElemnent($key);
+
+ // Debug message
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SEARCH-CRITERIA[' . __LINE__ . ']: getCriteriaChoiceElement(' . $key . ')[' . gettype($searchChoice) . ']=' . print_r($searchChoice, TRUE));
// 'choice' check
- $isMatching = (($isMatching === TRUE) && ((is_null($search)) || (in_array($value, explode(',', $search)))));
+ if ((is_array($searchChoice)) && (count($valueArray) == 1)) {
+ // $value is a single-search value, so use in_array()
+ $isMatching = (((is_null($searchDefault)) || ($isMatching === TRUE)) && ((is_null($searchChoice)) || ((is_array($searchChoice)) && (in_array($value, $searchChoice)))));
+
+ // Debug message
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SEARCH-CRITERIA[' . __LINE__ . ']: getCriteriaChoiceElement(' . $key . ')[]=' . gettype($searchChoice) . ',value=' . $value . ',isMatching=' . intval($isMatching) . ' - SINGLE-MATCH');
+ } elseif ((is_array($searchChoice)) && (count($valueArray) > 1)) {
+ // $value is choice-search value, so check all entries
+ $isMatching = ((is_null($searchDefault)) || ($isMatching === TRUE));
+ $idx = 0;
+ foreach ($valueArray as $idx => $match) {
+ // Debug message
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SEARCH-CRITERIA[' . __LINE__ . ']: match=' . $match . ',count(searchChoice)=' . count($searchChoice));
+
+ // Is it found? (one is okay)
+ $isMatching = (($isMatching === TRUE) && (in_array($match, $searchChoice)));
+ } // END - foreach
+
+ // Debug message
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SEARCH-CRITERIA[' . __LINE__ . ']: getCriteriaChoiceElement(' . $key . ')[]=' . gettype($searchChoice) . ',valueArray()=' . count($valueArray) . ',idx=' . $idx . ',isMatching=' . intval($isMatching) . ' - CHOICE-MATCH');
+ die(PHP_EOL);
+ } else {
+ // Choice-match is NULL?
+ $isMatching = (($isMatching === TRUE) || (is_null($searchChoice)));
+
+ // Debug message
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SEARCH-CRITERIA[' . __LINE__ . ']: getCriteriaChoiceElement(' . $key . ')[]=' . gettype($searchChoice) . ',value=' . $value . ',isMatching=' . intval($isMatching) . ' - NULL-MATCH');
+ }
+
+ // Debug message
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SEARCH-CRITERIA[' . __LINE__ . ']: getCriteriaChoiceElement(' . $key . ')[]=' . gettype($searchChoice) . ',isMatching=' . intval($isMatching));
// Get 'exclude' search value
- $search = $this->getCriteriaExcludeElemnent($key);
+ $searchExclude = $this->getCriteriaExcludeElemnent($key);
+
+ // Debug message
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SEARCH-CRITERIA[' . __LINE__ . ']: getCriteriaExcludeElement(' . $key . ')[' . gettype($searchExclude) . ']=' . $searchExclude);
// 'exclude' check
- $isMatching = (($isMatching === TRUE) && ((is_null($search)) || ($search != $value)));
+ $isMatching = (((is_null($searchChoice)) || ($isMatching === TRUE)) && ((is_null($searchExclude)) || ($searchExclude != $value)));
// Return result
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SEARCH-CRITERIA[' . __LINE__ . ']: key=' . $key . ',value=' . $value . ',isMatching=' . intval($isMatching) . ' - EXIT!');
return $isMatching;
}
}
// Read the file
$dataArray = $this->getDataArrayFromFile($pathName . $dataFile);
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DATABASE: dataFile=' . $dataFile . ',dataArray='.print_r($dataArray,true));
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DATABASE: dataFile=' . $dataFile . ',dataArray='.print_r($dataArray, TRUE));
// Is this an array?
if (is_array($dataArray)) {
+ // Default is nothing found
+ $isFound = TRUE;
+
// Search in the criteria with FMFW (First Matches, First Wins)
foreach ($dataArray as $key => $value) {
- // Is the criteria met or none set?
- if ($searchInstance->isCriteriaMatching($key, $value)) {
- // Shall we skip this entry?
- if ($searchInstance->getSkip() > 0) {
- // We shall skip some entries
- if ($skipFound < $searchInstance->getSkip()) {
- // Skip this entry
- $skipFound++;
- break;
- } // END - if
+ // Found one entry?
+ $isFound = (($isFound === TRUE) && ($searchInstance->isCriteriaMatching($key, $value)));
+ } // END - foreach
+
+ // Is all found?
+ if ($isFound === TRUE) {
+ // Shall we skip this entry?
+ if ($searchInstance->getSkip() > 0) {
+ // We shall skip some entries
+ if ($skipFound < $searchInstance->getSkip()) {
+ // Skip this entry
+ $skipFound++;
+ break;
} // END - if
+ } // END - if
- // Set id number
- $dataArray[$this->getIndexKey()] = $idx;
+ // Set id number
+ $dataArray[$this->getIndexKey()] = $idx;
- // Entry found!
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DATABASE: indexKey=' . $this->getIndexKey() . ',idx=' . $idx . ',dataArray=' . print_r($dataArray, true));
- $resultData[BaseDatabaseBackend::RESULT_INDEX_ROWS][] = $dataArray;
+ // Entry found!
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DATABASE: indexKey=' . $this->getIndexKey() . ',idx=' . $idx . ',dataArray=' . print_r($dataArray, TRUE));
+ $resultData[BaseDatabaseBackend::RESULT_INDEX_ROWS][] = $dataArray;
- // Count found entries up
- $limitFound++;
- break;
- } // END - if
- } // END - foreach
+ // Count found entries up
+ $limitFound++;
+ } // END - if
} else {
// Throw an exception here
throw new SqlException(array($this, sprintf("File '%s' contains invalid data.", $dataFile), self::DB_CODE_DATA_FILE_CORRUPT), self::EXCEPTION_SQL_QUERY);
// Open this file for reading
$dataArray = $this->getDataArrayFromFile($pathName . $dataFile);
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DATABASE: dataFile=' . $dataFile . ',dataArray='.print_r($dataArray,true));
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DATABASE: dataFile=' . $dataFile . ',dataArray='.print_r($dataArray, TRUE));
// Is this an array?
if (is_array($dataArray)) {
+ // Default is nothing found
+ $isFound = TRUE;
+
// Search in the criteria with FMFW (First Matches, First Wins)
foreach ($dataArray as $key => $value) {
- // Is the criteria met?
- if ($searchInstance->isCriteriaMatching($key, $value)) {
- // Shall we skip this entry?
- if ($searchInstance->getSkip() > 0) {
- // We shall skip some entries
- if ($skipFound < $searchInstance->getSkip()) {
- // Skip this entry
- $skipFound++;
- break;
- } // END - if
+ // Found one entry?
+ $isFound = (($isFound === TRUE) && ($searchInstance->isCriteriaMatching($key, $value)));
+ } // END - foreach
+
+ // Is all found?
+ if ($isFound === TRUE) {
+ // Shall we skip this entry?
+ if ($searchInstance->getSkip() > 0) {
+ // We shall skip some entries
+ if ($skipFound < $searchInstance->getSkip()) {
+ // Skip this entry
+ $skipFound++;
+ break;
} // END - if
+ } // END - if
- // Entry found, so update it
- foreach ($searchArray as $searchKey => $searchValue) {
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DATABASE: criteriaKey=' . $searchKey . ',criteriaValue=' . $searchValue);
- $dataArray[$searchKey] = $searchValue;
- } // END - foreach
+ // Entry found, so update it
+ foreach ($searchArray as $searchKey => $searchValue) {
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DATABASE: criteriaKey=' . $searchKey . ',criteriaValue=' . $searchValue);
+ $dataArray[$searchKey] = $searchValue;
+ } // END - foreach
- // Write the data to a local file
- $this->writeDataArrayToFqfn($pathName . $dataFile, $dataArray);
+ // Write the data to a local file
+ $this->writeDataArrayToFqfn($pathName . $dataFile, $dataArray);
- // Count it
- $limitFound++;
- break;
- } // END - if
- } // END - foreach
+ // Count found entries up
+ $limitFound++;
+ } // END - if
} // END - if
} // END - while
}
// Create method name
- $methodName = sprintf("create%s",
- $className
- );
+ $methodName = sprintf("create%s", $className);
// Run the user function
$objectInstance = call_user_func_array(array($className, $methodName), $args);