1
|
|
|
<?php |
2
|
|
|
namespace HexMakina\Crudites\Errors; |
3
|
|
|
|
4
|
|
|
class CruditesError |
5
|
|
|
{ |
6
|
|
|
protected string $message; |
7
|
|
|
protected string $state; |
8
|
|
|
protected int $code; |
9
|
|
|
|
10
|
|
|
protected string $table; |
11
|
|
|
protected array $columns = []; |
12
|
|
|
protected string $constraint; |
13
|
|
|
|
14
|
|
|
public function __construct($message) |
15
|
|
|
{ |
16
|
|
|
$this->message = $message; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public function __toString(){ |
20
|
|
|
$ret = $this->message; |
21
|
|
|
|
22
|
|
|
if(!empty($this->table)) |
23
|
|
|
$ret .= sprintf(' on table "%s"', $this->table); |
24
|
|
|
if(!empty($this->columns)) |
25
|
|
|
$ret .= sprintf(' on column(s) "%s"', implode('", "', $this->columns)); |
26
|
|
|
|
27
|
|
|
return $ret; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function import($something_PDO): self |
31
|
|
|
{ |
32
|
|
|
$errorInfo = null; |
33
|
|
|
|
34
|
|
|
if(method_exists($something_PDO, 'errorInfo')) |
35
|
|
|
$errorInfo = $something_PDO->errorInfo(); |
36
|
|
|
elseif(property_exists($something_PDO, 'errorInfo')) |
37
|
|
|
$errorInfo = $something_PDO->errorInfo; |
38
|
|
|
else |
39
|
|
|
throw new \InvalidArgumentException('INVALID_PDO_CLASS'); |
40
|
|
|
|
41
|
|
|
// 0: the SQLSTATE associated with the last operation on the database handle |
42
|
|
|
$this->setState($errorInfo[0] ?? null); |
|
|
|
|
43
|
|
|
|
44
|
|
|
// 1: driver-specific error code. |
45
|
|
|
$this->setCode($errorInfo[1] ?? null); |
|
|
|
|
46
|
|
|
|
47
|
|
|
// 2: driver-specific error message |
48
|
|
|
$this->setMessage($errorInfo[2] ?? null); |
|
|
|
|
49
|
|
|
|
50
|
|
|
return $this; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
public function setState(string $state): void { |
55
|
|
|
$this->state = $state; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function getState(): string { |
59
|
|
|
return $this->state; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function setCode(int $code): void { |
63
|
|
|
$this->code = $code; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function getCode(): int { |
67
|
|
|
return $this->code; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
|
71
|
|
|
public function setMessage(string $message): void { |
72
|
|
|
$this->message = $message; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function getMessage(): string { |
76
|
|
|
return $this->message; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
|
80
|
|
|
public function setTable(string $table): void { |
81
|
|
|
$this->table = $table; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function getTable(): string { |
85
|
|
|
return $this->table; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function setColumns(array $columns): void { |
89
|
|
|
$this->columns = $columns; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function getColumns(): array { |
93
|
|
|
return $this->columns; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function setConstraint(string $constraint): void { |
97
|
|
|
$this->constraint = $constraint; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function getConstraint(): string { |
101
|
|
|
return $this->constraint; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|