2008-10-08 22:42:09 +02:00
|
|
|
<?php
|
2016-06-15 06:03:16 +02:00
|
|
|
|
|
|
|
namespace SilverStripe\ORM;
|
|
|
|
|
|
|
|
use Exception;
|
|
|
|
|
2008-10-08 22:42:09 +02:00
|
|
|
/**
|
|
|
|
* Exception thrown by {@link DataObject}::write if validation fails. By throwing an
|
|
|
|
* exception rather than a user error, the exception can be caught in unit tests and as such
|
|
|
|
* can be used as a successful test.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2016-06-15 06:03:16 +02:00
|
|
|
* @subpackage orm
|
2008-10-08 22:42:09 +02:00
|
|
|
*/
|
|
|
|
class ValidationException extends Exception {
|
|
|
|
|
|
|
|
/**
|
2013-03-15 01:56:43 +01:00
|
|
|
* The contained ValidationResult related to this error
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-03-15 01:56:43 +01:00
|
|
|
* @var ValidationResult
|
2008-10-08 22:42:09 +02:00
|
|
|
*/
|
|
|
|
protected $result;
|
|
|
|
|
2013-03-15 01:56:43 +01:00
|
|
|
/**
|
|
|
|
* Construct a new ValidationException with an optional ValidationResult object
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-03-15 01:56:43 +01:00
|
|
|
* @param ValidationResult|string $result The ValidationResult containing the
|
|
|
|
* failed result. Can be substituted with an error message instead if no
|
|
|
|
* ValidationResult exists.
|
2014-08-15 08:53:05 +02:00
|
|
|
* @param string|integer $message The error message. If $result was given the
|
|
|
|
* message string rather than a ValidationResult object then this will have
|
2013-03-15 01:56:43 +01:00
|
|
|
* the error code number.
|
|
|
|
* @param integer $code The error code number, if not given in the second parameter
|
|
|
|
*/
|
2008-10-08 22:42:09 +02:00
|
|
|
public function __construct($result = null, $message = null, $code = 0) {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-15 01:56:43 +01:00
|
|
|
// Check arguments
|
|
|
|
if(!($result instanceof ValidationResult)) {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-15 01:56:43 +01:00
|
|
|
// Shift parameters if no ValidationResult is given
|
2008-10-08 22:42:09 +02:00
|
|
|
$code = $message;
|
|
|
|
$message = $result;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-15 01:56:43 +01:00
|
|
|
// Infer ValidationResult from parameters
|
|
|
|
$result = new ValidationResult(false, $message);
|
|
|
|
} elseif(empty($message)) {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-15 01:56:43 +01:00
|
|
|
// Infer message if not given
|
|
|
|
$message = $result->message();
|
2008-10-08 22:42:09 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-15 01:56:43 +01:00
|
|
|
// Construct
|
|
|
|
$this->result = $result;
|
2008-10-08 22:42:09 +02:00
|
|
|
parent::__construct($message, $code);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-15 01:56:43 +01:00
|
|
|
/**
|
|
|
|
* Retrieves the ValidationResult related to this error
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-03-15 01:56:43 +01:00
|
|
|
* @return ValidationResult
|
|
|
|
*/
|
2008-10-08 22:42:09 +02:00
|
|
|
public function getResult() {
|
2014-08-15 08:53:05 +02:00
|
|
|
return $this->result;
|
2008-10-08 22:42:09 +02:00
|
|
|
}
|
|
|
|
}
|