diff --git a/src/Error/ErrorMessages.php b/src/Error/ErrorMessages.php new file mode 100644 index 00000000..27f5b71d --- /dev/null +++ b/src/Error/ErrorMessages.php @@ -0,0 +1,50 @@ + + * @license https://github.com/contentstack/contentstack-php/blob/master/LICENSE.txt MIT Licence + * @link https://www.contentstack.com/docs/platforms/php/ + */ +class ErrorMessages +{ + // BaseQuery.php error messages + const FIELD_UIDS_ARRAY = 'Field UIDs must be an array. Convert the value to an array and try again.'; + const TAGS_ARRAY = 'Tags must be an array. Convert the value to an array and try again.'; + const VALUE_ARRAY = 'Value must be an array. Convert the value to an array and try again.'; + const INVALID_QUERY = 'Invalid query. Update the query and try again.'; + + // helper.php error messages + const INVALID_STRING_INPUT = 'Invalid input for "%s". Use a string value and try again.'; + const INVALID_INCLUDE_REFERENCES = 'Invalid input for includeReferences. Use an array and try again.'; + const INVALID_INPUT_TYPE = 'Invalid input. Use a string or an array and try again.'; + const INVALID_REGEX_KEY_VALUE = 'Invalid input for regex. Use a string for the key and a valid regular expression for the value.'; + const INVALID_REGEX_OPTIONS = 'Invalid regex options. Provide valid options and try again.'; + const INVALID_REGEX_ARGS = 'Invalid input for regex. Provide 2 or 3 arguments and try again.'; + const INVALID_TAGS_INPUT = 'Invalid input for tags. Use a valid array of tags and try again.'; + const INVALID_KEY_VALUE = 'Invalid input for "%s". Use a string for the key and a valid value, then try again.'; + const INVALID_QUERY_INPUT = 'Invalid input for "%s". Provide at least one query and try again.'; + const INVALID_QUERY_OBJECTS = 'Invalid input. Query objects are expected as arguments. Update the input and try again.'; + const INVALID_KEY_ARRAY_VALUE = 'Invalid input for "%s". Use a string for the key and an array for the value, then try again.'; + const INVALID_NUMERIC_INPUT = 'Invalid input for "%s". Use a numeric value and try again.'; + const INVALID_FIELD_INPUT = 'Invalid input for "%s". Use a valid field from the entry and try again.'; + const INVALID_FIELD_UID = 'Invalid input for "%s". Use a valid string field UID and try again.'; + + /** + * Format error message with function name + * + * @param string $message The message template containing %s placeholder + * @param string $functionName The function name to insert + * + * @return string Formatted error message + */ + public static function formatMessage($message, $functionName = '') + { + return sprintf($message, $functionName); + } +} diff --git a/src/Stack/BaseQuery.php b/src/Stack/BaseQuery.php index b63d5b66..039eeaa0 100755 --- a/src/Stack/BaseQuery.php +++ b/src/Stack/BaseQuery.php @@ -16,6 +16,7 @@ namespace Contentstack\Stack; use Contentstack\Support\Utility; +use Contentstack\Error\ErrorMessages; require_once __DIR__ . "/../Support/helper.php"; @@ -110,7 +111,7 @@ public function except($level = 'BASE', $field_uids = array()) ); return $this->queryObject; } - throw contentstackCreateError('field_uids must be an array'); + throw contentstackCreateError(ErrorMessages::FIELD_UIDS_ARRAY); } /** @@ -142,7 +143,7 @@ public function only($level = 'BASE', $field_uids = array()) ); return $this->queryObject; } - throw contentstackCreateError('field_uids must be an array'); + throw contentstackCreateError(ErrorMessages::FIELD_UIDS_ARRAY); } /** @@ -175,7 +176,7 @@ public function includeReference($field_uids = array()) ); return $this->queryObject; } - throw contentstackCreateError('field_uids must be an array'); + throw contentstackCreateError(ErrorMessages::FIELD_UIDS_ARRAY); } /** @@ -710,7 +711,7 @@ public function tags($tags = array()) ); return $this->queryObject; } - throw contentstackCreateError('tags must be an array'); + throw contentstackCreateError(ErrorMessages::TAGS_ARRAY); } /** @@ -773,7 +774,7 @@ public function containedIn($field = '', $value = array()) ); return $this->queryObject; } - throw contentstackCreateError('value must be an array'); + throw contentstackCreateError(ErrorMessages::VALUE_ARRAY); } /** @@ -809,7 +810,7 @@ public function notContainedIn($field = '', $value = array()) ); return $this->queryObject; } - throw contentstackCreateError('value must be an array'); + throw contentstackCreateError(ErrorMessages::VALUE_ARRAY); } /** @@ -990,7 +991,7 @@ public function addQuery($_query = array()) $this->subQuery = $_query; return $this->queryObject; } - throw contentstackCreateError("Provide valid query"); + throw contentstackCreateError(ErrorMessages::INVALID_QUERY); } /** diff --git a/src/Support/helper.php b/src/Support/helper.php index 04a08384..6b32e121 100755 --- a/src/Support/helper.php +++ b/src/Support/helper.php @@ -1,6 +1,7 @@ 0)) { - throw contentstackCreateError('Invalid options for regex. Please provide the valid options'); + throw contentstackCreateError(ErrorMessages::INVALID_REGEX_OPTIONS); } $query[$values[0]] = array($operator => $values[1]); if(isset($values[2])) $query[$values[0]]['$options'] = $values[2]; return $query; } else { - throw contentstackCreateError('Invalid input for regex. At least 2 or maximum 3 arguments are required.'); + throw contentstackCreateError(ErrorMessages::INVALID_REGEX_ARGS); } } } @@ -126,7 +127,7 @@ function contentstackRegexp($operator = '', $query = array(), $values = array()) * */ function contentstackTags($operator = '', $query = array(), $value = '') { if(!(is_array($value) && count($value) > 0)) - throw contentstackCreateError('Invalid input for tags.Value must be valid array of tags'); + throw contentstackCreateError(ErrorMessages::INVALID_TAGS_INPUT); $query[$operator] = $value; return $query; } @@ -146,7 +147,7 @@ function contentstackTags($operator = '', $query = array(), $value = '') { * */ function contentstackComparision($operator = '', $query = array(), $key = '', $value = '') { if(!(!Utility::isEmpty($key) && is_string($key) && !Utility::isEmpty($value))) - throw contentstackCreateError('Invalid input for "'.contentstackGetFunctionName().'". Key must be string and value should be valid not empty.'); + throw contentstackCreateError(ErrorMessages::formatMessage(ErrorMessages::INVALID_KEY_VALUE, contentstackGetFunctionName())); $query[$key] = array($operator => $value); return $query; } @@ -165,7 +166,7 @@ function contentstackComparision($operator = '', $query = array(), $key = '', $v * */ function contentstackLogical($operator = '', $query = array(), $value = array()) { if(!(is_array($value) && count($value) > 0)) - throw contentstackCreateError('Invalid input for "'.contentstackGetFunctionName().'". At least one Query or array object is expected'); + throw contentstackCreateError(ErrorMessages::formatMessage(ErrorMessages::INVALID_QUERY_INPUT, contentstackGetFunctionName())); foreach($value as $key => $_qry) { if(!Utility::isKeySet($query, $operator)) $query[$operator] = array(); if($_qry instanceof \Contentstack\Stack\BaseQuery) @@ -174,7 +175,7 @@ function contentstackLogical($operator = '', $query = array(), $value = array()) array_push($query[$operator], $_qry); else { unset($query[$operator]); - throw contentstackCreateError('Query objects are expected as arguments'); + throw contentstackCreateError(ErrorMessages::INVALID_QUERY_OBJECTS); } } return $query; @@ -194,7 +195,7 @@ function contentstackLogical($operator = '', $query = array(), $value = array()) * */ function contentstackContains($operator = '', $query = array(), $key = '', $value = array()) { if (!(!Utility::isEmpty($key) && is_string($key) && is_array($value))) - throw contentstackCreateError('Invalid input for "'.contentstackGetFunctionName().'". Key should be string and value must be array.'); + throw contentstackCreateError(ErrorMessages::formatMessage(ErrorMessages::INVALID_KEY_ARRAY_VALUE, contentstackGetFunctionName())); $query[$key] = array($operator => $value); return $query; } @@ -212,7 +213,7 @@ function contentstackContains($operator = '', $query = array(), $key = '', $valu * */ function contentstackPagination($operator = '', $query = array(), $value = '') { if (!(!Utility::isEmpty($value) && is_numeric($value))) - throw contentstackCreateError('Invalid input for "'.contentstackGetFunctionName().'", it should be Numeric.'); + throw contentstackCreateError(ErrorMessages::formatMessage(ErrorMessages::INVALID_NUMERIC_INPUT, contentstackGetFunctionName())); $query[$operator] = $value; return $query; } @@ -231,7 +232,7 @@ function contentstackPagination($operator = '', $query = array(), $value = '') { function contentstackLanguage($operator = '', $query = array(), $value = '') { if (!(!Utility::isEmpty($value) && is_string($value))) - throw contentstackCreateError('Invalid input for "'.contentstackGetFunctionName().'", it should be String.'); + throw contentstackCreateError(ErrorMessages::formatMessage(ErrorMessages::INVALID_STRING_INPUT, contentstackGetFunctionName())); $query[$operator] = $value; return $query; } @@ -249,7 +250,7 @@ function contentstackLanguage($operator = '', $query = array(), $value = '') { * */ function contentstackSorting($operator = '', $query = array(), $key = '') { if (!(!Utility::isEmpty($key) && is_string($key))) - throw contentstackCreateError('Invalid input for "'.contentstackGetFunctionName().'". Value should be valid field in entry'); + throw contentstackCreateError(ErrorMessages::formatMessage(ErrorMessages::INVALID_FIELD_INPUT, contentstackGetFunctionName())); $query[$operator] = $key; return $query; } @@ -300,7 +301,7 @@ function contentstackAddParam($key = '', $query = array(), $value = '') { * */ function contentstackExistence($operator = '', $query = array(), $key = '', $value = false) { if (!(!Utility::isEmpty($key) && is_string($key))) - throw contentstackCreateError('Invalid input for "'.contentstackGetFunctionName().'". Key should be valid String field uid'); + throw contentstackCreateError(ErrorMessages::formatMessage(ErrorMessages::INVALID_FIELD_UID, contentstackGetFunctionName())); $query[$key] = array($operator => $value); return $query; }