Completed
Push — master ( 0df277...6ea006 )
by Maxim
03:10
created

NotAllowedValuesConstraint::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 4
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 4
loc 4
rs 10
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
namespace Weew\Validator\Constraints;
4
5
use Weew\Validator\IConstraint;
6
use Weew\Validator\IValidationData;
7
8 View Code Duplication
class NotAllowedValuesConstraint 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...
9
    /**
10
     * @var array
11
     */
12
    protected $notAllowedValues;
13
14
    /**
15
     * @var string
16
     */
17
    protected $message;
18
19
    /**
20
     * @param array $values
21
     * @param string $message
22
     */
23
    public function __construct(array $values, $message = null) {
24
        $this->notAllowedValues = $values;
25
        $this->message = $message;
26
    }
27
28
    /**
29
     * @param $value
30
     * @param IValidationData $data
31
     *
32
     * @return bool
33
     */
34
    public function check($value, IValidationData $data = null) {
35
        return ! array_contains($this->notAllowedValues, $value);
36
    }
37
38
    /**
39
     * @return string
40
     */
41
    public function getMessage() {
42
        if ($this->message !== null) {
43
            return $this->message;
44
        }
45
46
        return s(
47
            'Is not allowed, forbidden values are "%s".',
48
            implode(', ', $this->notAllowedValues)
49
        );
50
    }
51
52
    /**
53
     * @return array
54
     */
55
    public function getOptions() {
56
        return [
57
            'not_allowed_values' => $this->notAllowedValues,
58
        ];
59
    }
60
}
61