Password   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getErrorMessage() 0 30 2
B validate() 0 38 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
/**
53
 * @class Password
54
 * @package Platine\Validator\Rule
55
 */
56
class Password implements RuleInterface
57
{
58
    /*
59
     * The error type list
60
     */
61
    public const ERROR_TYPE_LENGTH = 1;
62
    public const ERROR_TYPE_UPPERCASE = 2;
63
    public const ERROR_TYPE_LOWERCASE = 3;
64
    public const ERROR_TYPE_NUMBER = 4;
65
    public const ERROR_TYPE_SPECIAL_CHAR = 5;
66
67
    /**
68
     * The password strength rules
69
     * @var array<string, bool|int>
70
     */
71
    protected array $rules = [];
72
73
    /**
74
     * The error type
75
     * @var int
76
     */
77
    protected int $errorType = self::ERROR_TYPE_LENGTH;
78
79
    /**
80
     * Constructor
81
     * @param array<string, bool|int> $rules
82
     */
83
    public function __construct(array $rules = [])
84
    {
85
        $this->rules = array_merge([
86
            'length' => 5,
87
            'uppercase' => false,
88
            'lowercase' => false,
89
            'number' => false,
90
            'special_chars' => false,
91
           ], $rules);
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     * @see RuleInterface
97
     */
98
    public function validate(string $field, mixed $value, Validator $validator): bool
99
    {
100
        if (empty($value)) {
101
            return true;
102
        }
103
104
        $rules = $this->rules;
105
        if (strlen($value) < $rules['length']) {
106
            $this->errorType = self::ERROR_TYPE_LENGTH;
107
108
            return false;
109
        }
110
111
        if ($rules['uppercase'] && ((bool)preg_match('~[A-Z]~', $value)) === false) {
112
            $this->errorType = self::ERROR_TYPE_UPPERCASE;
113
114
            return false;
115
        }
116
117
        if ($rules['lowercase'] && ((bool)preg_match('~[a-z]~', $value)) === false) {
118
            $this->errorType = self::ERROR_TYPE_LOWERCASE;
119
120
            return false;
121
        }
122
123
        if ($rules['number'] && ((bool)preg_match('~[0-9]~', $value)) === false) {
124
            $this->errorType = self::ERROR_TYPE_NUMBER;
125
126
            return false;
127
        }
128
129
        if ($rules['special_chars'] && ((bool)preg_match('~[^\w]~', $value)) === false) {
130
            $this->errorType = self::ERROR_TYPE_SPECIAL_CHAR;
131
132
            return false;
133
        }
134
135
        return true;
136
    }
137
138
    /**
139
     * {@inheritdoc}
140
     * @see RuleInterface
141
     */
142
    public function getErrorMessage(string $field, mixed $value, Validator $validator): string
143
    {
144
        if ($this->errorType === self::ERROR_TYPE_LENGTH) {
145
            return $validator->translate(
146
                '%s must contain at least %d characters!',
147
                $validator->getLabel($field),
148
                $this->rules['length']
149
            );
150
        }
151
152
        $errorMaps = [
153
            self::ERROR_TYPE_LOWERCASE => $validator->translate(
154
                '%s should include at least one lower case!',
155
                $validator->getLabel($field)
156
            ),
157
            self::ERROR_TYPE_UPPERCASE => $validator->translate(
158
                '%s should include at least one upper case!',
159
                $validator->getLabel($field)
160
            ),
161
            self::ERROR_TYPE_NUMBER => $validator->translate(
162
                '%s should include at least one number!',
163
                $validator->getLabel($field)
164
            ),
165
            self::ERROR_TYPE_SPECIAL_CHAR => $validator->translate(
166
                '%s should include at least one special character!',
167
                $validator->getLabel($field)
168
            ),
169
        ];
170
171
        return $errorMaps[$this->errorType] ?? '';
172
    }
173
}
174