Blame view

framework/model/ValidationException.php 1.66 KB
0084d336   Administrator   Importers CRUD
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
  <?php
  /**
   * 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.
   * 
   * @package framework
   * @subpackage validation
   */
  class ValidationException extends Exception {
  
  	/**
  	 * The contained ValidationResult related to this error
  	 * 
  	 * @var ValidationResult
  	 */
  	protected $result;
  
  	/**
  	 * Construct a new ValidationException with an optional ValidationResult object
  	 * 
  	 * @param ValidationResult|string $result The ValidationResult containing the
  	 * failed result. Can be substituted with an error message instead if no
  	 * ValidationResult exists.
  	 * @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) {
  		
  		// Check arguments
  		if(!($result instanceof ValidationResult)) {
  			
  			// Shift parameters if no ValidationResult is given
  			$code = $message;
  			$message = $result;
  			
  			// Infer ValidationResult from parameters
  			$result = new ValidationResult(false, $message);
  		} elseif(empty($message)) {
  			
  			// Infer message if not given
  			$message = $result->message();
  		}
  		
  		// Construct
  		$this->result = $result;
  		parent::__construct($message, $code);
  	}
  	
  	/**
  	 * Retrieves the ValidationResult related to this error
  	 * 
  	 * @return ValidationResult
  	 */
  	public function getResult() {
  		return $this->result;	
  	}
  }