PHP code example of ernestochapon / rif-php

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

    

ernestochapon / rif-php example snippets




rnestoChapon\Rif\Rif;

// Validación simple
if (Rif::isValid('V113502963')) {
    echo "RIF válido!";
}

// Validación con manejo de excepciones
try {
    $rif = Rif::create('J000029679');
    echo "RIF: " . $rif->getRaw();
    echo "Tipo: " . $rif->getType()->getDescription();
    echo "Número: " . $rif->getNumber();
    echo "Dígito verificador: " . $rif->getCheckDigit();
} catch (ErnestoChapon\Rif\Exceptions\RifValidationException $e) {
    echo "Error: " . $e->getMessage();
}

// Validación de RIFs conocidos
$knownRifs = [
    'V113502963', // RIF personal
    'G200001100', // Banco Central de Venezuela
    'J000029679', // Banco Provincial
];

foreach ($knownRifs as $rifString) {
    if (Rif::isValid($rifString)) {
        echo "$rifString es válido\n";
    }
}



use ErnestoChapon\Rif\Rif;
use ErnestoChapon\Rif\Formatters\RifFormatter;

$rif = Rif::create('J000029679');

// Diferentes formatos disponibles
echo RifFormatter::standard($rif);        // J-00002967-9
echo RifFormatter::spaced($rif);          // J 00 002 967 9
echo RifFormatter::withDescription($rif); // J-00002967-9 (Persona Jurídica)
echo RifFormatter::dotted($rif);          // J-2.967-9
echo RifFormatter::legal($rif);           // R.I.F. J-00002967-9

// Método de conveniencia
echo $rif->format('standard');            // J-00002967-9
echo $rif->format('spaced');              // J 00 002 967 9



use ErnestoChapon\Rif\Rif;
use ErnestoChapon\Rif\Types\RifType;

// Generar un RIF aleatorio
$rif = Rif::generate();
echo $rif->getRaw(); // Ej: V123456789

// Generar un tipo específico
$rif = Rif::generate(RifType::LEGAL);
echo $rif->getRaw(); // Ej: J987654321

// Generar múltiples RIFs
$rifs = Rif::generateMultiple(5);
foreach ($rifs as $rif) {
    echo $rif->format() . "\n";
}

// Generar RIF secuencial (útil para testing)
$rif = Rif::generateSequential(42, RifType::NATURAL);
echo $rif->getRaw(); // V00000042X

// Usar el generador directamente
use ErnestoChapon\Rif\Services\RifGenerator;

$rif = RifGenerator::generateOneOfEachType();
foreach ($rif as $type => $rifInstance) {
    echo "{$type}: {$rifInstance->format()}\n";
}



use ErnestoChapon\Rif\Rif;
use ErnestoChapon\Rif\Validators\FormatValidator;

// Validar estructura completa (pero sin dígito verificador)
if (Rif::isValidFormat('J123456789')) {
    echo "Formato válido";
}

// Validar usando el validador directamente
if (FormatValidator::validateStructure('V113502963')) {
    echo "Estructura válida";
}

// Validación parcial (prefijo y cuerpo)
if (FormatValidator::validatePartial('J123456789')) {
    echo "Prefijo y cuerpo válidos";
}

// Validar componentes individuales
if (FormatValidator::isValidPrefix('V')) {
    echo "Prefijo válido";
}

if (FormatValidator::isValidBody('12345678')) {
    echo "Cuerpo válido";
}

// Extraer el tipo de RIF
$type = FormatValidator::extractType('J123456789');
if ($type) {
    echo "Tipo: " . $type->getDescription();
}

// Obtener detalles de validación para feedback al usuario
$details = Rif::validateFormat('X12A');
if (!$details['is_valid']) {
    foreach ($details['errors'] as $error) {
        echo "Error: $error\n";
    }
    foreach ($details['suggestions'] as $suggestion) {
        echo "Sugerencia: $suggestion\n";
    }
}
bash
composer 
bash
php -r "