PHP code example of darkghosthunter / rut-utils

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

    

darkghosthunter / rut-utils example snippets


 

use DarkGhostHunter\RutUtils\Rut;

// Create a RUT using its numbers and verification digit separately.
$rutA = new Rut('14328145', 0); 

// ...even if the RUT is malformed 
$rutB = new Rut(10000, 'foo'); 

 

use DarkGhostHunter\RutUtils\Rut;

// Create a RUT using its numbers and verification digit separately.
$rutA = Rut::make('14328145', 0);

// You can also use a whole string.
$rutB = Rut::make('14.328.145-0');

// And even malformed ones with invalid characters
$rutC = Rut::make('asdwdasd14.32.814.5-0');

 

use DarkGhostHunter\RutUtils\Rut;

$malformed = Rut::make('not-a-rut');

if (!$malformed) {
    echo 'This RUT is bad!';
}

 

use DarkGhostHunter\RutUtils\Rut;

$validA = Rut::make('14328145', 0, 'this is valid');
echo $validA; // "14.328.145-0"

$validB = Rut::make('14.328.145-0', function () {
    return 'also valid'; 
});
echo $validB; // "14.328.145-0"

$invalid = Rut::make('18.765.432-1', null, 'this is invalid');
echo $invalid; // "this is invalid"

 

use DarkGhostHunter\RutUtils\Rut;

$validA = Rut::makeOrThrow('18.765.432', 1);

// [!] [InvalidRutException]



use DarkGhostHunter\RutUtils\Rut;

// Create multiple RUTs
$rutsA = Rut::many('14.328.145-0', '14.328.145-0');

// Or issue an array of multiple RUTs
$rutsB = Rut::many([
    '14.328.145-0',
    '7976228-8',
    ['14.328.145', 0]
]);



use DarkGhostHunter\RutUtils\Rut;

// Let's create first the RUT:
$rut = Rut::make(14328145, 0);

// Return the RUT as a string
echo $rut; // 14.328.145-0

// You can get number or verification digit as an array
echo $rut['num']; // 14328145
echo $rut['vd']; // 0

// ...or as an object
echo $rut->num; // 14328145
echo $rut->vd; // 0



use DarkGhostHunter\RutUtils\Rut;

Rut::allLowercase();

echo Rut::make('12343580', 'K')->vd; // "k"

Rut::allUppercase();

echo Rut::make('12343580', 'K')->vd; // "K"



use DarkGhostHunter\RutUtils\Rut;

$rut = Rut::make('12343580', 'K');

$rut->lowercase();

echo $rut->vd; // "K"

$rut->uppercase();

echo $rut->vd; // "k"



use DarkGhostHunter\RutUtils\RutGenerator;

$rut = RutGenerator::make()->generate();

echo $rut; // "7.976.228-8" 



use DarkGhostHunter\RutUtils\RutGenerator;

echo $rut = RutGenerator::make()->asPerson()->generate();
// "15.846.327-K"

echo $company = RutGenerator::make()->asCompany()->generate();
// "54.029.467-4"



use DarkGhostHunter\RutUtils\RutGenerator;

$peopleRuts = RutGenerator::make()->asPerson()->generate(10);
$companyRuts = RutGenerator::make()->asCompany()->generate(35);



use DarkGhostHunter\RutUtils\RutGenerator;

$raw = RutGenerator::make()->asRaw()->generate(10);
$basic = RutGenerator::make()->asBasic()->generate(20);
$strict = RutGenerator::make()->asStrict()->generate(30);



use DarkGhostHunter\RutUtils\RutGenerator;

$ruts = RutGenerator::make()->withoutDuplicates()->generate(100000);



use DarkGhostHunter\RutUtils\RutGenerator;

$users = [
    ['name' => 'John'],
    ['name' => 'Clara'],
    ['name' => 'Mark'],
    // ... and other 99.997 records
];

$seeder = function ($user) {
    return array_merge($user, [
        'rut' => RutGenerator::make()->generateStatic()
    ]);
};

// Call the seeder
foreach ($users as $key => $user) {
    $users[$key] = $seeder($user);
}

// Flush the static array for the next seeder call
RutGenerator::make()->flushStatic();



use DarkGhostHunter\RutUtils\RutHelper;

echo RutHelper::cleanRut('f@a18765432@@7'); // "187654327"
echo RutHelper::cleanRut('18.290.743-K', false); // "18290743k"



use DarkGhostHunter\RutUtils\RutHelper;

RutHelper::separateRut('18.290.743-K', false);

// array(2) [
//    0 => 18290743,
//    1 => 'k',
// ]



use DarkGhostHunter\RutUtils\RutHelper;

echo RutHelper::validate('14328145-0'); // true
echo RutHelper::validate('14.328.145-0', '12343580-K'); // true
echo RutHelper::validate(143281450); // true
echo RutHelper::validate('not-a-rut'); // false
echo RutHelper::validate(['14.328.145-0', '12343580-K', 'foo']); // false



