Completed
Push — master ( f6e489...d5524c )
by Maxim
03:09
created

NullableConstraint::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 3
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 3
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Weew\Validator\Constraints;
4
5
use Weew\Validator\IConstraint;
6
use Weew\Validator\IValidationData;
7
8
/**
9
 * This constraint is used to allow to tag a specific
10
 * subject as "optional". Validator will remove any errors
11
 * that occurred for the given subject if the subject value
12
 * is null or '' where this constraint has been applied.
13
 */
14 View Code Duplication
class NullableConstraint 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...
15
    /**
16
     * @var string
17
     */
18
    protected $message;
19
20
    /**
21
     * FloatConstraint constructor.
22
     *
23
     * @param string $message
24
     */
25
    public function __construct($message = null) {
26
        $this->message = $message;
27
    }
28
29
    /**
30
     * @param $value
31
     * @param IValidationData $data
32
     *
33
     * @return bool
34
     */
35
    public function check($value, IValidationData $data = null) {
36
        return array_contains([null, ''], $value) ? false : true;
37
    }
38
39
    /**
40
     * @return string
41
     */
42
    public function getMessage() {
43
        if ($this->message !== null) {
44
            return $this->message;
45
        }
46
47
        return 'May be null.';
48
    }
49
50
    /**
51
     * @return array
52
     */
53
    public function getOptions() {
54
        return [];
55
    }
56
}
57