ORM Validation exceptions.
Class declared in MODPATH/orm/classes/ORM/Validation/Exception.php on line 10.
string
$error_viewlink to thiserror rendering view
string(12) "kohana/error"
string
$error_view_content_typelink to thiserror view content type
string(9) "text/html"
array
$php_errorslink to thisPHP error code => human readable name
array(9) ( 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" 8192 => string(10) "Deprecated" )
string
$_aliaslink to thisThe alias of the main ORM model this exception was created for
NULL
array
$_objectslink to thisArray of validation objects
array(0)
$codelink to thisinteger 0
$filelink to thisNULL
$linelink to thisNULL
$messagelink to thisstring(0) ""
Constructs a new exception for the specified model
string
$alias
required - The alias to use when looking for error messagesValidation
$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 exceptionunknown
$previous
= NULLvoid
public
function
__construct(
$alias
, Validation
$object
,
$message
=
'Failed to validate array'
,
array
$values
= NULL,
$code
= 0, Exception
$previous
= NULL)
{
$this
->_alias =
$alias
;
$this
->_objects[
'_object'
] =
$object
;
$this
->_objects[
'_has_many'
] = FALSE;
parent::__construct(
$message
,
$values
,
$code
,
$previous
);
}
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)
{
// We will need this when generating errors
$this
->_objects[
$alias
][
'_has_many'
] = (
$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 the protected _alias property from this exception
string
public
function
alias()
{
return
$this
->_alias;
}
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)
{
return
$this
->generate_errors(
$this
->_alias,
$this
->_objects,
$directory
,
$translate
);
}
Merges an ORM_Validation_Exception object into the current exception Useful when you want to combine errors into one array
ORM_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(ORM_Validation_Exception
$object
,
$has_many
= FALSE)
{
$alias
=
$object
->alias();
// We will need this when generating errors
$this
->_objects[
$alias
][
'_has_many'
] = (
$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
);
}
Exception handler, logs the exception and generates a Response object for display.
Exception
$e
required - $eResponse
public
static
function
_handler(Exception
$e
)
{
try
{
// Log the exception
Kohana_Exception::log(
$e
);
// Generate the response
$response
= Kohana_Exception::response(
$e
);
return
$response
;
}
catch
(Exception
$e
)
{
/**
* Things are going *really* badly for us, We now have no choice
* but to bail. Hard.
*/
// Clean the output buffer if one exists
ob_get_level() AND ob_clean();
// Set the Status code to 500, and Content-Type to text/plain.
header(
'Content-Type: text/plain; charset='
.Kohana::
$charset
, TRUE, 500);
echo
Kohana_Exception::text(
$e
);
exit
(1);
}
}
Inline exception handler, displays the error message, source of the exception, and the stack trace of the error.
Exception
$e
required - $evoid
public
static
function
handler(Exception
$e
)
{
$response
= Kohana_Exception::_handler(
$e
);
// Send the response to the browser
echo
$response
->send_headers()->body();
exit
(1);
}
Logs an exception.
Exception
$e
required - $eint
$level
= integer 0 - $levelvoid
public
static
function
log(Exception
$e
,
$level
= Log::EMERGENCY)
{
if
(
is_object
(Kohana::
$log
))
{
// Create a text version of the exception
$error
= Kohana_Exception::text(
$e
);
// Add this exception to the log
Kohana::
$log
->add(
$level
,
$error
, NULL,
array
(
'exception'
=>
$e
));
// Make sure the logs are written
Kohana::
$log
->write();
}
}
Get a Response object representing the exception
Exception
$e
required - $eResponse
public
static
function
response(Exception
$e
)
{
try
{
// Get the exception information
$class
= get_class(
$e
);
$code
=
$e
->getCode();
$message
=
$e
->getMessage();
$file
=
$e
->getFile();
$line
=
$e
->getLine();
$trace
=
$e
->getTrace();
/**
* HTTP_Exceptions are constructed in the HTTP_Exception::factory()
* method. We need to remove that entry from the trace and overwrite
* the variables from above.
*/
if
(
$e
instanceof
HTTP_Exception AND
$trace
[0][
'function'
] ==
'factory'
)
{
extract(
array_shift
(
$trace
));
}
if
(
$e
instanceof
ErrorException)
{
/**
* If XDebug is installed, and this is a fatal error,
* use XDebug to generate the stack trace
*/
if
(function_exists(
'xdebug_get_function_stack'
) AND
$code
== E_ERROR)
{
$trace
=
array_slice
(
array_reverse
(xdebug_get_function_stack()), 4);
foreach
(
$trace
as
&
$frame
)
{
/**
* XDebug pre 2.1.1 doesn't currently set the call type key
*/
if
( ! isset(
$frame
[
'type'
]))
{
$frame
[
'type'
] =
'??'
;
}
// Xdebug returns the words 'dynamic' and 'static' instead of using '->' and '::' symbols
if
(
'dynamic'
===
$frame
[
'type'
])
{
$frame
[
'type'
] =
'->'
;
}
elseif
(
'static'
===
$frame
[
'type'
])
{
$frame
[
'type'
] =
'::'
;
}
// XDebug also has a different name for the parameters array
if
(isset(
$frame
[
'params'
]) AND ! isset(
$frame
[
'args'
]))
{
$frame
[
'args'
] =
$frame
[
'params'
];
}
}
}
if
(isset(Kohana_Exception::
$php_errors
[
$code
]))
{
// Use the human-readable error name
$code
= Kohana_Exception::
$php_errors
[
$code
];
}
}
/**
* The stack trace becomes unmanageable inside PHPUnit.
*
* The error view ends up several GB in size, taking
* serveral minutes to render.
*/
if
(
defined(
'PHPUnit_MAIN_METHOD'
)
OR
defined(
'PHPUNIT_COMPOSER_INSTALL'
)
OR
defined(
'__PHPUNIT_PHAR__'
)
)
{
$trace
=
array_slice
(
$trace
, 0, 2);
}
// Instantiate the error view.
$view
= View::factory(Kohana_Exception::
$error_view
, get_defined_vars());
// Prepare the response object.
$response
= Response::factory();
// Set the response status
$response
->status((
$e
instanceof
HTTP_Exception) ?
$e
->getCode() : 500);
// Set the response headers
$response
->headers(
'Content-Type'
, Kohana_Exception::
$error_view_content_type
.
'; charset='
.Kohana::
$charset
);
// Set the response body
$response
->body(
$view
->render());
}
catch
(Exception
$e
)
{
/**
* Things are going badly for us, Lets try to keep things under control by
* generating a simpler response object.
*/
$response
= Response::factory();
$response
->status(500);
$response
->headers(
'Content-Type'
,
'text/plain'
);
$response
->body(Kohana_Exception::text(
$e
));
}
return
$response
;
}
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
string
$alias
required - Alias to use for messages filearray
$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(
$alias
,
array
$array
,
$directory
,
$translate
)
{
$errors
=
array
();
foreach
(
$array
as
$key
=>
$object
)
{
if
(
is_array
(
$object
))
{
$errors
[
$key
] = (
$key
===
'_external'
)
// Search for errors in $alias/_external.php
?
$this
->generate_errors(
$alias
.
'/'
.
$key
,
$object
,
$directory
,
$translate
)
// Regular models get their own file not nested within $alias
:
$this
->generate_errors(
$key
,
$object
,
$directory
,
$translate
);
}
elseif
(
$object
instanceof
Validation)
{
if
(
$directory
=== NULL)
{
// Return the raw errors
$file
= NULL;
}
else
{
$file
= trim(
$directory
.
'/'
.
$alias
,
'/'
);
}
// Merge in this array of errors
$errors
+=
$object
->errors(
$file
,
$translate
);
}
}
return
$errors
;
}