*/
abstract class BSApi
{
- /**
- * The callback function for the error handler
- * @var string
- */
- public static $errorHandler;
-
/**
* Fields: used for verification and sanitization
* NAME => array(TYPE, REQUIRED, VERIFY METHOD (:self for self-named method), RELATION => array(FILE, CLASS IN FILE, ALTERNATE FIELD NAME))
public $values = array();
/**
- * Fields that were manually set with set(), not by using setExisting()
+ * Fields that were set by the client
* @var array
*/
private $setfields = array();
/**
* Error queue that builds up errors
- * @var array
+ * @var ApiException
*/
- private $errors = array();
+ private $exception = null;
// ###################################################################
/**
/**
* Adds an error into the error queue that is then hit
*
- * @param string Error message
+ * @param Exception Error message
*/
- protected function _error($message)
+ protected function _error(Exception $e)
{
- $this->errors[] = $message;
+ if ($this->exception == null)
+ {
+ $this->exception = new ApiException();
+ }
+ $this->exception->addException($e);
}
// ###################################################################
/**
- * This runs through all the errors in the error queue and processes
- * them all at once. Error builders then get a list of all the errors,
- * and die-by-hit callbacks stop at the first one
- *
- * @param string A string param
- *
- * @return integer Return value
+ * This simply throws the ApiException if it exists, which inside holds
+ * all of the individual and specific errors
*/
private function _processErrorQueue()
{
- // we want to explicitly specify silence
- if (self::$errorHandler == 'silent')
- {
- return;
- }
-
- if (!is_callable(self::$errorHandler))
- {
- throw new Exception('No BSApi::$errorHandler handler has been set');
- }
-
- foreach ($this->errors AS $e)
+ if ($this->exception)
{
- call_user_func(BSApi::$errorHandler, $e);
+ throw $this->exception;
}
}
// ###################################################################
/**
- * Returns the error list. This is because we don't want people mucking
- * with the error system. It will return an empty array if there are
- * no errors.
+ * Returns the list of exceptions contained in the ApiException
*
* @return array Array of errors
*/
- public function checkErrors()
+ public function isValid()
{
- if (sizeof($this->errors) < 1)
+ if ($this->exception == null)
{
return array();
}
- return $this->errors;
+ return $this->exception->getExceptions();
}
// ###################################################################
if (!isset($this->fields["$field"]))
{
throw new Exception('Field "' . $field . '" is not valid');
- return;
}
$this->values["$field"] = ($doclean ? BSApp::GetType('Input')->clean($value, $this->fields["$field"][F_TYPE]) : $value);
{
if ($verify === false)
{
- $this->_error(sprintf(_('Validation of "%1$s" failed'), $field));
+ $this->_error(new Exception(sprintf(_('Validation of "%1$s" failed'), $field)));
}
else
{
if (!$this->values["$field"])
{
throw new Exception('The specified field "' . $field . '" for the condition could not be found as it is not set');
- continue;
}
$condbits[] = "$field = " . $this->_prepareFieldForSql($field);
if (!$this->values["$name"])
{
throw new Exception('Cannot determine condition from the REQ_AUTO field because it is not set');
- continue;
}
$this->condition = "$name = " . $this->_prepareFieldForSql($name);
}
}
- // ###################################################################
- /**
- * Sets existing data into $values where it's not already present
- */
- public function setExisting()
- {
- static $run;
- if ($run)
- {
- return;
- }
-
- $this->fetch();
-
- foreach ($this->objdata AS $field => $value)
- {
- if (!isset($this->values["$field"]))
- {
- $this->values["$field"] = $value;
- }
- }
-
- $run = true;
- }
-
// ###################################################################
/**
* Fetches a record based on the condition
}
// reset the error queue due to any validation errors caused by fetchable fields
- $this->errors = array();
+ $this->errors = null;
$this->_runActionMethod('pre_fetch', $doPre);
{
if (!isset($this->values["$name"]))
{
- $this->_error(sprintf(_('The required field "%1$s" was not set'), $name));
+ $this->_error(new Exception(sprintf(_('The required field "%1$s" was not set'), $name), $name));
}
}
else if ($options[F_REQ] == REQ_SET)
{
if ($this->values["$field"] == 0)
{
- return sprintf(_('The field "%1$s" cannot be zero'), $field);
+ return new Exception(sprintf(_('The field "%1$s" cannot be zero'), $field), $field);
}
return true;
{
if (empty($this->values["$field"]))
{
- return sprintf(_('The field "%1$s" cannot be empty'), $field);
+ return new Exception(sprintf(_('The field "%1$s" cannot be empty'), $field), $field);
}
return true;
$res = BSApp::GetType('Db')->queryFirst("SELECT $field FROM {$this->prefix}{$this->table} WHERE $field = " . $this->_prepareFieldForSql($field) . (empty($this->condition) ? "" : " AND !({$this->condition})"));
if ($res)
{
- return sprintf(_('The "%1$s" field must contain a unique value'), $field);
+ return new Exception(sprintf(_('The "%1$s" field must contain a unique value'), $field), $field);
}
return true;
}
}
+/**
+ * API Exception
+ *
+ * This class is an exception container that can be used to store a series
+ * of exceptions that can be thrown as one
+ *
+ * @author rsesek
+ * @copyright Copyright (c)2002 - 2007, Blue Static
+ * @package ISSO
+ *
+ */
+class ApiException extends Exception
+{
+ /**
+ * Array of exceptions
+ * @var array
+ */
+ private $exceptions = array();
+
+ // ###################################################################
+ /**
+ * Constructor
+ */
+ public function __construct()
+ {
+ parent::__construct(_('An error occurred while processing the API data.'));
+ }
+
+ // ###################################################################
+ /**
+ * Adds an exception to the collection
+ *
+ * @param Exception $e
+ */
+ public function addException(Exception $e)
+ {
+ $this->exceptions[] = $e;
+ }
+
+ // ###################################################################
+ /**
+ * Returns an array of all the exceptions in the collection
+ *
+ * @return array
+ */
+ public function getExceptions()
+ {
+ return $this->exceptions;
+ }
+}
+
?>
\ No newline at end of file