PHPackages                             pittacusw/chilean-bundle - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Validation &amp; Sanitization](/categories/validation)
4. /
5. pittacusw/chilean-bundle

ActiveLibrary[Validation &amp; Sanitization](/categories/validation)

pittacusw/chilean-bundle
========================

A PHP composer package with Chilean validations, common variables, etc. (RUT, IVA, ETC). Ready for Laravel 8.

2.1.3(7y ago)05MITPHPPHP &gt;=5.4.0

Since Aug 7Pushed 5y agoCompare

[ Source](https://github.com/PittacusW/ChileanBundle)[ Packagist](https://packagist.org/packages/pittacusw/chilean-bundle)[ RSS](/packages/pittacusw-chilean-bundle/feed)WikiDiscussions master Synced yesterday

READMEChangelogDependencies (2)Versions (19)Used By (0)

Chilean Bundle
==============

[](#chilean-bundle)

A PHP composer package with Chilean validations, common variables, etc. Viva Chile Mier...

This package includes:
----------------------

[](#this-package-includes)

- R.U.T. validation
- More functions coming soon...

Installation
------------

[](#installation)

### Step 1: Composer

[](#step-1-composer)

From the command line, run:

```
composer require freshwork/chilean-bundle

```

If you're not using Laravel, you are done.

### Step 2: Laravel Service Provider

[](#step-2-laravel-service-provider)

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
];
```

Usage
-----

[](#usage)

### The Basics

[](#the-basics)

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
```

### The `parse()` method

[](#the-parse-method)

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.
```

### The `set()` method

[](#the-set-method)

This is a shortcut for `(new Rut($number, $vn))`

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

### The `validate()` &amp; `isValid()` method

[](#the-validate--isvalid-method)

`validate()` is an alias of `isValid()`

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

### Invalid Formatted R.U.T.

[](#invalid-formatted-rut)

if the R.U.T. is invalid, it will return false

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

### Invalid Formatted R.U.T.

[](#invalid-formatted-rut-1)

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
```

##### `Quiet` mode

[](#quiet-mode)

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()`

### The `calculateVerificationNumber()` method

[](#the-calculateverificationnumber-method)

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:

### The `format()` method

[](#the-format-method)

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.
```

### The `normalize()` method

[](#the-normalize-method)

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'
```

### The `toArray()` method

[](#the-toarray-method)

Return the Rut object as string with a definied format.

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

### The `fix()` method

[](#the-fix-method)

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
```

### The `vn()` and `number()` method

[](#the-vn-and-number-method)

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'
```

Laravel
=======

[](#laravel)

### Validations

[](#validations)

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
    }
}
```

---

Examples
========

[](#examples)

RUT Generator

```
