Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions src/Error/ErrorMessages.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
namespace Contentstack\Error;

/**
* Class ErrorMessages
* Contains all error messages used across the SDK
*
* @category PHP
* @package Contentstack
* @author Contentstack <[email protected]>
* @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);
}
}
15 changes: 8 additions & 7 deletions src/Stack/BaseQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

namespace Contentstack\Stack;
use Contentstack\Support\Utility;
use Contentstack\Error\ErrorMessages;

require_once __DIR__ . "/../Support/helper.php";

Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -710,7 +711,7 @@ public function tags($tags = array())
);
return $this->queryObject;
}
throw contentstackCreateError('tags must be an array');
throw contentstackCreateError(ErrorMessages::TAGS_ARRAY);
}

/**
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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);
}

/**
Expand Down
31 changes: 16 additions & 15 deletions src/Support/helper.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use Contentstack\Support\Utility;
use Contentstack\Error\ErrorMessages;

if(!function_exists('contentstackGetFunctionName')) {
/*
Expand Down Expand Up @@ -40,7 +41,7 @@ function contentstackCreateError($msg = '') {
* */
function contentstackSearch($operator = '', $query = array(), $value = '') {
if(!(!Utility::isEmpty($value) && is_string($value)))
throw contentstackCreateError('Invalid input for "'.contentstackGetFunctionName().'". String value expected.');
throw contentstackCreateError(ErrorMessages::formatMessage(ErrorMessages::INVALID_STRING_INPUT, contentstackGetFunctionName()));
$query[$operator] = $value;
return $query;
}
Expand All @@ -57,7 +58,7 @@ function contentstackSearch($operator = '', $query = array(), $value = '') {
* */
function contentstackReferences($operator = '', $query = array(), $value = array()) {
if(!is_array($value))
throw contentstackCreateError('Invalid input for includeReferences. Array expected.');
throw contentstackCreateError(ErrorMessages::INVALID_INCLUDE_REFERENCES);
$query[$operator] = $value;
return $query;
}
Expand All @@ -77,7 +78,7 @@ function contentstackProjection($operator = '', $query = array(), $level = 'BASE
$value = $level;
$level = 'BASE';
}
if(!(!Utility::isEmpty($level) && is_string($level) && is_array($value))) throw contentstackCreateError('Invalid Input');
if(!(!Utility::isEmpty($level) && is_string($level) && is_array($value))) throw contentstackCreateError(ErrorMessages::INVALID_INPUT_TYPE);
if(!Utility::isKeySet($query, $operator)) $query[$operator] = array();
if(!Utility::isKeySet($query[$operator], $level)) $query[$operator][$level] = array();
$query[$operator][$level] = array_merge($query[$operator][$level], $value);
Expand All @@ -100,16 +101,16 @@ function contentstackProjection($operator = '', $query = array(), $level = 'BASE
function contentstackRegexp($operator = '', $query = array(), $values = array()) {
if(count($values) === 2 || count($values) === 3) {
if(Utility::isEmpty($values[0]) && Utility::isEmpty($values[1]) && is_string($values[0]) && is_string($values[1]))
throw contentstackCreateError('Invalid input for regex.Key must be string and value must be valid RegularExpression');
throw contentstackCreateError(ErrorMessages::INVALID_REGEX_KEY_VALUE);
if(isset($values[2]) && !(is_string($values[2]) && strlen($values[2]) > 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);
}
}
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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)
Expand All @@ -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;
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down