1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright 2016 - 2018, Cake Development Corporation (http://cakedc.com) |
4
|
|
|
* |
5
|
|
|
* Licensed under The MIT License |
6
|
|
|
* Redistributions of files must retain the above copyright notice. |
7
|
|
|
* |
8
|
|
|
* @copyright Copyright 2016 - 2018, Cake Development Corporation (http://cakedc.com) |
9
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php) |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace CakeDC\Api\Exception; |
13
|
|
|
|
14
|
|
|
use Cake\Core\Exception\Exception; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Used to return validation errors. |
18
|
|
|
*/ |
19
|
|
|
class ValidationException extends ServiceException |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
protected $_defaultCode = 422; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Validation errors |
26
|
|
|
* |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
protected $_validationErrors = []; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Construct method, for fast instantiation |
33
|
|
|
* |
34
|
|
|
* @param string $message the string of the error message |
35
|
|
|
* @param int $code The code of the error |
36
|
|
|
* @param \Exception|null $previous the previous exception. |
37
|
|
|
* @param array $validationErrors the array with the validations |
38
|
|
|
*/ |
39
|
13 |
|
public function __construct( |
40
|
|
|
$message = 'Validation errors', |
41
|
|
|
$code = 0, |
42
|
|
|
$previous = null, |
43
|
|
|
$validationErrors = [] |
44
|
|
|
) { |
45
|
13 |
|
if ($code === 0) { |
46
|
13 |
|
$code = $this->_defaultCode; |
47
|
13 |
|
} |
48
|
13 |
|
$this->_validationErrors = $validationErrors; |
49
|
13 |
|
parent::__construct($message, $code, $previous); |
50
|
13 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Sets validation errors |
54
|
|
|
* |
55
|
|
|
* @param array $validationErrors the array with the validations |
56
|
|
|
* @return void |
57
|
|
|
*/ |
58
|
|
|
public function setValidationErrors($validationErrors = []) |
59
|
|
|
{ |
60
|
|
|
$this->_validationErrors = $validationErrors; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Gets validation errors |
65
|
|
|
* |
66
|
|
|
* @return array |
67
|
|
|
*/ |
68
|
3 |
|
public function getValidationErrors() |
69
|
|
|
{ |
70
|
3 |
|
return $this->_validationErrors; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|