randomType()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
use Helip\NISS\Helpers\NISSGeneratorHelper;
4
use Helip\NISS\NISS;
5
6
// Show all errors
7
ini_set('display_errors', 1);
8
ini_set('display_startup_errors', 1);
9
error_reporting(E_ALL);
10
11
require_once __DIR__ . '/../vendor/autoload.php';
12
13
main();
14
15
function main(): void
16
{
17
    // Generates a random NISS numbers
18
    for ($i = 0; $i < 10; $i++) {
19
        // Generates random infos
20
        $birthDate = randomBirthdate();
21
        $order_number = random_int(1, 999);
22
        $gender = ($order_number % 2 === 0 ? NISS::GENDER_FEMALE : NISS::GENDER_MALE);
23
        $type = randomType();
24
25
        // Generates a NISS number
26
        $generatedNiss = NISSGeneratorHelper::generate($birthDate, $gender, null, $type);
27
28
        // Adds random chars to the NISS number to simulate random formatting
29
        $generatedNiss = insertRandomChars($generatedNiss, random_int(0, 10));
30
31
        // Instantiates a Niss object
32
        $niss = new NISS($generatedNiss);
33
34
        echo '================================================' . PHP_EOL;
35
        echo 'Gen. Birthdate:    ' . $birthDate->format('Y-m-d') . PHP_EOL;
36
        echo 'Gen. NISS:         ' . $generatedNiss . PHP_EOL;
37
        echo '------------------------------------------------' . PHP_EOL;
38
        echo 'Formatted NISS:    ' . $niss->getFormattedNiss() . PHP_EOL;
39
        echo 'Birthdate (Y-m-d): ' . ($niss->getBirthdate() ? $niss->getBirthdate()->format('Y-m-d') : 'Unknown') . PHP_EOL;
40
        echo 'Gender:            ' . $niss->getGender() . PHP_EOL;
41
        echo 'Type of NISS:      ' . $niss->getType() . PHP_EOL;
42
        echo 'Order number:      ' . $niss->getOrderNumber() . PHP_EOL;
43
        echo 'Control number:    ' . $niss->getControlNumber() . PHP_EOL;
44
        echo '================================================' . PHP_EOL . PHP_EOL;
45
    }
46
}
47
48
function randomBirthdate($age_min = 8, $age_max = 99): DateTime
49
{
50
    $current_year = date('Y');
51
52
    $max = strtotime($current_year - $age_max . '-01-01');
53
    $min = strtotime($current_year - $age_min . '-12-31');
54
55
    $val = rand($min, $max);
56
57
    return new DateTime(date('Y-m-d', $val));
58
}
59
60
function randomType(): string
61
{
62
    $type = [NISS::TYPE_REGULAR, NISS::TYPE_BIS, NISS::TYPE_UNOFFICIAL, NISS::TYPE_TER, NISS::TYPE_DOB_UNKNOWN];
63
    return $type[array_rand($type)];
64
}
65
66
// Adds random chars to a string
67
function insertRandomChars(string $str, int $numChars): string
68
{
69
    $chars = ['.', '-', ' ', '_', ':', ',', ';', '/', '@'];
70
    $n_chars = count($chars);
71
    $len = strlen($str);
72
73
    for ($i = 0; $i < $numChars; $i++) {
74
        $pos = mt_rand(0, $len);
75
        $char = $chars[mt_rand(0, $n_chars - 1)];
76
        $str = substr_replace($str, $char, $pos, 0);
77
    }
78
79
    return $str;
80
}
81