Completed
Push — master ( 8a62f1...9df4dd )
by Jacques
56:42 queued 41:42
created

Identification   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 0
cbo 1
dl 0
loc 49
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C is_valid() 0 31 8
1
<?php
2
/**
3
 * Identification Validation
4
 *
5
 * @author    Jacques Marneweck <[email protected]>
6
 * @copyright 2015-2016 Jacques Marneweck.  All rights strictly reserved.
7
 * @license   MIT
8
 */
9
10
namespace Jacques\Validators;
11
12
use Carbon\Carbon;
13
14
class Identification {
15
    /**
16
     * Checks that a identification number is theoretically valid.  It does not
17
     * validate that the identification number actually belongs to a human.
18
     *
19
     * id_type is either:
20
     *   - 1 - South African Identification Number
21
     *   - 2 - Passport
22
     *   - 3 - South African Asylum Document Number
23
     *
24
     * @param string  $id_document_number Document Number of the document
25
     * @param integer $id_type Identification Document Type
26
     *
27
     * @throws \InvalidArgumentException
28
     *
29
     * @return bool   true if is theoretically valid else false
30
     */
31
    public static function is_valid($id_document_number = null, $id_type = null) {
32
        if (is_null($id_document_number)) {
33
            throw new \InvalidArgumentException('Please enter a valid id document number.');
34
        }
35
36
        if (is_null($id_type)) {
37
            throw new \InvalidArgumentException('Please pass in a valid id type for the id document number you are testing.');
38
        }
39
40
        if (!is_numeric($id_type)) {
41
            throw new \InvalidArgumentException('Please pass in a numeric value for the id type wanting to be tested.');
42
        }
43
44
        $id_type = (int)$id_type;
45
46
        if ($id_type < 1 || $id_type > 3) {
47
            throw new \InvalidArgumentException('Please enter a numeric value for the id type.  1 == ZA ID / 2 == Passport / 3 == ZA Asylum');
48
        }
49
50
        if (1 == $id_type) {
51
            if (!ctype_digit($id_document_number)) {
52
                return false;
53
            }
54
55
            $idvalid = \PayBreak\Luhn\Luhn::validateNumber($id_document_number);
56
57
            return ($idvalid);
58
        }
59
60
        return false;
61
    }
62
}
63