|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace VGirol\JsonApiStructure\Constraint; |
|
6
|
|
|
|
|
7
|
|
|
use VGirol\JsonApiStructure\Exception\ValidationException; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Abstract class for constraint |
|
11
|
|
|
*/ |
|
12
|
|
|
abstract class Constraint |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* Undocumented variable |
|
16
|
|
|
* |
|
17
|
|
|
* @var string |
|
18
|
|
|
*/ |
|
19
|
|
|
private $failureMessage; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Returns a string representation of the constraint. |
|
23
|
|
|
*/ |
|
24
|
|
|
abstract public function default(): string; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Returns a string representation of the constraint. |
|
28
|
|
|
*/ |
|
29
|
174 |
|
public function toString(): string |
|
30
|
|
|
{ |
|
31
|
174 |
|
$failureMessage = $this->default(); |
|
32
|
174 |
|
if ($this->failureMessage) { |
|
33
|
84 |
|
$failureMessage .= "\n" . $this->failureMessage; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
174 |
|
return $failureMessage; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Evaluates the constraint for parameter $other |
|
41
|
|
|
* |
|
42
|
|
|
* @param mixed $inspected |
|
43
|
|
|
* @param string $description |
|
44
|
|
|
* @param string|int $code |
|
45
|
|
|
* |
|
46
|
|
|
* @return void |
|
47
|
|
|
* @throws \VGirol\JsonApiStructure\Exception\ValidationException |
|
48
|
|
|
*/ |
|
49
|
462 |
|
public function evaluate($inspected, string $description = '', $code = 403): void |
|
50
|
|
|
{ |
|
51
|
462 |
|
$success = $this->handle($inspected); |
|
52
|
|
|
|
|
53
|
462 |
|
if (!$success) { |
|
54
|
174 |
|
$this->fail($description, $code); |
|
55
|
|
|
} |
|
56
|
372 |
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Evaluates the constraint for parameter $inspected. Returns true if the constraint is met, false otherwise. |
|
60
|
|
|
* |
|
61
|
|
|
* @param mixed $inspected value or object to evaluate |
|
62
|
|
|
* |
|
63
|
|
|
* @return boolean |
|
64
|
|
|
*/ |
|
65
|
|
|
abstract public function handle($inspected): bool; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Undocumented function |
|
69
|
|
|
* |
|
70
|
|
|
* @param string $message |
|
71
|
|
|
* |
|
72
|
|
|
* @return void |
|
73
|
|
|
*/ |
|
74
|
84 |
|
protected function setFailureMessage(string $message): void |
|
75
|
|
|
{ |
|
76
|
84 |
|
$this->failureMessage = $message; |
|
77
|
84 |
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Undocumented function |
|
81
|
|
|
* |
|
82
|
|
|
* @param string $description |
|
83
|
|
|
* @param mixed $code |
|
84
|
|
|
* |
|
85
|
|
|
* @return void |
|
86
|
|
|
* @throws \VGirol\JsonApiStructure\Exception\ValidationException |
|
87
|
|
|
*/ |
|
88
|
174 |
|
private function fail(string $description, $code) |
|
89
|
|
|
{ |
|
90
|
174 |
|
$failureMessage = $this->toString(); |
|
91
|
174 |
|
if ($description) { |
|
92
|
6 |
|
$failureMessage .= "\n" . $description; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
174 |
|
throw new ValidationException($failureMessage, $code); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|