Code Duplication    Length = 43-50 lines in 7 locations

src/Weew/Validator/Constraints/EmailConstraint.php 1 location

@@ 11-56 (lines=46) @@
8
/**
9
 * Check if the value is valid email address.
10
 */
11
class EmailConstraint implements IConstraint {
12
    /**
13
     * @var string
14
     */
15
    protected $message;
16
17
    /**
18
     * EmailConstraint constructor.
19
     *
20
     * @param string $message
21
     */
22
    public function __construct($message = null) {
23
        $this->message = $message;
24
    }
25
26
    /**
27
     * @param $value
28
     * @param IValidationData $data
29
     *
30
     * @return bool
31
     */
32
    public function check($value, IValidationData $data = null) {
33
        if (is_string($value)) {
34
            return filter_var($value, FILTER_VALIDATE_EMAIL) !== false;
35
        }
36
        return false;
37
    }
38
39
    /**
40
     * @return string
41
     */
42
    public function getMessage() {
43
        if ($this->message !== null) {
44
            return $this->message;
45
        }
46
47
        return 'Must be an email address.';
48
    }
49
50
    /**
51
     * @return array
52
     */
53
    public function getOptions() {
54
        return [];
55
    }
56
}
57

src/Weew/Validator/Constraints/IPConstraint.php 1 location

@@ 11-53 (lines=43) @@
8
/**
9
 * Check if the value is a valid IP address.
10
 */
11
class IPConstraint implements IConstraint {
12
    /**
13
     * @var string
14
     */
15
    protected $message;
16
17
    /**
18
     * IPConstraint constructor.
19
     *
20
     * @param string $message
21
     */
22
    public function __construct($message = null) {
23
        $this->message = $message;
24
    }
25
26
    /**
27
     * @param $value
28
     * @param IValidationData $data
29
     *
30
     * @return bool
31
     */
32
    public function check($value, IValidationData $data = null) {
33
        return filter_var($value, FILTER_VALIDATE_IP) !== false;
34
    }
35
36
    /**
37
     * @return string
38
     */
39
    public function getMessage() {
40
        if ($this->message !== null) {
41
            return $this->message;
42
        }
43
44
        return 'Must be an IP address.';
45
    }
46
47
    /**
48
     * @return array
49
     */
50
    public function getOptions() {
51
        return [];
52
    }
53
}
54

src/Weew/Validator/Constraints/IPv4Constraint.php 1 location

@@ 11-53 (lines=43) @@
8
/**
9
 * Check if the value is a valid IP v4 address.
10
 */
11
class IPv4Constraint implements IConstraint {
12
    /**
13
     * @var string
14
     */
15
    protected $message;
16
17
    /**
18
     * IPv4Constraint constructor.
19
     *
20
     * @param string $message
21
     */
22
    public function __construct($message = null) {
23
        $this->message = $message;
24
    }
25
26
    /**
27
     * @param $value
28
     * @param IValidationData $data
29
     *
30
     * @return bool
31
     */
32
    public function check($value, IValidationData $data = null) {
33
        return filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) !== false;
34
    }
35
36
    /**
37
     * @return string
38
     */
39
    public function getMessage() {
40
        if ($this->message !== null) {
41
            return $this->message;
42
        }
43
44
        return 'Must be an IPv4 address.';
45
    }
46
47
    /**
48
     * @return array
49
     */
50
    public function getOptions() {
51
        return [];
52
    }
53
}
54

src/Weew/Validator/Constraints/IPv6Constraint.php 1 location

@@ 11-53 (lines=43) @@
8
/**
9
 * Check if the value is a valid IP v6 address.
10
 */
11
class IPv6Constraint implements IConstraint {
12
    /**
13
     * @var string
14
     */
15
    protected $message;
16
17
    /**
18
     * IPv6Constraint constructor.
19
     *
20
     * @param string $message
21
     */
22
    public function __construct($message = null) {
23
        $this->message = $message;
24
    }
25
26
    /**
27
     * @param $value
28
     * @param IValidationData $data
29
     *
30
     * @return bool
31
     */
32
    public function check($value, IValidationData $data = null) {
33
        return filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) !== false;
34
    }
35
36
    /**
37
     * @return string
38
     */
39
    public function getMessage() {
40
        if ($this->message !== null) {
41
            return $this->message;
42
        }
43
44
        return 'Must be an IPv6 address.';
45
    }
46
47
    /**
48
     * @return array
49
     */
50
    public function getOptions() {
51
        return [];
52
    }
53
}
54

src/Weew/Validator/Constraints/UrlConstraint.php 1 location

@@ 11-60 (lines=50) @@
8
/**
9
 * Checks if the value is a valid URL
10
 */
11
class UrlConstraint implements IConstraint {
12
    /**
13
     * @var string
14
     */
15
    protected $message;
16
17
    /**
18
     * UrlConstraint constructor.
19
     *
20
     * @param string $message
21
     */
22
    public function __construct($message = null) {
23
        $this->message = $message;
24
    }
25
26
    /**
27
     * Note, that any space characters in either the beginning or
28
     * the end of the string will result in a failure.
29
     *
30
     * @param $value
31
     * @param IValidationData $data
32
     *
33
     * @return bool
34
     */
35
    public function check($value, IValidationData $data = null) {
36
        if (is_string($value)) {
37
            return filter_var($value, FILTER_VALIDATE_URL) !== false;
38
        }
39
40
        return false;
41
    }
42
43
    /**
44
     * @return string
45
     */
46
    public function getMessage() {
47
        if ($this->message !== null) {
48
            return $this->message;
49
        }
50
51
        return 'Must be a url.';
52
    }
53
54
    /**
55
     * @return array
56
     */
57
    public function getOptions() {
58
        return [];
59
    }
60
}
61

src/Weew/Validator/Constraints/MacAddressConstraint.php 1 location

@@ 11-57 (lines=47) @@
8
/**
9
 * Check if the value is a mac address.
10
 */
11
class MacAddressConstraint implements IConstraint {
12
    /**
13
     * @var string
14
     */
15
    protected $message;
16
17
    /**
18
     * MacAddressConstraint constructor.
19
     *
20
     * @param string $message
21
     */
22
    public function __construct($message = null) {
23
        $this->message = $message;
24
    }
25
26
    /**
27
     * @param $value
28
     * @param IValidationData $data
29
     *
30
     * @return bool
31
     */
32
    public function check($value, IValidationData $data = null) {
33
        if (is_string($value)) {
34
            return filter_var($value, FILTER_VALIDATE_MAC) !== false;
35
        }
36
37
        return false;
38
    }
39
40
    /**
41
     * @return string
42
     */
43
    public function getMessage() {
44
        if ($this->message !== null) {
45
            return $this->message;
46
        }
47
48
        return 'Must be a MAC address.';
49
    }
50
51
    /**
52
     * @return array
53
     */
54
    public function getOptions() {
55
        return [];
56
    }
57
}
58

src/Weew/Validator/Constraints/NullableConstraint.php 1 location

@@ 14-56 (lines=43) @@
11
 * that occurred for the given subject if the subject value
12
 * is null or '' where this constraint has been applied.
13
 */
14
class NullableConstraint implements IConstraint {
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