From: Roland Häder Date: Fri, 10 Feb 2023 15:41:58 +0000 (+0100) Subject: Continued: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=3994d9152d5af5bd403dae76e256a830096d9749;p=core.git Continued: - added more debug lines - added more thrown exceptions - rewrote assert() lines to thrown exceptions --- diff --git a/framework/main/classes/criteria/class_BaseCriteria.php b/framework/main/classes/criteria/class_BaseCriteria.php index f1221d67..8590f09d 100644 --- a/framework/main/classes/criteria/class_BaseCriteria.php +++ b/framework/main/classes/criteria/class_BaseCriteria.php @@ -8,6 +8,11 @@ use Org\Mxchange\CoreFramework\Criteria\Search\SearchCriteria; use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem; use Org\Mxchange\CoreFramework\Utils\Strings\StringUtils; +// Import SPL stuff +use \BadMethodCallException; +use \InvalidArgumentException; +use \UnexpectedValueException; + /** * A general crtieria class * @@ -36,6 +41,11 @@ abstract class BaseCriteria extends BaseFrameworkSystem implements Criteria { */ private $frontendConfigEntry = ''; + /** + * All supported criteria types + */ + private static $CRITERIA_TYPES = []; + /** * Protected constructor * @@ -46,9 +56,13 @@ abstract class BaseCriteria extends BaseFrameworkSystem implements Criteria { // Call parent constructor parent::__construct($className); + // Initialize valid criteria types array + self::$CRITERIA_TYPES = [Criteria::CRITERIA_TYPE_DEFAULT, Criteria::CRITERIA_TYPE_CHOICE, Criteria::CRITERIA_TYPE_EXCLUDE]; + // Initialize all criteria arrays - foreach (array('default', 'choice', 'exclude') as $criteriaType) { + foreach (self::$CRITERIA_TYPES as $criteriaType) { // Init it + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-CRITERIA: Initializing criteriaType=%s ...', strtoupper($criteriaType), $criteriaType)); $this->initGenericArrayKey('criteria', $criteriaType, 'entries'); } } @@ -59,9 +73,21 @@ abstract class BaseCriteria extends BaseFrameworkSystem implements Criteria { * * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude' * @return $count Count of all criteria entries + * @throws InvalidArgumentException If a parameter is not valid */ - protected final function count (string $criteriaType = 'default') { - // Return it + protected final function count (string $criteriaType = Criteria::CRITERIA_TYPE_DEFAULT) { + // Check parameter + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-CRITERIA: criteriaType=%s - CALLED!', strtoupper($criteriaType), $criteriaType)); + if (empty($criteriaType)) { + // Throw it again + throw new InvalidArgumentException('Parameter "criteriaType" is empty'); + } elseif (!in_array($criteriaType, self::$CRITERIA_TYPES)) { + // Throw it again + throw new InvalidArgumentException(sprintf('criteriaType=%s is not supported', $criteriaType)); + } + + // Invoke inner method + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-CRITERIA: Invoking this->countGenericArrayGroup(criteria, %s) ...', strtoupper($criteriaType), $criteriaType)); return $this->countGenericArrayGroup('criteria', $criteriaType); } @@ -70,39 +96,74 @@ abstract class BaseCriteria extends BaseFrameworkSystem implements Criteria { * * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude' * @param $criteriaKey Criteria key - * @return $isSet Whether key is set + * @return $isset Whether key is set + * @throws InvalidArgumentException If a parameter is not valid + * @throws UnexpectedValueException If a parameter contains an unexpected/unsupported value */ public function isKeySet (string $criteriaType, string $criteriaKey) { - // Make sure no 'my-' or 'my_' passes this point - assert((strpos($criteriaKey, 'my-') === false) && (strpos($criteriaKey, 'my_') === false)); + // Check parameters + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-CRITERIA: criteriaType=%s,criteriaKey=%s - CALLED!', strtoupper($criteriaType), $criteriaType, $criteriaKey)); + if (empty($criteriaType)) { + // Throw it again + throw new InvalidArgumentException('Parameter "criteriaType" is empty'); + } elseif (!in_array($criteriaType, self::$CRITERIA_TYPES)) { + // Throw it again + throw new UnexpectedValueException(sprintf('criteriaType=%s is not supported', $criteriaType)); + } elseif (empty($criteriaKey)) { + // Throw it again + throw new InvalidArgumentException('Parameter "criteriaKey" is empty'); + } elseif ((strpos($criteriaKey, 'my-') !== false) || (strpos($criteriaKey, 'my_') !== false)) { + // Throw it again + throw new InvalidArgumentException(sprintf('criteriaKey=%s has illegal prefix "my"', $criteriaKey)); + } // Determine it - $isSet = $this->isGenericArrayElementSet('criteria', $criteriaType, 'entries', $criteriaKey); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-CRITERIA: Invoking this->isGenericArrayElementSet(criteria,%s,entries,%s) ...', strtoupper($criteriaType), $criteriaType, $criteriaKey)); + $isset = $this->isGenericArrayElementSet('criteria', $criteriaType, 'entries', $criteriaKey); // Return it - return $isSet; + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-CRITERIA: isset=%d - EXIT!', strtoupper($criteriaType), intval($isset))); + return $isset; } /** * Checks whether given key is set for 'choice' type * * @param $criteriaKey Criteria key - * @return $isSet Whether key is set + * @return $isset Whether key is set + * @throws InvalidArgumentException If a parameter is not valid */ public function isChoiceKeySet (string $criteriaKey) { - // Call inner method - return $this->isKeySet('choice', $criteriaKey); + // Validate parameter + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-CRITERIA: criteriaKey=%s - CALLED!', $criteriaKey)); + if (empty($criteriaKey)) { + // Throw it again + throw new InvalidArgumentException('Parameter "criteriaKey" is empty'); + } + + // Invoke inner method + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-CRITERIA: Invoking this->isKeySet(%s,%s) ...', Criteria::CRITERIA_TYPE_CHOICE, $criteriaKey)); + return $this->isKeySet(Criteria::CRITERIA_TYPE_CHOICE, $criteriaKey); } /** * Checks whether given key is set for 'exclude' type * * @param $criteriaKey Criteria key - * @return $isSet Whether key is set + * @return $isset Whether key is set + * @throws InvalidArgumentException If a parameter is not valid */ public function isExcludeKeySet (string $criteriaKey) { - // Call inner method - return $this->isKeySet('exclude', $criteriaKey); + // Validate parameter + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-CRITERIA: criteriaKey=%s - CALLED!', $criteriaKey)); + if (empty($criteriaKey)) { + // Throw it again + throw new InvalidArgumentException('Parameter "criteriaKey" is empty'); + } + + // Invoke inner method + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-CRITERIA: Invoking this->isKeySet(%s,%s) ...', Criteria::CRITERIA_TYPE_EXCLUDE, $criteriaKey)); + return $this->isKeySet(Criteria::CRITERIA_TYPE_EXCLUDE, $criteriaKey); } /** @@ -129,8 +190,22 @@ abstract class BaseCriteria extends BaseFrameworkSystem implements Criteria { * * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude' * @return $criteria + * @throws InvalidArgumentException If a parameter is not valid + * @throws UnexpectedValueException If a parameter contains an unexpected/unsupported value */ - public final function getCriteriaArray (string $criteriaType = 'default') { + public final function getCriteriaArray (string $criteriaType = Criteria::CRITERIA_TYPE_DEFAULT) { + // Check parameters + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-CRITERIA: criteriaType=%s - CALLED!', strtoupper($criteriaType), $criteriaType)); + if (empty($criteriaType)) { + // Throw it again + throw new InvalidArgumentException('Parameter "criteriaType" is empty'); + } elseif (!in_array($criteriaType, self::$CRITERIA_TYPES)) { + // Throw it again + throw new UnexpectedValueException(sprintf('criteriaType=%s is not supported', $criteriaType)); + } + + // Invoke inner method + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-CRITERIA: Invoking this->getGenericArrayKey(criteria,%s,entries) ...', strtoupper($criteriaType), $criteriaType)); return $this->getGenericArrayKey('criteria', $criteriaType, 'entries'); } @@ -140,7 +215,8 @@ abstract class BaseCriteria extends BaseFrameworkSystem implements Criteria { * @return $criteria */ public final function getCriteriaChoiceArray () { - return $this->getCriteriaArray('choice'); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-CRITERIA: CALLED!', strtoupper($criteriaType))); + return $this->getCriteriaArray(Criteria::CRITERIA_TYPE_CHOICE); } /** @@ -149,7 +225,8 @@ abstract class BaseCriteria extends BaseFrameworkSystem implements Criteria { * @return $criteria */ public final function getCriteriaExcludeArray () { - return $this->getCriteriaArray('exclude'); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-CRITERIA: CALLED!', strtoupper($criteriaType))); + return $this->getCriteriaArray(Criteria::CRITERIA_TYPE_EXCLUDE); } /** @@ -159,17 +236,29 @@ abstract class BaseCriteria extends BaseFrameworkSystem implements Criteria { * @return void */ public final function unsetCriteria (string $criteriaKey) { - // Make sure no 'my-' or 'my_' passes this point - assert((strpos($criteriaKey, 'my-') === false) && (strpos($criteriaKey, 'my_') === false)); + // Check parameter + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-CRITERIA: criteriaKey=%s - CALLED!', $criteriaKey)); + if (empty($criteriaKey)) { + // Throw it again + throw new InvalidArgumentException('Parameter "criteriaKey" is empty'); + } elseif ((strpos($criteriaKey, 'my-') !== false) || (strpos($criteriaKey, 'my_') !== false)) { + // Throw it again + throw new InvalidArgumentException(sprintf('criteriaKey=%s has illegal prefix "my"', $criteriaKey)); + } // Convert dashes to underscore $criteriaKey = StringUtils::convertDashesToUnderscores($criteriaKey); // "Walk" through all criterias + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-CRITERIA: criteriaKey=%s', strtoupper($criteriaType), $criteriaKey)); foreach ($this->getGenericArray('criteria') as $criteriaType => $dummy) { // Remove it + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-CRITERIA: Removing criteriaType=%s,criteriaKey=%s ...', strtoupper($criteriaType), $criteriaType, $criteriaKey)); $this->unsetGenericArrayElement('criteria', $criteriaType, 'entries', $criteriaKey); } + + // Trace message + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-CRITERIA: EXIT!'); } /** @@ -180,22 +269,38 @@ abstract class BaseCriteria extends BaseFrameworkSystem implements Criteria { * @param $criteriaValue Criteria value * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude' * @return void + * @throws InvalidArgumentException If a parameter is not valid + * @throws UnexpectedValueException If a parameter contains an unexpected/unsupported value */ - public final function addCriteria (string $criteriaKey, $criteriaValue, string $criteriaType = 'default') { - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($criteriaType) . '-CRITERIA: criteriaKey=' . $criteriaKey . ',criteriaValue=' . $criteriaValue . ',criteriaType=' . $criteriaType . ' - CALLED!'); - - // Make sure no 'my-' or 'my_' passes this point - assert((strpos($criteriaKey, 'my-') === false) && (strpos($criteriaKey, 'my_') === false) && (!is_bool($criteriaValue))); + public final function addCriteria (string $criteriaKey, $criteriaValue, string $criteriaType = Criteria::CRITERIA_TYPE_DEFAULT) { + // Check parameter + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s--CRITERIA: criteriaKey=%s,criteriaValue[]=%s$criteriaValue,criteriaType=%s - CALLED!', strtoupper($criteriaType), $criteriaKey, gettype($criteriaValue), $criteriaType)); + if (empty($criteriaKey)) { + // Throw IAE + throw new InvalidArgumentException('Parameter "criteriaKey" is empty'); + } elseif ((strpos($criteriaKey, 'my-') !== false) || (strpos($criteriaKey, 'my_') !== false)) { + // Throw it again + throw new InvalidArgumentException(sprintf('criteriaKey=%s has illegal prefix "my"', $criteriaKey)); + } elseif (is_array($criteriaValue) || is_bool($criteriaValue) || is_object($criteriaValue) || is_resource($criteriaValue)) { + // Throw it again + throw new InvalidArgumentException(sprintf('value[]=%s is not supported', gettype($criteriaValue))); + } elseif (empty($criteriaType)) { + // Throw it again + throw new InvalidArgumentException('Parameter "criteriaType" is empty'); + } elseif (!in_array($criteriaType, self::$CRITERIA_TYPES)) { + // Throw it again + throw new UnexpectedValueException(sprintf('criteriaType=%s is not supported', $criteriaType)); + } // Convert dashes to underscore $criteriaKey = StringUtils::convertDashesToUnderscores($criteriaKey); - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($criteriaType) . '(' . $this->__toString() . ')-CRITERIA: criteriaKey=' . $criteriaKey); - // Append it + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-CRITERIA: Invoking this->appendStringToGenericArrayElement(criteria,%s,entries,%s,criteriaValue[]=%s) ...', strtoupper($criteriaType), $criteriaType, $criteriaKey, gettype($criteriaValue))); $this->appendStringToGenericArrayElement('criteria', $criteriaType, 'entries', $criteriaKey, $criteriaValue); + + // Trace message + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-CRITERIA: EXIT!', strtoupper($criteriaType))); } /** @@ -206,22 +311,38 @@ abstract class BaseCriteria extends BaseFrameworkSystem implements Criteria { * @param $criteriaValue Criteria value * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude' * @return void + * @throws InvalidArgumentException If a parameter is not valid + * @throws UnexpectedValueException If a parameter contains an unexpected/unsupported value */ - public final function setCriteria (string $criteriaKey, $criteriaValue, string $criteriaType = 'default') { - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($criteriaType) . '-CRITERIA: criteriaKey=' . $criteriaKey . ',criteriaValue=' . $criteriaValue . ',criteriaType=' . $criteriaType . ' - CALLED!'); - - // Make sure no 'my-' or 'my_' passes this point - assert((strpos($criteriaKey, 'my-') === false) && (strpos($criteriaKey, 'my_') === false) && (!is_bool($criteriaValue))); + public final function setCriteria (string $criteriaKey, $criteriaValue, string $criteriaType = Criteria::CRITERIA_TYPE_DEFAULT) { + // Check parameter + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-CRITERIA: criteriaKey=%s,criteriaValue[]=%s$criteriaValue,criteriaType=%s - CALLED!', strtoupper($criteriaType), $criteriaKey, gettype($criteriaValue), $criteriaType)); + if (empty($criteriaKey)) { + // Throw IAE + throw new InvalidArgumentException('Parameter "criteriaKey" is empty'); + } elseif ((strpos($criteriaKey, 'my-') !== false) || (strpos($criteriaKey, 'my_') !== false)) { + // Throw it again + throw new InvalidArgumentException(sprintf('criteriaKey=%s has illegal prefix "my"', $criteriaKey)); + } elseif (is_array($criteriaValue) || is_bool($criteriaValue) || is_object($criteriaValue) || is_resource($criteriaValue)) { + // Throw it again + throw new InvalidArgumentException(sprintf('value[]=%s is not supported', gettype($criteriaValue))); + } elseif (empty($criteriaType)) { + // Throw it again + throw new InvalidArgumentException('Parameter "criteriaType" is empty'); + } elseif (!in_array($criteriaType, self::$CRITERIA_TYPES)) { + // Throw it again + throw new UnexpectedValueException(sprintf('criteriaType=%s is not supported', $criteriaType)); + } // Convert dashes to underscore $criteriaKey = StringUtils::convertDashesToUnderscores($criteriaKey); - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($criteriaType) . '(' . $this->__toString() . ')-CRITERIA: criteriaKey=' . $criteriaKey); - // Set it + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-CRITERIA: Invoking this->setStringGenericArrayElement(criteria,%s,entries,%s,criteriaValue[]=%s) ...', strtoupper($criteriaType), $criteriaKey, gettype($criteriaValue))); $this->setStringGenericArrayElement('criteria', $criteriaType, 'entries', $criteriaKey, $criteriaValue); + + // Trace message + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-CRITERIA: EXIT!'); } /** @@ -231,14 +352,28 @@ abstract class BaseCriteria extends BaseFrameworkSystem implements Criteria { * @param $criteriaKey Criteria key * @param $criteriaValue Criteria value * @return void + * @throws InvalidArgumentException If a parameter is not valid + * @throws UnexpectedValueException If $criteriaValue has an unexpected type */ public final function addChoiceCriteria (string $criteriaKey, $criteriaValue) { - // Make sure no 'my-' or 'my_' passes this point - assert((strpos($criteriaKey, 'my-') === false) && (strpos($criteriaKey, 'my_') === false) && (!is_bool($criteriaValue))); + // Check parameter + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-CRITERIA: criteriaKey=%s,criteriaValue[]=%s - CALLED!', $criteriaKey, gettype($criteriaValue))); + if (empty($criteriaKey)) { + // Throw IAE + throw new InvalidArgumentException('Parameter "criteriaKey" is empty'); + } elseif ((strpos($criteriaKey, 'my-') !== false) || (strpos($criteriaKey, 'my_') !== false)) { + // Throw it again + throw new InvalidArgumentException(sprintf('criteriaKey=%s has illegal prefix "my"', $criteriaKey)); + } elseif (is_array($criteriaValue) || is_bool($criteriaValue) || is_object($criteriaValue) || is_resource($criteriaValue)) { + // Throw UAE + throw new UnexpectedValueException(sprintf('criteriaValue[]=%s is not accepted', gettype($criteriaValue))); + } // Add it - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($criteriaType) . '(' . $this->__toString() . ')-CRITERIA: criteriaKey=' . $criteriaKey . ',criteriaValue=' . $criteriaValue); - $this->pushValueToGenericArrayElement('criteria', 'choice', 'entries', StringUtils::convertDashesToUnderscores($criteriaKey), (string) $criteriaValue); + $this->pushValueToGenericArrayElement('criteria', Criteria::CRITERIA_TYPE_CHOICE, 'entries', StringUtils::convertDashesToUnderscores($criteriaKey), (string) $criteriaValue); + + // Trace message + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-CRITERIA: EXIT!'); } /** @@ -248,10 +383,28 @@ abstract class BaseCriteria extends BaseFrameworkSystem implements Criteria { * @param $criteriaKey Criteria key * @param $criteriaValue Criteria value * @return void + * @throws InvalidArgumentException If a parameter is not valid + * @throws UnexpectedValueException If $criteriaValue has an unexpected type */ public final function addExcludeCriteria (string $criteriaKey, $criteriaValue) { + // Check parameter + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-CRITERIA: criteriaKey=%s,criteriaValue[%s]=%s - CALLED!', $criteriaKey, gettype($criteriaValue), $criteriaValue)); + if (empty($criteriaKey)) { + // Throw IAE + throw new InvalidArgumentException('Parameter "criteriaKey" is empty'); + } elseif ((strpos($criteriaKey, 'my-') !== false) || (strpos($criteriaKey, 'my_') !== false)) { + // Throw it again + throw new InvalidArgumentException(sprintf('criteriaKey=%s has illegal prefix "my"', $criteriaKey)); + } elseif (is_array($criteriaValue) || is_bool($criteriaValue) || is_object($criteriaValue) || is_resource($criteriaValue)) { + // Throw UAE + throw new UnexpectedValueException(sprintf('criteriaValue[]=%s is not accepted', gettype($criteriaValue))); + } + // Add it with generic method - $this->addCriteria($criteriaKey, $criteriaValue, 'exclude'); + $this->addCriteria($criteriaKey, $criteriaValue, Criteria::CRITERIA_TYPE_EXCLUDE); + + // Trace message + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-CRITERIA: EXIT!'); } /** @@ -261,11 +414,38 @@ abstract class BaseCriteria extends BaseFrameworkSystem implements Criteria { * @param $configEntry Configuration entry * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude' * @return void + * @throws InvalidArgumentException If a parameter is not valid + * @throws UnexpectedValueException If a parameter contains an unexpected/unsupported value */ - public final function addConfiguredCriteria (string $criteriaKey, string $configEntry, string $criteriaType = 'default') { + public final function addConfiguredCriteria (string $criteriaKey, string $configEntry, string $criteriaType = Criteria::CRITERIA_TYPE_DEFAULT) { + // Check parameter + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-CRITERIA: criteriaKey=%s,configEntry=%s,criteriaType=%s - CALLED!', strtoupper($criteriaType), $criteriaKey, $configEntry, $criteriaType)); + if (empty($criteriaKey)) { + // Throw IAE + throw new InvalidArgumentException('Parameter "criteriaKey" is empty'); + } elseif ((strpos($criteriaKey, 'my-') !== false) || (strpos($criteriaKey, 'my_') !== false)) { + // Throw it again + throw new InvalidArgumentException(sprintf('criteriaKey=%s has illegal prefix "my"', $criteriaKey)); + } elseif (empty($configEntry)) { + // Throw it again + throw new InvalidArgumentException('Parameter "configEntry" is empty'); + } elseif (empty($criteriaType)) { + // Throw it again + throw new InvalidArgumentException('Parameter "criteriaType" is empty'); + } elseif (!in_array($criteriaType, self::$CRITERIA_TYPES)) { + // Throw it again + throw new UnexpectedValueException(sprintf('criteriaType=%s is not supported', $criteriaType)); + } + // Add the configuration entry as a criteria $value = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry($configEntry); + + // Invoke inner method + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-CRITERIA: Invoking this->addCriteria(%s,value[%s]=%s,%s) ...', strtoupper($criteriaType), $criteriaKey, gettype($value), $value, $criteriaType)); $this->addCriteria($criteriaKey, $value, $criteriaType); + + // Trace message + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-CRITERIA: EXIT!', strtoupper($criteriaType))); } /** @@ -274,27 +454,42 @@ abstract class BaseCriteria extends BaseFrameworkSystem implements Criteria { * @param $criteriaKey The requested criteria key * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude' * @return $value Whether the value of the critera or false + * @throws InvalidArgumentException If a parameter is not valid + * @throws UnexpectedValueException If a parameter contains an unexpected/unsupported value */ - public function getCriteriaElemnent (string $criteriaKey, string $criteriaType = 'default') { - // Make sure no 'my-' or 'my_' passes this point - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($criteriaType) . '-CRITERIA: criteriaKey=' . $criteriaKey . ',criteriaType=' . $criteriaType . ' - CALLED!'); - assert((strpos($criteriaKey, 'my-') === false) && (strpos($criteriaKey, 'my_') === false)); + public function getCriteriaElemnent (string $criteriaKey, string $criteriaType = Criteria::CRITERIA_TYPE_DEFAULT) { + // Check parameter + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-CRITERIA: criteriaKey=%s,configEntry=%s,criteriaType=%s - CALLED!', strtoupper($criteriaType), $criteriaKey, $configEntry, $criteriaType)); + if (empty($criteriaKey)) { + // Throw IAE + throw new InvalidArgumentException('Parameter "criteriaKey" is empty'); + } elseif ((strpos($criteriaKey, 'my-') !== false) || (strpos($criteriaKey, 'my_') !== false)) { + // Throw it again + throw new InvalidArgumentException(sprintf('criteriaKey=%s has illegal prefix "my"', $criteriaKey)); + } elseif (empty($criteriaType)) { + // Throw it again + throw new InvalidArgumentException('Parameter "criteriaType" is empty'); + } elseif (!in_array($criteriaType, self::$CRITERIA_TYPES)) { + // Throw it again + throw new UnexpectedValueException(sprintf('criteriaType=%s is not supported', $criteriaType)); + } // Convert dashes to underscore $criteriaKey = StringUtils::convertDashesToUnderscores($criteriaKey); // Default is not found - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($criteriaType) . '-CRITERIA: criteriaKey=' . $criteriaKey . ',criteria()=' . $this->countGenericArrayGroup('criteria', $criteriaType)); $value = false; // Is the criteria there? + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-CRITERIA: criteriaKey=%s,criteria()=%d', strtoupper($criteriaType), $criteriaKey, $this->countGenericArrayGroup('criteria', $criteriaType))); if ($this->isKeySet($criteriaType, $criteriaKey)) { // Then use it + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-CRITERIA: Invoking this->getGenericArrayElement(criteria,%s,entries,%s) ...', strtoupper($criteriaType), $criteriaType, $criteriaKey)); $value = $this->getGenericArrayElement('criteria', $criteriaType, 'entries', $criteriaKey); } // Return the value - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($criteriaType) . '-CRITERIA: value=' . $value . ' - EXIT!'); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-CRITERIA: value[]=%s - EXIT!', strtoupper($criteriaType), gettype($value))); return $value; } @@ -303,10 +498,21 @@ abstract class BaseCriteria extends BaseFrameworkSystem implements Criteria { * * @param $criteriaKey The requested criteria key * @return $value Whether the value of the critera or false + * @throws InvalidArgumentException If a parameter is not valid */ public function getCriteriaChoiceElemnent (string $criteriaKey) { - // Call inner method - return $this->getCriteriaElemnent($criteriaKey, 'choice'); + // Check parameter + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-CRITERIA: criteriaKey=%s - CALLED!', strtoupper($criteriaType), $criteriaKey)); + if (empty($criteriaKey)) { + // Throw IAE + throw new InvalidArgumentException('Parameter "criteriaKey" is empty'); + } elseif ((strpos($criteriaKey, 'my-') !== false) || (strpos($criteriaKey, 'my_') !== false)) { + // Throw it again + throw new InvalidArgumentException(sprintf('criteriaKey=%s has illegal prefix "my"', $criteriaKey)); + } + + // Invoke inner method + return $this->getCriteriaElemnent($criteriaKey, Criteria::CRITERIA_TYPE_CHOICE); } /** @@ -314,10 +520,21 @@ abstract class BaseCriteria extends BaseFrameworkSystem implements Criteria { * * @param $criteriaKey The requested criteria key * @return $value Whether the value of the critera or false + * @throws InvalidArgumentException If a parameter is not valid */ public function getCriteriaExcludeElemnent (string $criteriaKey) { - // Call inner method - return $this->getCriteriaElemnent($criteriaKey, 'exclude'); + // Check parameter + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-CRITERIA: criteriaKey=%s - CALLED!', strtoupper($criteriaType), $criteriaKey)); + if (empty($criteriaKey)) { + // Throw IAE + throw new InvalidArgumentException('Parameter "criteriaKey" is empty'); + } elseif ((strpos($criteriaKey, 'my-') !== false) || (strpos($criteriaKey, 'my_') !== false)) { + // Throw it again + throw new InvalidArgumentException(sprintf('criteriaKey=%s has illegal prefix "my"', $criteriaKey)); + } + + // Invoke inner method + return $this->getCriteriaElemnent($criteriaKey, Criteria::CRITERIA_TYPE_EXCLUDE); } /** @@ -326,8 +543,23 @@ abstract class BaseCriteria extends BaseFrameworkSystem implements Criteria { * @param $entryArray Array with the entries to find * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude' * @return $matches Whether the entry matches or not + * @throws InvalidArgumentException If a parameter is not valid + * @throws UnexpectedValueException If a parameter contains an unexpected/unsupported value */ - public function ifEntryMatches (array $entryArray, string $criteriaType = 'default') { + public function ifEntryMatches (array $entryArray, string $criteriaType = Criteria::CRITERIA_TYPE_DEFAULT) { + // Check parameters + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-CRITERIA: entryArray()=%d,criteriaType=%s - CALLED!', strtoupper($criteriaType), count($entryArray), $criteriaType)); + if (count($entryArray) == 0) { + // Throw IAE + throw new InvalidArgumentException('entryArray cannot be an empty array'); + } elseif (empty($criteriaType)) { + // Throw it again + throw new InvalidArgumentException('Parameter "criteriaType" is empty'); + } elseif (!in_array($criteriaType, self::$CRITERIA_TYPES)) { + // Throw it again + throw new UnexpectedValueException(sprintf('criteriaType=%s is not supported', $criteriaType)); + } + // First nothing matches and nothing is counted $matches = false; $counted = 0; @@ -335,7 +567,11 @@ abstract class BaseCriteria extends BaseFrameworkSystem implements Criteria { // Walk through all entries foreach ($entryArray as $key => $entry) { // Make sure no 'my-' or 'my_' passes this point - assert((strpos($key, 'my-') === false) && (strpos($key, 'my_') === false)); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-CRITERIA: key=%s,entry[%s]=%s', strtoupper($criteriaType), $key, gettype($entry), $entry)); + if ((strpos($key, 'my-') !== false) || (strpos($key, 'my_') !== false)) { + // Throw it again + throw new InvalidArgumentException(sprintf('key=%s has illegal prefix "my"', $key)); + } // Convert dashes to underscore $key = StringUtils::convertDashesToUnderscores($key); @@ -343,7 +579,11 @@ abstract class BaseCriteria extends BaseFrameworkSystem implements Criteria { // Then walk through all search criteria foreach ($this->getGenericArrayKey('criteria', $criteriaType, 'entries') as $criteriaKey => $criteriaValue) { // Make sure no 'my-' or 'my_' passes this point - assert((strpos($criteriaKey, 'my-') === false) && (strpos($criteriaKey, 'my_') === false) && (!is_bool($criteriaValue))); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-CRITERIA: criteriaKey=%s,criteriaValue[%s]=%s', strtoupper($criteriaType), $criteriaKey, gettype($criteriaValue), $criteriaValue)); + if ((strpos($criteriaKey, 'my-') !== false) || (strpos($criteriaKey, 'my_') !== false)) { + // Throw it again + throw new UnexpectedValueException(sprintf('criteriaKey=%s has illegal prefix "my"', $criteriaKey)); + } // Convert dashes to underscore $criteriaKey = StringUtils::convertDashesToUnderscores($criteriaKey); @@ -351,15 +591,18 @@ abstract class BaseCriteria extends BaseFrameworkSystem implements Criteria { // Is the element found and does it match? if (($key == $criteriaKey) && ($criteriaValue == $entry)) { // Then count this one up + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-CRITERIA: MATCHES!', strtoupper($criteriaType))); $counted++; } } } // Now check if expected criteria counts match + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-CRITERIA: counted=%d', strtoupper($criteriaType), $counted)); $matches = ($counted == $this->countGenericArrayGroup('criteria', $criteriaType)); // Return the result + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-CRITERIA: matches=%d - EXIT!', strtoupper($criteriaType), intval($matches))); return $matches; } @@ -368,10 +611,19 @@ abstract class BaseCriteria extends BaseFrameworkSystem implements Criteria { * * @param $entryArray Array with the entries to find * @return $matches Whether the entry matches or not + * @throws InvalidArgumentException If a parameter is not valid */ public function ifChoiceMatches (array $entryArray) { - // Call inner method - return $this->ifEntryMatches($entryArray, 'choice'); + // Check parameter + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-CRITERIA: entryArray()=%d - CALLED!', count($entryArray))); + if (count($entryArray) == 0) { + // Throw IAE + throw new InvalidArgumentException('entryArray cannot be an empty array'); + } + + // Invoke inner method + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-CRITERIA: Invoking this->ifEntryMatches(%d,%s) ...', count($entryArray), Criteria::CRITERIA_TYPE_CHOICE)); + return $this->ifEntryMatches($entryArray, Criteria::CRITERIA_TYPE_CHOICE); } /** @@ -379,10 +631,19 @@ abstract class BaseCriteria extends BaseFrameworkSystem implements Criteria { * * @param $entryArray Array with the entries to find * @return $matches Whether the entry matches or not + * @throws InvalidArgumentException If a parameter is not valid */ public function ifExcludeMatches (array $entryArray) { - // Call inner method - return $this->ifEntryMatches($entryArray, 'exclude'); + // Check parameter + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-CRITERIA: entryArray()=%d - CALLED!', count($entryArray))); + if (count($entryArray) == 0) { + // Throw IAE + throw new InvalidArgumentException('entryArray cannot be an empty array'); + } + + // Invoke inner method + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-CRITERIA: Invoking this->ifEntryMatches(%d,%s) ...', count($entryArray), Criteria::CRITERIA_TYPE_EXCLUDE)); + return $this->ifEntryMatches($entryArray, Criteria::CRITERIA_TYPE_EXCLUDE); } /** @@ -391,13 +652,23 @@ abstract class BaseCriteria extends BaseFrameworkSystem implements Criteria { * @param $onlyKeys Only use these keys for a cache key * @param $criteriaType Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude' * @return $cacheKey The key suitable for the cache system + * @throws InvalidArgumentException If a parameter is not valid + * @throws UnexpectedValueException If a parameter contains an unexpected/unsupported value + * @throws BadMethodCallException If this method is invoked before $criteriaType has been initialized */ - public function getCacheKey (array $onlyKeys = [], string $criteriaType = 'default') { - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput($this->__toString() . ': criteriaType=' . $criteriaType . ',count()=' . $this->countGenericArray('criteria'))); - - // Make sure the criteria is there - assert($this->isValidGenericArrayGroup('criteria', $criteriaType)); + public function getCacheKey (array $onlyKeys = [], string $criteriaType = Criteria::CRITERIA_TYPE_DEFAULT) { + // Check parameters + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-CRITERIA: onlyKeys()=%d,criteriaType=%s - CALLED!', strtoupper($criteriaType), count($onlyKeys), $criteriaType)); + if (empty($criteriaType)) { + // Throw it again + throw new InvalidArgumentException('Parameter "criteriaType" is empty'); + } elseif (!in_array($criteriaType, self::$CRITERIA_TYPES)) { + // Throw it again + throw new UnexpectedValueException(sprintf('criteriaType=%s is not supported', $criteriaType)); + } elseif (!$this->isValidGenericArrayGroup('criteria', $criteriaType)) { + // Not intialized yet + throw new BadMethodCallException(sprintf('Method cannot be invoked before criteriaType=%s is initialized!', $criteriaType)); + } // Initialize the key $cacheKey = ''; @@ -405,17 +676,23 @@ abstract class BaseCriteria extends BaseFrameworkSystem implements Criteria { // Now walk through all criterias foreach ($this->getGenericArrayKey('criteria', $criteriaType, 'entries') as $criteriaKey => $criteriaValue) { // Make sure no 'my-' or 'my_' passes this point - assert((strpos($criteriaKey, 'my-') === false) && (strpos($criteriaKey, 'my_') === false) && (!is_bool($criteriaValue))); - - // $criteriaValue cannot be an array - assert(!is_array($criteriaValue)); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-CRITERIA: criteriaKey=%s,criteriaValue[%s]=%s', strtoupper($criteriaType), $criteriaKey, gettype($criteriaValue), $criteriaValue)); + if ((strpos($criteriaKey, 'my-') !== false) || (strpos($criteriaKey, 'my_') !== false)) { + // Throw UAE + throw new UnexpectedValueException(sprintf('criteriaKey=%s has illegal prefix "my"', $criteriaKey)); + } elseif (is_array($criteriaValue) || is_bool($criteriaValue) || is_object($criteriaValue) || is_resource($criteriaValue)) { + // Throw it again + throw new UnexpectedValueException(sprintf('criteriaValue[]=%s is not supported', gettype($criteriaValue))); + } // Convert dashes to underscore $criteriaKey = StringUtils::convertDashesToUnderscores($criteriaKey); // Is the value in array or is $onlyKeys empty? + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-CRITERIA: criteriaKey=%s', strtoupper($criteriaType), $criteriaKey)); if ((isset($onlyKeys[$criteriaKey])) || (count($onlyKeys) == 0)) { // Add the value URL encoded to avoid any trouble with special characters + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-CRITERIA: Adding criteriaKey=%s,criteriaValue[%s]=%s to cache key ...', strtoupper($criteriaType), $criteriaKey, gettype($criteriaValue), $criteriaValue)); $cacheKey .= sprintf('%s=%s;', $criteriaKey, urlencode($criteriaValue) @@ -427,10 +704,12 @@ abstract class BaseCriteria extends BaseFrameworkSystem implements Criteria { $cacheKey = substr($cacheKey, 0, -1); // Is the instance SearchCriteria? + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-CRITERIA: cacheKey=%s,this=%s', strtoupper($criteriaType), $cacheKey, $this->__toString())); if ($this instanceof SearchCriteria) { // Check if 'limit' and 'skip' are in if (((isset($onlyKeys['limit'])) && (isset($onlyKeys['skip']))) || (count($onlyKeys) == 0)) { // Add limit and skip values + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-CRITERIA: Adding this->limit=%d,this->skip=%d to cache key ...', strtoupper($criteriaType), $this->getLimit(), $this->getSkip())); $cacheKey .= sprintf(';%%limit%%=%s;%%skip%%=%s', $this->getLimit(), $this->getSkip() @@ -439,6 +718,7 @@ abstract class BaseCriteria extends BaseFrameworkSystem implements Criteria { } // Return the cache key + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-CRITERIA: cacheKey=%s - EXIT!', strtoupper($criteriaType), $cacheKey)); return $cacheKey; } @@ -449,8 +729,12 @@ abstract class BaseCriteria extends BaseFrameworkSystem implements Criteria { * @return $cacheKey The key suitable for the cache system */ public function getCacheKeyChoice (array $onlyKeys = []) { - // Call inner method - return $this->getCacheKey($onlyKeys, 'choice'); + // Trace message + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-CRITERIA: onlyKeys()=%d - CALLED!', count($onlyKeys))); + + // Invoke inner method + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-CRITERIA: Invoking this->getCacheKey(onlyKeys()=%d,%s) ...', count($onlyKeys),Criteria::CRITERIA_TYPE_CHOICE)); + return $this->getCacheKey($onlyKeys, Criteria::CRITERIA_TYPE_CHOICE); } /** @@ -460,8 +744,12 @@ abstract class BaseCriteria extends BaseFrameworkSystem implements Criteria { * @return $cacheKey The key suitable for the cache system */ public function getCacheKeyExclude (array $onlyKeys = []) { - // Call inner method - return $this->getCacheKey($onlyKeys, 'exclude'); + // Trace message + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-CRITERIA: onlyKeys()=%d - CALLED!', count($onlyKeys))); + + // Invoke inner method + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-CRITERIA: Invoking this->getCacheKey(onlyKeys()=%d,%s) ...', count($onlyKeys),Criteria::CRITERIA_TYPE_EXCLUDE)); + return $this->getCacheKey($onlyKeys, Criteria::CRITERIA_TYPE_EXCLUDE); } /** @@ -471,7 +759,9 @@ abstract class BaseCriteria extends BaseFrameworkSystem implements Criteria { * @return $count Count of all criteria entries */ public final function countChoice () { - return $this->count('choice'); + // Trace message + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-CRITERIA: CALLED!'); + return $this->count(Criteria::CRITERIA_TYPE_CHOICE); } /** @@ -481,7 +771,9 @@ abstract class BaseCriteria extends BaseFrameworkSystem implements Criteria { * @return $count Count of all criteria entries */ public final function countExclude () { - return $this->count('exclude'); + // Trace message + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-CRITERIA: CALLED!'); + return $this->count(Criteria::CRITERIA_TYPE_EXCLUDE); } } diff --git a/framework/main/interfaces/criteria/class_Criteria.php b/framework/main/interfaces/criteria/class_Criteria.php index 5ca1498e..989c872a 100644 --- a/framework/main/interfaces/criteria/class_Criteria.php +++ b/framework/main/interfaces/criteria/class_Criteria.php @@ -28,6 +28,11 @@ use Org\Mxchange\CoreFramework\Generic\FrameworkInterface; * along with this program. If not, see . */ interface Criteria extends FrameworkInterface { + // Criteria types + const CRITERIA_TYPE_DEFAULT = 'default'; + const CRITERIA_TYPE_CHOICE = 'choice'; + const CRITERIA_TYPE_EXCLUDE = 'exclude'; + /** * Setter for frontend class name *