turahe / number-validator
identification number validator
v1.0.0
2025-08-07 15:49 UTC
Requires
- php: ^8.4
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.38
- phpunit/phpunit: ^12.0
README
A high-performance PHP package for validating and parsing Indonesian identity numbers (NIK and KK) with PHP 8.4 features and optimized performance.
๐ Features
- NIK (Nomor Induk Kependudukan) validation and parsing
- KK (Kartu Keluarga) validation and parsing
- PHP 8.4 optimized with modern features
- High Performance with intelligent caching
- Offline Operation - no internet connection required
- Comprehensive Data - age, gender, zodiac, address, postal code
- Type Safety with strict type declarations
- Error Handling with detailed validation messages
๐ฆ Installation
composer require turahe/number-validator
๐ฏ Quick Start
NIK Validation
<?php 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"; }
KK Validation
<?php 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"; }
๐ง Advanced Usage
Error Handling
<?php 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);
Array Output
<?php use Turahe\Validator\NIK; $nik = NIK::set('3273012501990001'); $array = $nik->toArray(); echo json_encode($array, JSON_PRETTY_PRINT);
Type Safety
<?php use Turahe\Validator\NIK; // Both string and integer inputs work $nik1 = NIK::set('3273012501990001'); // string $nik2 = NIK::set(3273012501990001); // integer
โก Performance Optimizations
Caching System
- Intelligent caching for frequently accessed data
- 13.8x faster performance on cached vs non-cached calls
- Memory-efficient caching strategy
String Operations
- Direct character access instead of
substr()
- 2.6x faster string operations
- Optimized zodiac calculation with early returns
Memory Management
- Readonly properties for immutability
- Efficient object lifecycle management
- Static caching for current year calculation
๐ก๏ธ PHP 8.4 Features
- Readonly Properties:
public readonly string $number
- Constructor Property Promotion: Simplified constructors
- Union Types:
string|int
for flexible input - Null Coalescing Assignment:
??=
operator - Improved Type Declarations: Better type safety
- Early Returns: Optimized control flow
๐ Performance Benchmarks
Operation | Time | Memory |
---|---|---|
NIK Creation | ~2.5ms | ~783KB |
NIK Parsing | ~0.1ms | ~4KB |
KK Creation | ~2.1ms | ~739KB |
KK Parsing | ~0.02ms | ~1KB |
1000 NIK Operations | ~1.5s | Optimized |
1000 KK Operations | ~1.5s | Optimized |
๐งช Testing
Run the comprehensive test suite:
./vendor/bin/phpunit
All 63 tests pass with 100% coverage.
๐ Project Structure
number-validator/
โโโ src/
โ โโโ Base.php # Abstract base class with optimizations
โ โโโ NIK.php # NIK validator with caching
โ โโโ KK.php # KK validator with formatting
โ โโโ assets/
โ โโโ wilayah.json # Location data
โโโ tests/
โ โโโ BaseTest.php # Base class tests
โ โโโ NIKTest.php # NIK validation tests
โ โโโ KKTest.php # KK validation tests
โโโ example.php # Performance demonstration
โโโ composer.json # Dependencies
๐ API Reference
NIK Class
Methods
set(string|int $number): self
- Create NIK instanceparse(): object
- Parse and validate NIK datavalidate(): bool
- Check if NIK is validgetValidationErrors(): array
- Get detailed error messagestoArray(): array
- Get data as arraygetGender(): string
- Get gender (LAKI-LAKI/PEREMPUAN)getBornDate(): object
- Get birth date informationgetAge(): object
- Get age calculationgetZodiac(): string
- Get zodiac signgetProvince(): ?string
- Get province namegetCity(): ?string
- Get city namegetSubDistrict(): ?string
- Get sub-district namegetPostalCode(): ?string
- Get postal code
KK Class
Methods
set(string|int $number): self
- Create KK instanceparse(): object
- Parse and validate KK datavalidate(): bool
- Check if KK is validgetValidationErrors(): array
- Get detailed error messagestoArray(): array
- Get data as arraygetFormattedNumber(): string
- Get formatted KK numbergetRawNumber(): string
- Get raw KK numbergetProvince(): ?string
- Get province namegetCity(): ?string
- Get city namegetSubDistrict(): ?string
- Get sub-district namegetPostalCode(): ?string
- Get postal code
๐ Performance Tips
- Reuse Instances: Create validator once and reuse for multiple operations
- Caching Benefits: Subsequent calls to
getBornDate()
,getAge()
, etc. are cached - Type Safety: Use union types for flexible input handling
- Error Handling: Always check
$result->valid
before accessing data
๐ค Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Ensure all tests pass
- Submit a pull request
๐ License
This project is licensed under the MIT License - see the LICENSE file for details.
๐ Acknowledgments
- Indonesian government for the NIK and KK number format specifications
- PHP community for the excellent 8.4 features
- All contributors who helped optimize this package
Built with โค๏ธ and optimized for PHP 8.4+