Passed
Push — master ( f78a01...30a551 )
by Ion
03:10
created

AppServiceProvider::register()   B

Complexity

Conditions 6
Paths 1

Size

Total Lines 37
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 20
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 37
rs 8.9777
1
<?php
2
3
namespace App\Providers;
4
5
use Illuminate\Support\Facades\Validator;
6
use Illuminate\Support\ServiceProvider;
7
8
/**
9
 * Class AppServiceProvider
10
 *
11
 * @package App\Providers
12
 */
13
class AppServiceProvider extends ServiceProvider
14
{
15
    /**
16
     * Register any application services.
17
     */
18
    public function register()
19
    {
20
        /**
21
         * Validate alpha spaces
22
         */
23
        Validator::extend('name', function ($attribute, $value) {
24
            return preg_match('/^[\pL\s\']+$/u', $value);
25
        });
26
27
        /**
28
         * Validate phone number
29
         */
30
        Validator::extend('phone', function ($attribute, $value) {
31
            $conditions   = [];
32
            $conditions[] = strlen($value) >= 10;
33
            $conditions[] = strlen($value) <= 16;
34
            $conditions[] = preg_match('/[^\d]/i', $value) === 0;
35
36
            $isDigit = (bool)array_product($conditions);
37
38
            $isE123 = preg_match('/^(?:\((\+?\d+)?\)|\+?\d+) ?\d*(-?\d{2,3} ?){0,4}$/',
39
                    $value) === 1 && strlen($value) <= 16;
40
41
            $conditions   = [];
42
            $conditions[] = strpos($value, '+') === 0;
43
            $conditions[] = strlen($value) >= 9;
44
            $conditions[] = strlen($value) <= 16;
45
            $conditions[] = preg_match('/[^\d+]/i', $value) === 0;
46
47
            $isE164 = (bool)array_product($conditions);
48
49
            $conditions   = [];
50
            $conditions[] = preg_match('/^(?:\+1|1)?\s?-?\(?\d{3}\)?(\s|-)?\d{3}-\d{4}$/i', $value) > 0;
51
52
            $isNANP = (bool)array_product($conditions) && strlen($value) <= 16;
53
54
            return $isE123 || $isE164 || $isNANP || $isDigit;
55
        });
56
    }
57
}
58