silverstripe-framework/ORM/ValidationException.php

63 lines
1.6 KiB
PHP
Raw Normal View History

<?php
namespace SilverStripe\ORM;
use Exception;
/**
* 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.
*/
class ValidationException extends Exception {
/**
* The contained ValidationResult related to this error
2014-08-15 08:53:05 +02:00
*
* @var ValidationResult
*/
protected $result;
/**
* Construct a new ValidationException with an optional ValidationResult object
2014-08-15 08:53:05 +02: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
* the error code number.
* @param integer $code The error code number, if not given in the second parameter
*/
public function __construct($result = null, $message = null, $code = 0) {
2014-08-15 08:53:05 +02:00
// Check arguments
if(!($result instanceof ValidationResult)) {
2014-08-15 08:53:05 +02:00
// Shift parameters if no ValidationResult is given
$code = $message;
$message = $result;
2014-08-15 08:53:05 +02:00
// Infer ValidationResult from parameters
$result = new ValidationResult(false, $message);
} elseif(empty($message)) {
2014-08-15 08:53:05 +02:00
// Infer message if not given
$message = $result->message();
}
2014-08-15 08:53:05 +02:00
// Construct
$this->result = $result;
parent::__construct($message, $code);
}
2014-08-15 08:53:05 +02:00
/**
* Retrieves the ValidationResult related to this error
2014-08-15 08:53:05 +02:00
*
* @return ValidationResult
*/
public function getResult() {
2014-08-15 08:53:05 +02:00
return $this->result;
}
}