PHP code example of turahe / number-validator

1. Go to this page and download the library: Download turahe/number-validator library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

turahe / number-validator example snippets



use Turahe\Validator\NIK;

$nik = NIK::set('3273012501990001');
$result = $nik->parse();

if ($result->valid) {
    echo "Gender: " . $result->gender . "\n";
    echo "Born: " . $result->born->full . "\n";
    echo "Age: " . $result->age->year . " years\n";
    echo "Zodiac: " . $result->zodiac . "\n";
    echo "Province: " . $result->address->province . "\n";
    echo "City: " . $result->address->city . "\n";
    echo "Sub-district: " . $result->address->subDistrict . "\n";
    echo "Postal Code: " . $result->postalCode . "\n";
    echo "Unique Code: " . $result->uniqueCode . "\n";
}


use Turahe\Validator\KK;

$kk = KK::set('3273012501990001');
$result = $kk->parse();

if ($result->valid) {
    echo "Formatted: " . $kk->getFormattedNumber() . "\n";
    echo "Province: " . $result->address->province . "\n";
    echo "City: " . $result->address->city . "\n";
    echo "Sub-district: " . $result->address->subDistrict . "\n";
    echo "Postal Code: " . $result->postalCode . "\n";
}


use Turahe\Validator\NIK;

try {
    $nik = NIK::set('123456789012345'); // Too short
} catch (\InvalidArgumentException $e) {
    echo "Error: " . $e->getMessage() . "\n";
}

// Get detailed validation errors
$nik = NIK::set('1234567890123456'); // Valid format but invalid data
$errors = $nik->getValidationErrors();
print_r($errors);


use Turahe\Validator\NIK;

$nik = NIK::set('3273012501990001');
$array = $nik->toArray();

echo json_encode($array, JSON_PRETTY_PRINT);


use Turahe\Validator\NIK;

// Both string and integer inputs work
$nik1 = NIK::set('3273012501990001');  // string
$nik2 = NIK::set(3273012501990001);    // integer