Passed
Push — develop ( bb7610...58cba6 )
by nguereza
02:23
created

Password   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 53
c 1
b 0
f 0
dl 0
loc 117
rs 10
wmc 14

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getErrorMessage() 0 30 2
B validate() 0 39 11
A __construct() 0 9 1
1
<?php
2
3
/**
4
 * Platine Validator
5
 *
6
 * Platine Validator is a simple, extensible validation library with support for filtering
7
 *
8
 * This content is released under the MIT License (MIT)
9
 *
10
 * Copyright (c) 2020 Platine Validator
11
 *
12
 * Permission is hereby granted, free of charge, to any person obtaining a copy
13
 * of this software and associated documentation files (the "Software"), to deal
14
 * in the Software without restriction, including without limitation the rights
15
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16
 * copies of the Software, and to permit persons to whom the Software is
17
 * furnished to do so, subject to the following conditions:
18
 *
19
 * The above copyright notice and this permission notice shall be included in all
20
 * copies or substantial portions of the Software.
21
 *
22
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28
 * SOFTWARE.
29
 */
30
31
/**
32
 *  @file Password.php
33
 *
34
 *  Validate the password strength
35
 *
36
 *  @package    Platine\Validator\Rule
37
 *  @author Platine Developers Team
38
 *  @copyright  Copyright (c) 2020
39
 *  @license    http://opensource.org/licenses/MIT  MIT License
40
 *  @link   https://www.platine-php.com
41
 *  @version 1.0.0
42
 *  @filesource
43
 */
44
45
declare(strict_types=1);
46
47
namespace Platine\Validator\Rule;
48
49
use Platine\Validator\RuleInterface;
50
use Platine\Validator\Validator;
51
52
class Password implements RuleInterface
53
{
54
    /*
55
     * The error type list
56
     */
57
    public const ERROR_TYPE_LENGTH = 1;
58
    public const ERROR_TYPE_UPPERCASE = 2;
59
    public const ERROR_TYPE_LOWERCASE = 3;
60
    public const ERROR_TYPE_NUMBER = 4;
61
    public const ERROR_TYPE_SPECIAL_CHAR = 5;
62
63
    /**
64
     * The password strength rules
65
     * @var array<string, bool|int>
66
     */
67
    protected array $rules = [];
68
69
    /**
70
     * The error type
71
     * @var int
72
     */
73
    protected int $errorType = self::ERROR_TYPE_LENGTH;
74
75
    /**
76
     * Constructor
77
     * @param array<string, bool|int> $rules
78
     */
79
    public function __construct(array $rules = [])
80
    {
81
        $this->rules = array_merge([
82
            'length' => 5,
83
            'uppercase' => false,
84
            'lowercase' => false,
85
            'number' => false,
86
            'special_chars' => false,
87
           ], $rules);
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     * @see RuleInterface
93
     */
94
    public function validate(string $field, $value, Validator $validator): bool
95
    {
96
        if (empty($value)) {
97
            return true;
98
        }
99
100
        $rules = $this->rules;
101
102
        if (strlen($value) < $rules['length']) {
103
            $this->errorType = self::ERROR_TYPE_LENGTH;
104
105
            return false;
106
        }
107
108
        if ($rules['uppercase'] && ((bool)preg_match('~[A-Z]~', $value)) === false) {
109
            $this->errorType = self::ERROR_TYPE_UPPERCASE;
110
111
            return false;
112
        }
113
114
        if ($rules['lowercase'] && ((bool)preg_match('~[a-z]~', $value)) === false) {
115
            $this->errorType = self::ERROR_TYPE_LOWERCASE;
116
117
            return false;
118
        }
119
120
        if ($rules['number'] && ((bool)preg_match('~[0-9]~', $value)) === false) {
121
            $this->errorType = self::ERROR_TYPE_NUMBER;
122
123
            return false;
124
        }
125
126
        if ($rules['special_chars'] && ((bool)preg_match('~[^\w]~', $value)) === false) {
127
            $this->errorType = self::ERROR_TYPE_SPECIAL_CHAR;
128
129
            return false;
130
        }
131
132
        return true;
133
    }
134
135
    /**
136
     * {@inheritdoc}
137
     * @see RuleInterface
138
     */
139
    public function getErrorMessage(string $field, $value, Validator $validator): string
140
    {
141
        if ($this->errorType === self::ERROR_TYPE_LENGTH) {
142
            return $validator->translate(
143
                '%s must contain at least %d characters!',
144
                $validator->getLabel($field),
145
                $this->rules['length']
146
            );
147
        }
148
149
        $errorMaps = [
150
            self::ERROR_TYPE_LOWERCASE => $validator->translate(
151
                '%s should include at least one lower case!',
152
                $validator->getLabel($field)
153
            ),
154
            self::ERROR_TYPE_UPPERCASE => $validator->translate(
155
                '%s should include at least one upper case!',
156
                $validator->getLabel($field)
157
            ),
158
            self::ERROR_TYPE_NUMBER => $validator->translate(
159
                '%s should include at least one number!',
160
                $validator->getLabel($field)
161
            ),
162
            self::ERROR_TYPE_SPECIAL_CHAR => $validator->translate(
163
                '%s should include at least one special character!',
164
                $validator->getLabel($field)
165
            ),
166
        ];
167
168
        return $errorMaps[$this->errorType] ?? '';
169
    }
170
}
171