use DarkGhostHunter\RutUtils\Rut;

// Cleans the rut, and validate it 
echo Rut::make('14328145-0')->isValid(); // true
echo Rut::make('94.328.145-0')->isValid(); // false
echo Rut::make('cleanthis14328145-0-andthistoo')->isValid(); // true



use DarkGhostHunter\RutUtils\RutHelper;

// Receive the raw string, and strictly validate it
echo RutHelper::validateStrict('14328145-0'); // false
echo RutHelper::validateStrict('14.328.145-0', '12343580-K'); // false
echo RutHelper::validateStrict(143281450); // false
echo RutHelper::validateStrict('not-a-rut'); // false
echo RutHelper::validateStrict(143281450, 'not-a-rut'); // false
echo RutHelper::validateStrict('14.328.1!45-0'); // false



use DarkGhostHunter\RutUtils\RutHelper;

// Filter only the valid ruts, and leave the invalid out of the result.
$rutsA = RutHelper::filter(
    '14328145-0', 
    '12343580-K', 
    '94.328.145-0', 
    'not-a-rut'
);

var_dump($rutsA);

// array(1) {
//     [0] => 14328145-0
//     [1] => 12343580-K
// } 

$rutsB = RutHelper::filter([
    '14328145-0',
    '12343580-K',
    '94.328.145-0',
    'not-a-rut'
]);

var_dump($rutsB);

// array(1) {
//     [0] => 14328145-0
//     [1] => 12343580-K
// } 



use DarkGhostHunter\RutUtils\RutHelper;

$rut = RutHelper::rectify('18765432');

echo $rut->num; // "18765432"
echo $rut->vd; // "7"



use DarkGhostHunter\RutUtils\RutHelper;

echo RutHelper::isPerson('22605071-k'); // true



use DarkGhostHunter\RutUtils\RutHelper;

echo RutHelper::make('22605071-k')->isPerson(); // true



use DarkGhostHunter\RutUtils\RutHelper;

echo RutHelper::isCompany('50000000-7'); // true



use DarkGhostHunter\RutUtils\RutHelper;

echo RutHelper::make('50000000-7')->isCompany(); // true



use DarkGhostHunter\RutUtils\RutHelper;
use Application\Models\User;

$ruts = RutHelper::isEqual(
    'thisisARut12343580-K',
    '12343580-K',
    User::getRutFromDatabase()
);

echo $ruts; // true



use DarkGhostHunter\RutUtils\Rut;
use Application\Models\User;

$rut = Rut::make(User::getRutFromDatabase());

echo $rut->isEqual('thisisARut12343580-K', '12343580-k'); // true



use DarkGhostHunter\RutUtils\RutHelper;

$vd = RutHelper::getVd(12343580);

echo $vd; // 'K'



use DarkGhostHunter\RutUtils\RutHelper;

$unpacked = RutHelper::unpack([
    ['12343580K', '22605071-k', 500000007],
]);

echo count($unpacked); // 3



use DarkGhostHunter\RutUtils\Rut;
use DarkGhostHunter\RutUtils\RutHelper;

Rut::after(function ($ruts) {
    
    return array_merge($ruts, [
        'count' => count($ruts),
    ]);
});

$ruts = Rut::many([
    // ...
]);

var_dump($ruts);
// array(100) [
//    // ...
//    'count' => 100,
// ]



use DarkGhostHunter\RutUtils\Rut;

$rut = Rut::make('22605071-k');

echo serialize($rut); // C:28:"DarkGhostHunter\RutUtils\Rut":9:{106663092}



use DarkGhostHunter\RutUtils\Rut;

$rut = Rut::make('22605071-k');

Rut::allFormatStrict(); 

echo (string)$rut; // "22.605.071-K"

Rut::allFormatBasic(); 

echo (string)$rut; // "22605071-K"

Rut::allFormatRaw(); 

echo (string)$rut; // "22605071K"

// Per instance
echo $rut->toStrictString(); // "22.605.071-K"
echo $rut->toBasicString(); // "22605071-K"
echo $rut->toRawString(); // "22605071K"



use DarkGhostHunter\RutUtils\Rut;

Rut::allJsonAsArray();

$rut = Rut::make('22605071-k');

echo json_encode($rut); // {"num":"22605071","vd":"K"}

Rut::allJsonAsString();

echo json_encode($rut); // "22.605.071-K"

$rut->jsonAsArray();

echo json_encode($rut); // {"num":"22605071","vd":"K"}

$rut->jsonAsString();

echo json_encode($rut); // "22.605.071-K"



$rut = rut('10.666.309-2');

echo $rut; // '10.666.309-2';

$rut = rut('an invalid rut', 'use this!');

echo $rut; // 'use this!'

$rut = rut()->generate();

echo $rut; // '20.750.456-4'