1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Exception for WPDB DB Delta validator. |
7
|
|
|
* |
8
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
9
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
10
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
11
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
12
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
13
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
14
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
15
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
16
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
17
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
18
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
19
|
|
|
* |
20
|
|
|
* @since 1.1.0 |
21
|
|
|
* @author Glynn Quelch <[email protected]> |
22
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License |
23
|
|
|
* @package PinkCrab\Table_Builder |
24
|
|
|
*/ |
25
|
|
|
|
26
|
|
|
namespace PinkCrab\Table_Builder\Exception\WPDB_DB_Delta; |
27
|
|
|
|
28
|
|
|
use Exception; |
29
|
|
|
use Throwable; |
30
|
|
|
use PinkCrab\Table_Builder\Schema; |
31
|
|
|
|
32
|
|
|
class WPDB_Validator_Exception extends Exception { |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Array of errors during validation. |
36
|
|
|
* |
37
|
|
|
* @var string[] |
38
|
|
|
*/ |
39
|
|
|
private $validation_errors; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Schema |
43
|
|
|
* |
44
|
|
|
* @var Schema |
45
|
|
|
*/ |
46
|
|
|
private $schema; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param \PinkCrab\Table_Builder\Schema $schema |
50
|
|
|
* @param string[] $validation_errors |
51
|
|
|
* @param string $message |
52
|
|
|
* @param int $code |
53
|
|
|
* @param \Throwable|null $previous |
54
|
|
|
*/ |
55
|
|
|
public function __construct( |
56
|
|
|
Schema $schema, |
57
|
|
|
array $validation_errors, |
58
|
|
|
string $message = '', |
59
|
|
|
int $code = 0, |
60
|
|
|
?Throwable $previous = null |
61
|
|
|
) { |
62
|
|
|
$this->schema = $schema; |
63
|
|
|
$this->validation_errors = $validation_errors; |
64
|
|
|
parent::__construct( $message, $code, $previous ); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Returns an exception for a schema failing validation |
69
|
|
|
* |
70
|
|
|
* @param \PinkCrab\Table_Builder\Schema $schema |
71
|
|
|
* @param string[] $errors |
72
|
|
|
* @return WPDB_Validator_Exception |
73
|
|
|
* @code 201 |
74
|
|
|
*/ |
75
|
|
|
public static function failed_validation( Schema $schema, array $errors ): WPDB_Validator_Exception { |
76
|
|
|
return new WPDB_Validator_Exception( |
77
|
|
|
$schema, |
78
|
|
|
$errors, |
79
|
|
|
sprintf( '%s failed with %d errors', $schema->get_table_name(), count( $errors ) ), |
80
|
|
|
201 |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Get the table schema. |
86
|
|
|
* |
87
|
|
|
* @return Schema |
88
|
|
|
*/ |
89
|
|
|
public function get_schema(): Schema { |
90
|
|
|
return $this->schema; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Get array of errors during validation. |
95
|
|
|
* |
96
|
|
|
* @return string[] |
97
|
|
|
*/ |
98
|
|
|
public function get_validation_errors(): array { |
99
|
|
|
return $this->validation_errors; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|