A PHP composer package with Chilean validations, common variables, etc. Viva Chile Mier...
- R.U.T. validation
- More functions coming soon...
From the command line, run:
composer require freshwork/chilean-bundle
If you're not using Laravel, you are done.
If you're using Laravel 5.5 this package supports Auto-Discovery. So you can skip this step.
For Laravel 5.4 or below
Append this line to your providers
array in your config/app.php
file:
'providers' => [
...
Freshwork\ChileanBundle\Laravel\ChileanBundleServiceProvider::class
];
and you can add the RUT
Facade
'aliases' => [
...
'Rut' => Freshwork\ChileanBundle\Laravel\Facades\Rut::class
];
You can use the RUT object, but I recommend using the Rut::parse() method.
include('vendor/autoload.php'); //Enable composer autloading if not using laravel
use Freshwork\ChileanBundle\Rut;
$rut = new Rut('11.111.111', '1');
$rut->validate(); //true
(new Rut('12345678', '5'))->validate(); //true
Is the recommended way of using the object. It will automatically separate the Verification Number (Dígito verrificador) and the rest of the number.
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.
This is a shortcut for (new Rut($number, $vn))
Rut::set('10.123.123', '5'); //return true
validate()
is an alias of isValid()
Rut::parse('12345678-5')->isValid(); //true
if the R.U.T. is invalid, it will return false
Rut::parse('12.345.678-9')->validate(); //false
if the R.U.T. has a wrong format, it will throw an Freshwork\ChileanBundle\Exceptions\InvalidFormatException
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
You can prevent that the object throws an InvalidFormatException
, so validate()
will just return false, using the quiet()
mehtod.
Rut::parse('12.3k5.6L8-9')->quiet()->validate(); //return false. No exception
You can re-enable exceptions with use_exceptions()
You can get the correct verification number of a RUT. Note that the we are passing just one argument to the set()
method. We are not defining a verification number.
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
Esta clase se basa en está simple, pero eficiente función: http://www.dcc.uchile.cl/~mortega/microcodigos/validarrut/php.php
Return the Rut object as string with a definied format.
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.
If you are saving RUTs in the database, I recommend saving the normalized rut string. This method is an alias of ->format(Rut::FORMAT_ESCAPED)
Rut::parse('12.345.678-5')->normalize(); //return '123456785'
Return the Rut object as string with a definied format.
Rut::parse('12.345.678-5')->toArray(); //return ['12345678', '5']
Fix the current RUT. It will take the number part of the RUT (without verification number) and it will calculate what's the correct verification number for that RUT and then updates the verification number with this result.
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
You can set and get the RUT number and verification number with this methods. If no argumen are passed to this methods, it will return the corresponding value. Otherwise, it sets the value.
//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'
If you're using the service Provider, you can validate ruts usign the validation system of Laravel using the cl_rut
validation.
...
class ClientController extends Controller{
...
use ValidateRequests;
...
public function store(Request $request) {
$this->validate($request, [
`nombre' => 'required|min:2',
'email' => 'required|email',
'rut' => 'required|cl_rut'
]);
//Do stuff
}
}
RUT Generator
<?php
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
This generates ten random RUT's between 1.000.000 and 25.000.000
Even a shorter syntaxis:
<?php
for($i = 0; $i < 10; $i++)
echo \Freshwork\ChileanBundle\Rut::set(rand(1000000, 25000000))->fix()->format() . "\n";
Puedes usar este paquete gratuitamente sin ninguna restricción, aunque como está de moda, implementamos una licencia 'Postalware'. Si usas este paquete en producción y te gusta como funciona, Agradeceríamos bastante si nos envías una postal de tu ciudad/comuna, una nota de agradecimiento o un súper8.
Dirección Nombre: Gonzalo De Spirito Providencia 229, Providencia. Chile,
You can run the tests as ./vendor/bin/codecept run
Please, feel free to contact me at [email protected].-