PHP code example of erajkhatiwada / imb

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

    

erajkhatiwada / imb example snippets




use Imb\IMB;

// Encode with de_id' => '01',
    'service_type' => '234',
    'mailer_id' => '567094',
    'serial_num' => '987654321',
]);
// Output: "ADFTATFTDTADTDAFF..." (65 characters)

// Encode with full routing information
$barcode = IMB::encode([
    'barcode_id' => '01',
    'service_type' => '234',
    'mailer_id' => '567094',
    'serial_num' => '987654321',
    'zip' => '12345',
    'plus4' => '6789',
    'delivery_pt' => '01',
]);



use Imb\IMB;

$result = IMB::decode('ADFTATFTDTADTDAFF...');

echo $result->data->barcodeId;    // "01"
echo $result->data->serviceType;  // "234"
echo $result->data->mailerId;     // "567094"
echo $result->data->serialNum;    // "987654321"
echo $result->data->zip;          // "12345" (if present)
echo $result->data->plus4;        // "6789" (if present)
echo $result->data->deliveryPt;   // "01" (if present)

// Convert to array
$array = IMB::decodeToArray('ADFTATFTDTADTDAFF...');



use Imb\IMB;

// Validate input data
$isValid = IMB::validate([
    'barcode_id' => '01',
    'service_type' => '234',
    'mailer_id' => '567094',
    'serial_num' => '987654321',
]);

// Validate barcode string
$isValid = IMB::validateBarcode('ADFTATFTDTADTDAFF...');



use Imb\IMB;

// Get the raw numeric string representation
$code = IMB::stringify([
    'barcode_id' => '00',
    'service_type' => '270',
    'mailer_id' => '103502',
    'serial_num' => '017955971',
    'zip' => '50310',
    'plus4' => '1605',
    'delivery_pt' => '15',
]);
// Output: "0027010350201795597150310160515" (31 digits)

// Without routing info (tracking only)
$code = IMB::stringify([
    'barcode_id' => '01',
    'service_type' => '234',
    'mailer_id' => '567094',
    'serial_num' => '987654321',
]);
// Output: "01234567094987654321" (20 digits)



use Imb\IMB;
use Imb\IMBData;

// Create data object directly
$data = new IMBData(
    '01',        // barcodeId
    '234',       // serviceType
    '567094',    // mailerId
    '987654321', // serialNum
    '12345'      // zip (optional)
);

$barcode = IMB::encode($data);

// Create from array (recommended)
$data = IMBData::fromArray([
    'barcode_id' => '01',
    'service_type' => '234',
    'mailer_id' => '567094',
    'serial_num' => '987654321',
]);



use Imb\Encoder;
use Imb\Decoder;

$encoder = new Encoder();
$decoder = new Decoder();

$barcode = $encoder->encode([...]);
$result = $decoder->decode($barcode);



use Imb\IMB;
use Imb\Exception\ValidationException;
use Imb\Exception\DecodingException;

try {
    $barcode = IMB::encode([...]);
} catch (ValidationException $e) {
    echo "Invalid input: " . $e->getMessage();
}

try {
    $result = IMB::decode('invalid-barcode');
} catch (DecodingException $e) {
    echo "Could not decode: " . $e->getMessage();
}



use Imb\IMB;

$result = IMB::decode($damagedBarcode);

if ($result->wasDamaged()) {
    echo "Barcode was damaged and repaired";
    echo "Suggested correction: " . $result->suggest;
    // $result->highlight shows which positions were corrected
}