PHP code example of freshwork / chilean-bundle

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

    

freshwork / chilean-bundle example snippets


'providers' => [
    ...
    Freshwork\ChileanBundle\Laravel\ChileanBundleServiceProvider::class
];

'aliases' => [
    ...
    'Rut'   => Freshwork\ChileanBundle\Laravel\Facades\Rut::class
];


use Freshwork\ChileanBundle\Rut;

$rut = new Rut('11.111.111', '1');
$rut->validate(); //true

(new Rut('12345678', '5'))->validate(); //true


    Rut::parse('11.111.111-1')->validate(); //true
    Rut::parse('11111111-1')->validate(); //true
    Rut::parse('12.345.678-5')->validate(); //true
    Rut::parse('123456785')->validate(); //true
    Rut::parse('1.23.45.6.7.8-5')->validate(); //true. It escapes all the dots and dashes.

Rut::set('10.123.123', '5'); //return true

Rut::parse('12345678-5')->isValid(); //true

Rut::parse('12.345.678-9')->validate(); //false

Rut::parse('12.3k5.6L8-9')->validate(); //throw Freshwork\ChileanBundle\Exceptions\InvalidFormatException
Rut::set('12.345.678')->validate(); // throw exception. We didn't set the verification number

Rut::parse('12.3k5.6L8-9')->quiet()->validate(); //return false. No exception

Rut::set('12.345.678')->calculateVerificationNumber(); //return 5
Rut::set('12.345.678-9')->calculateVerificationNumber(); //return 5
Rut::parse('12.345.678-9')->calculateVerificationNumber(); //return 5

Rut::parse('123456789')->format(); //return 12.345.678-9. It doesn't validates. It just formats.
Rut::parse('123456785')->format(Rut::FORMAT_COMPLETE); //return 12.345.678-5.
Rut::parse('123456785')->format(Rut::FORMAT_WITH_DASH); //return 12345678-5.
Rut::parse('123456785')->format(Rut::FORMAT_ESCAPED); //return 123456785.
Rut::parse('12.345.678-5')->format(Rut::FORMAT_ESCAPED); //return 123456785.

Rut::parse('12.345.678-5')->normalize(); //return '123456785'

Rut::parse('12.345.678-5')->toArray(); //return ['12345678', '5']

Rut::parse('12.345.678-9')->fix()->format(); //return '12.345.678-5'

//Set a new rut without setting any verification number
Rut::set('12345678')->fix()->format(); //return '12.345.678-5'

//Set a new rut with an invalid verification number that will be replaced
Rut::set('12345678', '6')->fix()->format(); //return '12.345.678-5'

Rut::parse('12.345.678-9')->validate(); //return false
Rut::parse('12.345.678-9')->fix()->validate(); //return true

//Getter
Rut::parse('12.345.678-9')->vn(); //return '9'
Rut::parse('12.345.678-9')->number(); //return '12345678'

//Setter
Rut::parse('12.345.678-9')->vn('7')->format(); //return '12.345.678-7'
Rut::parse('12.345.678-9')->number('11111111')->format(); //return '11.111.111-9'
Rut::set()->number('12.345.678')->vn('9')->format() //return '12.345.678-9'

...
class ClientController extends Controller{
    ...
    use ValidateRequests;
    ...
    public function store(Request $request) {
        $this->validate($request, [
            `nombre' => '
 


use \Freshwork\ChileanBundle\Rut;

//We loop 10 times
for($i = 0; $i < 10; $i++)
{
    //generate random number between 1.000.000 and 25.000.000
    $random_number = rand(1000000, 25000000);

    //We create a new RUT wihtout verification number (the second paramenter of Rut constructor)
    $rut = new Rut($random_number);

    //The fix method calculates the verification number
    echo $rut->fix()->format() . " \n";
}
//Output (random)

17.062.139-5
18.815.969-9
14.287.543-8
13.864.006-K
16.724.081-K
20.465.345-3
13.294.672-8
11.102.906-7
8.333.479-7
7.661.557-8


for($i = 0; $i < 10; $i++)
    echo \Freshwork\ChileanBundle\Rut::set(rand(1000000, 25000000))->fix()->format() . "\n";