Completed
Push — master ( 46ca35...9cf9e5 )
by Maxim
03:19
created

ForbiddenConstraint::getMessage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 10
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 10
loc 10
rs 9.4285
cc 2
eloc 6
nc 2
nop 0
1
<?php
2
3
namespace Weew\Validator\Constraints;
4
5
use Weew\Validator\IConstraint;
6
use Weew\Validator\IValidationData;
7
8
/**
9
 * Check if the value is not in the list of forbidden values.
10
 */
11 View Code Duplication
class ForbiddenConstraint implements IConstraint {
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
12
    /**
13
     * @var array
14
     */
15
    protected $forbidden;
16
17
    /**
18
     * @var string
19
     */
20
    protected $message;
21
22
    /**
23
     * @param array $forbidden
24
     * @param string $message
25
     */
26
    public function __construct(array $forbidden, $message = null) {
27
        $this->forbidden = $forbidden;
28
        $this->message = $message;
29
    }
30
31
    /**
32
     * @param $value
33
     * @param IValidationData $data
34
     *
35
     * @return bool
36
     */
37
    public function check($value, IValidationData $data = null) {
38
        return ! array_contains($this->forbidden, $value);
39
    }
40
41
    /**
42
     * @return string
43
     */
44
    public function getMessage() {
45
        if ($this->message !== null) {
46
            return $this->message;
47
        }
48
49
        return s(
50
            'Invalid value, forbidden values are "%s".',
51
            implode(', ', $this->forbidden)
52
        );
53
    }
54
55
    /**
56
     * @return array
57
     */
58
    public function getOptions() {
59
        return [
60
            'forbidden' => $this->forbidden,
61
        ];
62
    }
63
}
64