Modules

ORM_Validation_Exception
extends Kohana_ORM_Validation_Exception
extends Kohana_Exception
extends Kohana_Kohana_Exception
extends Exception

ORM Validation exceptions.

package
Kohana/ORM
author
Kohana Team
copyright
© 2008-2009 Kohana Team
license
http://kohanaphp.com/license

Class declared in MODPATH/orm/classes/orm/validation/exception.php on line 10.

Properties

public static string $error_view

error rendering view

string(12) "kohana/error"

public static string $error_view_content_type

error view content type

string(9) "text/html"

public static array $php_errors

PHP 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"
)

protected string $_alias

The alias of the main ORM model this exception was created for

protected array $_objects

Array of validation objects

protected $code

protected $file

protected $line

protected $message

Methods

public __construct( string $alias , Validation $object [, string $message = string(24) "Failed to validate array" , array $values = NULL , integer $code = integer 0 ] ) (defined in Kohana_ORM_Validation_Exception)

Constructs a new exception for the specified model

Parameters

  • string $alias required - The alias to use when looking for error messages
  • Validation $object required - The Validation object of the model
  • string $message = string(24) "Failed to validate array" - The error message
  • array $values = NULL - The array of values for the error message
  • integer $code = integer 0 - The error code for the exception

Return Values

  • void

Source Code

public function __construct($alias, Validation $object, $message = 'Failed to validate array', array $values = NULL, $code = 0)
{
	$this->_alias = $alias;
	$this->_objects['_object'] = $object;
	$this->_objects['_has_many'] = FALSE;

	parent::__construct($message, $values, $code);
}

public add_object( string $alias , Validation $object [, mixed $has_many = bool FALSE ] ) (defined in Kohana_ORM_Validation_Exception)

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',
//   ),
// );

Parameters

  • string $alias required - The relationship alias from the model
  • Validation $object required - The Validation object to merge
  • mixed $has_many = bool FALSE - The array key to use if this exception can be merged multiple times

Return Values

  • ORM_Validation_Exception

Source Code

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;
}

public alias( ) (defined in Kohana_ORM_Validation_Exception)

Returns the protected _alias property from this exception

Return Values

  • string

Source Code

public function alias()
{
	return $this->_alias;
}

public errors( [ string $directory = NULL , mixed $translate = bool TRUE ] ) (defined in Kohana_ORM_Validation_Exception)

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');

Parameters

  • string $directory = NULL - Directory to load error messages from
  • mixed $translate = bool TRUE - Translate the message

Tags

  • See - generate_errors()

Return Values

  • array

Source Code

public function errors($directory = NULL, $translate = TRUE)
{
	return $this->generate_errors($this->_alias, $this->_objects, $directory, $translate);
}

public merge( ORM_Validation_Exception $object [, mixed $has_many = bool FALSE ] ) (defined in Kohana_ORM_Validation_Exception)

Merges an ORM_Validation_Exception object into the current exception Useful when you want to combine errors into one array

Parameters

  • ORM_Validation_Exception $object required - The exception to merge
  • mixed $has_many = bool FALSE - The array key to use if this exception can be merged multiple times

Return Values

  • ORM_Validation_Exception

Source Code

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;
}

public objects( ) (defined in Kohana_ORM_Validation_Exception)

Returns the protected _objects property from this exception

Return Values

  • array

Source Code

public function objects()
{
	return $this->_objects;
}

public __toString( ) (defined in Kohana_Kohana_Exception)

Magic object-to-string method.

echo $exception;

Tags

Return Values

  • string

Source Code

public function __toString()
{
	return Kohana_Exception::text($this);
}

public static handler( Exception $e ) (defined in Kohana_Kohana_Exception)

Inline exception handler, displays the error message, source of the exception, and the stack trace of the error.

Parameters

  • Exception $e required - $e

Tags

Return Values

  • boolean

Source Code

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);

			$strace = Kohana_Exception::text($e)."\n--\n" . $e->getTraceAsString();
			Kohana::$log->add(Log::STRACE, $strace);

			// 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: '.Kohana_Exception::$error_view_content_type.'; 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);
	}
}

public static text( Exception $e ) (defined in Kohana_Kohana_Exception)

Get a single line of text representing the exception:

Error [ Code ]: Message ~ File [ Line ]

Parameters

  • Exception $e required - $e

Return Values

  • string

Source Code

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());
}

final public getCode( ) (defined in Exception)

final public getFile( ) (defined in Exception)

final public getLine( ) (defined in Exception)

final public getMessage( ) (defined in Exception)

final public getPrevious( ) (defined in Exception)

final public getTrace( ) (defined in Exception)

final public getTraceAsString( ) (defined in Exception)

protected generate_errors( string $alias , array $array , string $directory , mixed $translate ) (defined in Kohana_ORM_Validation_Exception)

Recursive method to fetch all the errors in this exception

Parameters

  • string $alias required - Alias to use for messages file
  • array $array required - Array of Validation objects to get errors from
  • string $directory required - Directory to load error messages from
  • mixed $translate required - Translate the message

Return Values

  • array

Source Code

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;
}

final private __clone( ) (defined in Exception)