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