Completed
Push — master ( 21ca38...9d386c )
by Jacques
11:01
created

Valitron   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 10
lcom 0
cbo 3
dl 0
loc 82
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C addrules() 0 79 10
1
<?php
2
/**
3
 * Validations reworked for use with Valitron.
4
 *
5
 * @author Jacques Marneweck <[email protected]>
6
 * @copyright 2002-2018 Jacques Marneweck.  All rights strictly reserved.
7
 */
8
9
namespace Jacques\Validators;
10
11
use Carbon\Carbon;
12
13
class Valitron
14
{
15
    static public function addrules()
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
16
    {
17
        /**
18
         * Validate the possibility of the South African Identity Number beng a valid South
19
         * African identity number.
20
         */
21
        \Valitron\Validator::addRule('za_identity_number', function($field, $value, array $params, array $fields) {
0 ignored issues
show
Unused Code introduced by
The parameter $params is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $fields is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
22
            if (!ctype_digit($value)) {
23
                return false;
24
            }
25
26
            $match = preg_match("!^(\d{2})(\d{2})(\d{2})\d\d{6}$!", $value, $matches);
27
            if (!$match) {
28
                return false;
29
            }
30
            list (, $year, $month, $day) = $matches;
0 ignored issues
show
Unused Code introduced by
The assignment to $year is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
Unused Code introduced by
The assignment to $month is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
Unused Code introduced by
The assignment to $day is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
31
32
            /**
33
             * Check citizenship of the users id (0 = .za, 1 = permanent resident)
34
             */
35
            if (!in_array($value{10}, array(0, 1))) {
36
                return false;
37
            }
38
39
            /**
40
             * Seen 8 or 9 here.
41
             */
42
            if (!in_array($value{11}, [8, 9])) {
43
                return false;
44
            }
45
46
            $idvalid = \PayBreak\Luhn\Luhn::validateNumber($value);
47
48
            return ($idvalid);
49
        }, 'must be a valid South African Identity Number');
50
51
        /**
52
         * Validate the possibility of a mobile number being a valid South African Mobile Number.
53
         */
54
        \Valitron\Validator::addRule('za_mobile_number', function($field, $value, array $params, array $fields) {
0 ignored issues
show
Unused Code introduced by
The parameter $params is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $fields is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
55
            $msisdn = str_replace(array(' ', '-', '(', ')'), '', $value);
56
            $country = 'ZA';
57
58
            $phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance();
59
            try {
60
                $phoneNumberProto = $phoneUtil->parse($msisdn, $country);
61
            } catch (\libphonenumber\NumberParseException $e) {
62
                return false;
63
            }
64
65
            if (!$phoneUtil->isValidNumber($phoneNumberProto)) {
66
                return false;
67
            }
68
69
            if (
70
                !in_array(
71
                    $phoneUtil->getNumberType($phoneNumberProto),
72
                    [
73
                        1,
74
                        2,
75
                    ]
76
                )
77
            ) {
78
                return false;
79
            }
80
81
            if (
82
                !is_null($country)
83
            ) {
84
                if ($country == $phoneUtil->getRegionCodeForNumber($phoneNumberProto)) {
85
                    return true;
86
                }
87
88
                return false;
89
            }
90
91
            return true;
92
        }, 'must be a valid South African Mobile Number');
93
    }
94
}
95