PHP code example of trishanapp / lk-business-validator

1. Go to this page and download the library: Download trishanapp/lk-business-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/ */

    

trishanapp / lk-business-validator example snippets


use Trishanapp\LkBusinessValidator\LkValidator;

// NIC
LkValidator::nic('851234567V');       // true
LkValidator::nic('198512345678');     // true (new format)

// Phone
LkValidator::phone('0771234567');     // true
LkValidator::phone('+94771234567');   // true

// Business Registration
LkValidator::br('PV00123456');        // true
LkValidator::br('PV/00123456');       // true (slash accepted)

// Postal Code
LkValidator::postalCode('00100');     // true (Colombo)
LkValidator::postalCode('80000');     // true (Galle)

// VAT / TIN
LkValidator::vat('123456789-7000');   // true
LkValidator::tin('123456789');        // true

use Trishanapp\LkBusinessValidator\LkValidator;

// Validate (old format: 9 digits + V/X, new format: 12 digits)
LkValidator::nic('851234567V');       // true
LkValidator::nic('198512345678');     // true

// Parse — returns full details
LkValidator::nicParse('851234567V');
// [
//   'valid'      => true,
//   'format'     => 'old',
//   'birth_year' => 1985,
//   'gender'     => 'Male',
// ]

// Direct helpers
NicValidator::getBirthYear('851234567V');       // 1985
NicValidator::getGender('856234567V');          // 'Female' (day > 500)
NicValidator::convertToNew('851234567V');       // '198512304567'

// Validate — supports local, +94, and 94 prefixes
LkValidator::phone('0771234567');      // true
LkValidator::phone('+94771234567');    // true
LkValidator::phone('077 123 4567');    // true (spaces OK)

// Parse
LkValidator::phoneParse('0771234567');
// [
//   'valid'         => true,
//   'normalized'    => '0771234567',
//   'international' => '+94771234567',
//   'network'       => 'Dialog',
//   'type'          => 'mobile',
// ]

// Helpers
LkValidator::phoneNormalize('+94771234567');     // '0771234567'
LkValidator::phoneToInternational('0771234567'); // '+94771234567'

// Supported prefixes: PV, PB, HP, SP, PR, GN, SC
LkValidator::br('PV00123456');    // true — Private Limited
LkValidator::br('PR00123456');    // true — Partnership
LkValidator::br('PV/00123456');   // true — slash format OK

// Parse
LkValidator::brParse('PV00123456');
// [
//   'valid'        => true,
//   'normalized'   => 'PV00123456',
//   'prefix'       => 'PV',
//   'company_type' => 'Private Limited Company',
// ]

LkValidator::postalCode('00100');  // true — Colombo 1
LkValidator::postalCode('80000');  // true — Galle
LkValidator::postalCode('999');    // false — too short

// Parse with province detection
LkValidator::postalCodeParse('80000');
// [
//   'valid'    => true,
//   'code'     => '80000',
//   'province' => 'Southern',
// ]

LkValidator::vat('123456789-7000');  // true
LkValidator::vat('123456789');       // true (without suffix)
LkValidator::tin('123456789');       // true (9 digits)
LkValidator::tin('1234567890');      // true (10 digits)

// Parse
LkValidator::vatParse('123456789');
// [
//   'valid'      => true,
//   'type'       => 'TIN',
//   'normalized' => '123456789-7000',
// ]

$result = LkValidator::validateBatch([
    'nic'          => '851234567V',
    'phone'        => '0771234567',
    'br'           => 'PV00123456',
    'postal_code'  => '00100',
    'vat'          => '123456789-7000',
]);

// ['valid' => true, 'errors' => []]

$request->validate([
    'nic'          => [''],
    'br_number'    => [' => ['nullable', 'lk_vat'],
    'tin_number'   => ['nullable', 'lk_tin'],
]);

// lang/en/validation.php
'lk_nic'         => 'Please enter a valid Sri Lanka NIC number.',
'lk_phone'       => 'Please enter a valid Sri Lanka phone number.',
'lk_br'          => 'Please enter a valid Business Registration number.',
'lk_postal_code' => 'Please enter a valid Sri Lanka postal code.',
'lk_vat'         => 'Please enter a valid VAT registration number.',