<commit_before># Laravel Phone Validator

Adds a phone validator to Laravel 4 based on the [PHP port](https://github.com/giggsey/libphonenumber-for-php) of [Google's libphonenumber API](https://code.google.com/p/libphonenumber/) by [giggsey](https://github.com/giggsey).

### Installation

1. In the `require` key of `composer.json` file add the following

    ```json
    "propaganistas/laravel-phone": "~1.0"
    ```

2. Run the Composer update comand

    ```bash
    $ composer update
    ```

3. In your `app/config/app.php` add `'Propaganistas\LaravelPhone\LaravelPhoneServiceProvider',` to the end of the `$providers` array

    ```php
    'providers' => [
        'Illuminate\Foundation\Providers\ArtisanServiceProvider',
        'Illuminate\Auth\AuthServiceProvider',
        ...
        'Propaganistas\LaravelPhone\LaravelPhoneServiceProvider',
    ],
    ```

4. In your `app/lang` add for each language an extra language line for the validator:

    ```php
    "phone" => "The :attribute field contains an invalid number.",
    ```

### Usage

To validate a field using the phone validator, use the `phone` keyword in your validation rules array. The phone validator is able to operate in two ways.

- You either specify *ISO 3166-1 compliant* country codes yourself as parameters for the validator, e.g.:

    ```php
    public static $rules = [
        'phonefield'  => 'phone:US,BE',
    ];
    ```

  The validator will check if the number is valid in at least one of provided countries, so feel free to add as many country codes as you like.

- Or you don't specify any parameters but you plug in a dedicated country input field (keyed by *ISO 3166-1 compliant* country codes) to allow end users to supply a country on their own. The easiest method by far is to install the [CountryList package by monarobase](https://github.com/Monarobase/country-list). The country field has to be named similar to the phone field but with `_country` appended:

    ```php
    public static $rules = [
        'phonefield'          => 'phone',
        'phonefield_country'  => 'required_with:phonefield',
    ];
    ```

  If using the CountryList package, you could then use the following in the form view:

    ```php
    {{ Form::text('phonefield') }}
    {{ Form::select('phonefield_country', Countries::getList(App::getLocale(), 'php', 'cldr')) }}
    ```

  Finally, if using a Presenter package like [robclancy/presenter](https://github.com/robclancy/presenter), you could add this to the Presenter to automatically format phone fields.

    ```php
    public function presentPhonefield()
    {
        $phone = $this->getObject()->phonefield;
        $country = $this->getObject()->phonefield_country; // Or supply a country code yourself...
    
        $phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance();
        $phoneProto = $phoneUtil->parse($phone, $country);
        return $phoneUtil->format($phoneProto, \libphonenumber\PhoneNumberFormat::INTERNATIONAL);
    }
    ```

NOTE: remember to update all occurences of *phonefield* (and *phonefield_country*) with their respective names.<commit_msg>Fix README indentation<commit_after># Laravel Phone Validator

Adds a phone validator to Laravel 4 based on the [PHP port](https://github.com/giggsey/libphonenumber-for-php) of [Google's libphonenumber API](https://code.google.com/p/libphonenumber/) by [giggsey](https://github.com/giggsey).

### Installation

1. In the `require` key of `composer.json` file add the following

    ```json
    "propaganistas/laravel-phone": "~1.0"
    ```

2. Run the Composer update comand

    ```bash
    $ composer update
    ```

3. In your `app/config/app.php` add `'Propaganistas\LaravelPhone\LaravelPhoneServiceProvider',` to the end of the `$providers` array

    ```php
    'providers' => [
        'Illuminate\Foundation\Providers\ArtisanServiceProvider',
        'Illuminate\Auth\AuthServiceProvider',
        ...
        'Propaganistas\LaravelPhone\LaravelPhoneServiceProvider',
    ],
    ```

4. In your `app/lang` add for each language an extra language line for the validator:

    ```php
    "phone" => "The :attribute field contains an invalid number.",
    ```

### Usage

To validate a field using the phone validator, use the `phone` keyword in your validation rules array. The phone validator is able to operate in two ways.

- You either specify *ISO 3166-1 compliant* country codes yourself as parameters for the validator, e.g.:

    ```php
    public static $rules = [
        'phonefield'  => 'phone:US,BE',
    ];
    ```

  The validator will check if the number is valid in at least one of provided countries, so feel free to add as many country codes as you like.

- Or you don't specify any parameters but you plug in a dedicated country input field (keyed by *ISO 3166-1 compliant* country codes) to allow end users to supply a country on their own. The easiest method by far is to install the [CountryList package by monarobase](https://github.com/Monarobase/country-list). The country field has to be named similar to the phone field but with `_country` appended:

    ```php
    public static $rules = [
        'phonefield'          => 'phone',
        'phonefield_country'  => 'required_with:phonefield',
    ];
    ```

  If using the CountryList package, you could then use the following in the form view:

    ```php
{{ Form::text('phonefield') }}
{{ Form::select('phonefield_country', Countries::getList(App::getLocale(), 'php', 'cldr')) }}
    ```

  Finally, if using a Presenter package like [robclancy/presenter](https://github.com/robclancy/presenter), you could add this to the Presenter to automatically format phone fields.

    ```php
public function presentPhonefield()
{
        $phone = $this->getObject()->phonefield;
        $country = $this->getObject()->phonefield_country; // Or supply a country code yourself...
        $phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance();
        $phoneProto = $phoneUtil->parse($phone, $country);
        return $phoneUtil->format($phoneProto, \libphonenumber\PhoneNumberFormat::INTERNATIONAL);
}
    ```

NOTE: remember to update all occurences of *phonefield* (and *phonefield_country*) with their respective names.
{'fraction_non_alphanumeric': 0.1150016061676839, 'fraction_numerical': 0.00546097012528108, 'mean_word_length': 3.5652492668621703, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 25, 'https://': 10, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7670640', 'n_tokens_mistral': 1799, 'n_tokens_neox': 1742, 'n_words': 625}