Completed
Push — development ( e80362...5ad269 )
by Claudio
02:41
created

Validation::filterUserName()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 3
nc 3
nop 1
1
<?php
2
3
namespace App\Helpers;
4
5
use App\Singleton;
6
use Illuminate\Support\Facades\Config;
7
8
/**
9
 * Class Validation
10
 * @package App\Helpers
11
 */
12
final class Validation extends Singleton
13
{
14
    /**
15
     * Filter an Username from the Invalid Names Base.
16
     *
17
     * @param string $username
18
     * @return bool
19
     */
20
    public function filterUserName(string $username): bool
21
    {
22
        return $this->checkSize($username, 4, 15) && $this->checkWords($username) &&
23
            !preg_match('/[^A-Za-z0-9]/', $username);
24
    }
25
26
    /**
27
     * Check String Size
28
     *
29
     * @param string $needle
30
     * @param int $min
31
     * @param int $max
32
     * @return bool
33
     */
34
    public function checkSize(string $needle, int $min, int $max)
35
    {
36
        return strlen($needle) <= $max && strlen($needle) >= $min;
37
    }
38
39
    /**
40
     * Check for Illegal Words
41
     *
42
     * @param string $needle
43
     * @return bool
44
     */
45
    public function checkWords(string $needle): bool
46
    {
47
        return count(array_filter(Config::get('chocolatey.invalid'), function ($illegal) use ($needle) {
48
                return stripos($needle, $illegal) !== false;
49
            })) == 0;
50
    }
51
}
52