Passed
Push — master ( 9e8075...242c87 )
by Dedipyaman
01:41
created

UsernameValidator::validate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
/*
3
 * This file is part of Phypes <https://github.com/2DSharp/Phypes>.
4
 *
5
 * (c) Dedipyaman Das <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
12
namespace Phypes\Validator;
13
14
use Phypes\Result\Result;
15
use Phypes\Rule\Aggregate\ForAll;
16
use Phypes\Rule\String\AlphaNumeric;
17
use Phypes\Rule\String\MaximumLength;
18
use Phypes\Rule\String\MinimumLength;
19
20
class UsernameValidator implements Validator
21
{
22
    /**
23
     * @var integer $minLength
24
     */
25
    private $minLength;
26
    /**
27
     * @var integer $maxLength
28
     */
29
    private $maxLength;
30
    /**
31
     * @var array $allowedChars
32
     */
33
    private $allowedSpecialChars = [];
34
35
    public function __construct(int $minLength = 4, int $maxLength = 12, $allowedSpecialChars = [])
36
    {
37
        $this->minLength = $minLength;
38
        $this->maxLength = $maxLength;
39
        $this->allowedSpecialChars = $allowedSpecialChars;
40
    }
41
42
    public function validate($username): Result
43
    {
44
        $rule = new ForAll(
45
            new AlphaNumeric($this->allowedSpecialChars),
46
            new MinimumLength($this->minLength),
47
            new MaximumLength($this->maxLength));
48
49
        return $rule->validate($username);
50
51
    }
52
}