Completed
Push — master ( 125592...50d3ce )
by Maxim
10:57 queued 07:00
created

Validators::compare()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 6
nop 3
dl 0
loc 12
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
namespace WebComplete\form;
4
5
class Validators
6
{
7
8
    /**
9
     * @param $value
10
     * @param array $params
11
     * - value : mixed - comparing value
12
     * - not : bool - not equals, default = false
13
     *
14
     * @return bool
15
     */
16
    public function equals($value, array $params = []): bool
17
    {
18
        $compareValue = $params['value'] ?? $value;
19
        $not = isset($params['not']) ? (bool)$params['not'] : false;
20
21
        return $not
22
            ? $value !== $compareValue
23
            : $value === $compareValue;
24
    }
25
26
    /**
27
     * @param $value
28
     * @param array $params
29
     * - field : string - field of a form to compare
30
     * - not : bool - not equals, default = false
31
     *
32
     * @param AbstractForm $form
33
     *
34
     * @return bool
35
     */
36
    public function compare($value, array $params, AbstractForm $form): bool
37
    {
38
        $result = true;
39
        $field = $params['field'] ?? false;
40
        $not = isset($params['not']) ? (bool)$params['not'] : false;
41
        if ($field) {
42
            $result = $not
43
                ? $value !== $form->getValue($field)
44
                : $value === $form->getValue($field);
45
        }
46
47
        return $result;
48
    }
49
50
    /**
51
     * @param $value
52
     *
53
     * @return bool
54
     */
55
    public function email($value): bool
56
    {
57
        $pattern = '/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.'
58
            . '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/';
59
        return preg_match($pattern, $value) > 0;
60
    }
61
62
    /**
63
     * @param $value
64
     * @param array $params
65
     * - min : float - min value, default = null
66
     * - max : float - max value, default = null
67
     *
68
     * @return bool
69
     */
70 View Code Duplication
    public function number($value, array $params = []): bool
0 ignored issues
show
Duplication introduced by
This method 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...
71
    {
72
        if (!is_numeric($value)) {
73
            return false;
74
        }
75
        if ($min = $params['min'] ?? null) {
76
            if ($value < $min) {
77
                return false;
78
            }
79
        }
80
        if ($max = $params['max'] ?? null) {
81
            if ($value > $max) {
82
                return false;
83
            }
84
        }
85
86
        return true;
87
    }
88
89
    /**
90
     * @param $value
91
     * @param array $params
92
     * - min : int - min length, default = null
93
     * - max : int - max length, default = null
94
     *
95
     * @return bool
96
     */
97 View Code Duplication
    public function string($value, array $params = []): bool
0 ignored issues
show
Duplication introduced by
This method 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...
98
    {
99
        if ($min = $params['min'] ?? null) {
100
            if (mb_strlen($value) < $min) {
101
                return false;
102
            }
103
        }
104
        if ($max = $params['max'] ?? null) {
105
            if (mb_strlen($value) > $max) {
106
                return false;
107
            }
108
        }
109
        return true;
110
    }
111
112
    /**
113
     * @param $value
114
     * @param array $params
115
     * - pattern : regex
116
     *
117
     * @return bool
118
     */
119
    public function regex($value, array $params = []): bool
120
    {
121
        $pattern = $params['pattern'] ?? '//';
122
        return preg_match($pattern, $value) > 0;
123
    }
124
}
125