designbycode / south-african-id-validator
The South African ID Validator is a PHP class that validates and extracts information from South African ID numbers. It uses the Luhn Algorithm to validate the ID number and provides methods to determine the gender, citizenship, and birthdate of the individual.
Fund package maintenance!
designbycode
Requires
- php: ^8.1|^8.2|^8.3
- designbycode/luhn-algorithm: ^1.0
- nesbot/carbon: ^3.6
Requires (Dev)
- laravel/pint: ^1.0
- pestphp/pest: ^2.20
- spatie/ray: ^1.28
This package is auto-updated.
Last update: 2024-12-02 13:43:13 UTC
README
The South African ID Validator is a PHP class that validates and extracts information from South African ID numbers. It uses the Luhn Algorithm to validate the ID number and provides methods to determine the gender, citizenship, and birthdate of the individual.
Installation
You can install the package via composer:
composer require designbycode/south-african-id-validator
Usage
Instantiation
To use the South African ID Validator, create an instance of the SouthAfricanIdValidator
class:
$validator = new SouthAfricanIdValidator();
Validation
To validate an ID number, use the isValid
method:
$idNumber = '9404260051081'; if ($validator->isValid($idNumber)) { echo 'ID number is valid'; } else { echo 'ID number is invalid'; }
Parsing
To extract information from a valid ID number, use the parse
method:
$idNumber = '9404260051081'; $parsedData = $validator->parse($idNumber); print_r($parsedData);
Output:
Array
(
[valid] => 1
[birthday] => Arrya(
'default' => '1978-04-29',
'iso' => '1978-04-29',
'american' => '04/29/1978',
'european' => '04/29/1978',
'long' => 'April 29, 1978',
)
[age] => 28
[gender] => Male
[citizenship] => SA Citizen
)
Gender Determination
To determine the gender of the individual, use the isMale
or isFemale
methods:
$idNumber = '9404260051081'; if ($validator->isMale($idNumber)) { echo 'Male'; } else { echo 'Female'; }
Citizenship Determination
To determine the citizenship of the individual, use the isSACitizen
or isPermanentResident
methods:
$idNumber = '9404260051081'; if ($validator->isSACitizen($idNumber)) { echo 'SA Citizen'; } else { echo 'Permanent Resident'; }
Methods
isValid(mixed $idNumber): bool
Validates the ID number using the Luhn Algorithm and checks if it has a length of 13 digits and is numeric.
isLength13(mixed $idNumber): bool
Checks if the ID number has a length of 13 digits.
isNumber(mixed $idNumber): bool
Checks if the ID number is numeric.
passesLuhnCheck(mixed $idNumber): bool
Validates the ID number using the Luhn Algorithm.
isMale(mixed $idNumber): bool
Determines if the ID number is for a male.
isFemale(mixed $idNumber): bool
Determines if the ID number is for a female.
isSACitizen(mixed $idNumber): bool
Determines if the ID number is for a South African citizen.
isPermanentResident(mixed $idNumber): bool
Determines if the ID number is for a permanent resident.
parse(mixed $idNumber): array
Parses the ID number and returns an array with the following information:
valid
: A boolean indicating if the ID number is valid.birthday
: The birthdate in the formatYYYY/MM/DD
.age
: The age of the individual.gender
: The gender of the individual (Male or Female).citizenship
: The citizenship status of the individual (SA Citizen or Permanent Resident).
Use Cases
Validating ID numbers in a registration form
Use the isValid
method to validate ID numbers in a registration form to ensure that only valid ID numbers are accepted.
Extracting information from ID numbers
Use the parse
method to extract information from ID numbers, such as birthdate, age, gender, and citizenship, to populate user profiles or perform analytics.
Determining gender and citizenship
Use the isMale
, isFemale
, isSACitizen
, and isPermanentResident
methods to determine the gender and
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.