PHP code example of yomo / south-african-idnumber-generator

1. Go to this page and download the library: Download yomo/south-african-idnumber-generator 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/ */

    

yomo / south-african-idnumber-generator example snippets


class UserFactory extends Factory
{
    /**
     * Define the model's default state.
     *
     * @return array<string, mixed>
     */
    public function definition(): array
    {
        return [
            'name' => $name,
            'email' => $this->faker->safeEmailAddress(),
            'rsa_id_number' => $this->faker->southAfricanIdNumber(),  # Completely random
            'email_verified_at' => Carbon::now(),
            'password' => bcrypt('12345'),
            'remember_token' => Str::random(10),
            'created_at' => Carbon::now(),
            'updated_at' => Carbon::now(),
            'last_accessed_at' => Carbon::now(),
            'is_active' => $this->faker->boolean(),
            'settings_json' => $this->faker->words(),
        ];
    }
}
 
$this->faker->southAfricanIdNumber('1982-08-01', 'm');

$this->faker->southAfricanIdNumberFemale()

$this->faker->southAfricanIdNumberMale(Carbon::parse('1985-01-09'))

use Illuminate\Support\Carbon;
use Nonsapiens\SouthAfricanIdNumberFaker;

// Example: Generate an ID for a male South African citizen born on 15 August 1995, using Carbon
$dateOfBirth = Carbon::createFromDate(1995, 8, 15);
$idNumber1 = RsaIdNumber::generateRsaIdNumber($dateOfBirth, 'm', true);

// Example: Generate for a female, random date of birth, non-citizen
$idNumber2 = RsaIdNumber::generateRsaIdNumber(null, 'f', false);

$idNumber = new RsaIdNumber('8208015009088');     # This will also accept ID numbers with spaces
if ($idNumber->isValid()) {
    echo "This ID number is {$idNumber->gender()}";
} else {
    echo 'Invalid ID number';
}

$idNumber->dateOfBirth();
$idNumber->age();
$idNumber->isAdult();
$idNumber->isCitizen();
$idNumber->isPermanentResident();

$idNumber = new RsaIdNumber('7504220045086');
echo $idNumber->toNatural();     // Echoes as "750422 0045 08 6"