Passed
Push — master ( 1ec89a...1818e9 )
by Vincent
06:54 queued 04:35
created

Constraint::toString()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 2
rs 10
c 1
b 0
f 0
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