ORM Validation exceptions.
Class declared in MODPATH/orm/classes/kohana/orm/validation/exception.php on line 10.
string
$error_viewerror rendering view
string(12) "kohana/error"
array
$php_errorsPHP error code => human readable name
array(8) ( 1 => string(11) "Fatal Error" 256 => string(10) "User Error" 4 => string(11) "Parse Error" 2 => string(7) "Warning" 512 => string(12) "User Warning" 2048 => string(6) "Strict" 8 => string(6) "Notice" 4096 => string(17) "Recoverable Error" )
string
$_object_nameThe _object_name property of the main ORM model this exception was created for
array
$_objectsArray of validation objects
$code
$file
$line
$messageConstructs a new exception for the specified model
string
$object_name
required - The _object_name of the model this exception is forValidation
$object
required - The Validation object of the modelstring
$message
= string(24) "Failed to validate array" - The error messagearray
$values
= NULL - The array of values for the error messageinteger
$code
= integer 0 - The error code for the exceptionvoid
public function __construct($object_name, Validation $object, $message = 'Failed to validate array', array $values = NULL, $code = 0)
{
$this->_object_name = $object_name;
$this->_objects['_object'] = $object;
parent::__construct($message, $values, $code);
}
Adds a Validation object to this exception
// The following will add a validation object for a profile model
// inside the exception for a user model.
$e->add_object('profile', $validation);
// The errors array will now look something like this
// array
// (
// 'username' => 'This field is required',
// 'profile' => array
// (
// 'first_name' => 'This field is required',
// ),
// );
string
$alias
required - The relationship alias from the modelValidation
$object
required - The Validation object to mergemixed
$has_many
= bool FALSE - The array key to use if this exception can be merged multiple timesORM_Validation_Exception
public function add_object($alias, Validation $object, $has_many = FALSE)
{
if ($has_many === TRUE)
{
// This is most likely a has_many relationship
$this->_objects[$alias][]['_object'] = $object;
}
elseif ($has_many)
{
// This is most likely a has_many relationship
$this->_objects[$alias][$has_many]['_object'] = $object;
}
else
{
$this->_objects[$alias]['_object'] = $object;
}
return $this;
}
Returns a merged array of the errors from all the Validation objects in this exception
// Will load Model_User errors from messages/orm-validation/user.php
$e->errors('orm-validation');
string
$directory
= NULL - Directory to load error messages frommixed
$translate
= bool TRUE - Translate the messagearray
public function errors($directory = NULL, $translate = TRUE)
{
if ($directory !== NULL)
{
// Everything starts at $directory/$object_name
$directory .= '/'.$this->_object_name;
}
return $this->generate_errors($this->_objects, $directory, $translate);
}
Merges an ORM_Validation_Exception object into the current exception Useful when you want to combine errors into one array
string
$alias
required - The relationship alias from the modelORM_Validation_Exception
$object
required - The exception to mergemixed
$has_many
= bool FALSE - The array key to use if this exception can be merged multiple timesORM_Validation_Exception
public function merge($alias, ORM_Validation_Exception $object, $has_many = FALSE)
{
if ($has_many === TRUE)
{
// This is most likely a has_many relationship
$this->_objects[$alias][] = $object->objects();
}
elseif ($has_many)
{
// This is most likely a has_many relationship
$this->_objects[$alias][$has_many] = $object->objects();
}
else
{
$this->_objects[$alias] = $object->objects();
}
return $this;
}
Returns the protected _objects property from this exception
array
public function objects()
{
return $this->_objects;
}
Magic object-to-string method.
echo $exception;
string
public function __toString()
{
return Kohana_Exception::text($this);
}
Inline exception handler, displays the error message, source of the exception, and the stack trace of the error.
Exception
$e
required - $eboolean
public static function handler(Exception $e)
{
try
{
// Get the exception information
$type = get_class($e);
$code = $e->getCode();
$message = $e->getMessage();
$file = $e->getFile();
$line = $e->getLine();
// Get the exception backtrace
$trace = $e->getTrace();
if ($e instanceof ErrorException)
{
if (isset(Kohana_Exception::$php_errors[$code]))
{
// Use the human-readable error name
$code = Kohana_Exception::$php_errors[$code];
}
if (version_compare(PHP_VERSION, '5.3', '<'))
{
// Workaround for a bug in ErrorException::getTrace() that
// exists in all PHP 5.2 versions.
// @link http://bugs.php.net/45895
for ($i = count($trace) - 1; $i > 0; --$i)
{
if (isset($trace[$i - 1]['args']))
{
// Re-position the args
$trace[$i]['args'] = $trace[$i - 1]['args'];
// Remove the args
unset($trace[$i - 1]['args']);
}
}
}
}
// Create a text version of the exception
$error = Kohana_Exception::text($e);
if (is_object(Kohana::$log))
{
// Add this exception to the log
Kohana::$log->add(Log::ERROR, $error);
// Make sure the logs are written
Kohana::$log->write();
}
if (Kohana::$is_cli)
{
// Just display the text of the exception
echo "\n{$error}\n";
exit(1);
}
if ( ! headers_sent())
{
// Make sure the proper http header is sent
$http_header_status = ($e instanceof HTTP_Exception) ? $code : 500;
header('Content-Type: text/html; charset='.Kohana::$charset, TRUE, $http_header_status);
}
if (Request::$current !== NULL AND Request::current()->is_ajax() === TRUE)
{
// Just display the text of the exception
echo "\n{$error}\n";
exit(1);
}
// Start an output buffer
ob_start();
// Include the exception HTML
if ($view_file = Kohana::find_file('views', Kohana_Exception::$error_view))
{
include $view_file;
}
else
{
throw new Kohana_Exception('Error view file does not exist: views/:file', array(
':file' => Kohana_Exception::$error_view,
));
}
// Display the contents of the output buffer
echo ob_get_clean();
exit(1);
}
catch (Exception $e)
{
// Clean the output buffer if one exists
ob_get_level() and ob_clean();
// Display the exception text
echo Kohana_Exception::text($e), "\n";
// Exit with an error status
exit(1);
}
}
Get a single line of text representing the exception:
Error [ Code ]: Message ~ File [ Line ]
Exception
$e
required - $estring
public static function text(Exception $e)
{
return sprintf('%s [ %s ]: %s ~ %s [ %d ]',
get_class($e), $e->getCode(), strip_tags($e->getMessage()), Debug::path($e->getFile()), $e->getLine());
}
Recursive method to fetch all the errors in this exception
array
$array
required - Array of Validation objects to get errors fromstring
$directory
required - Directory to load error messages frommixed
$translate
required - Translate the messagearray
protected function generate_errors(array $array, $directory, $translate)
{
$errors = array();
foreach ($array as $alias => $object)
{
if ($directory === NULL)
{
// Return the raw errors
$file = NULL;
}
else
{
$file = trim($directory.'/'.$alias, '/');
}
if (is_array($object))
{
// Recursively fill the errors array
$errors[$alias] = $this->generate_errors($object, $file, $translate);
}
else
{
// Merge in this array of errors
$errors += $object->errors($directory, $translate);
}
}
return $errors;
}