My first commit of codes

This commit is contained in:
sujitprasad
2015-05-01 13:13:01 +05:30
parent 4c8e5096f1
commit a6e5a69348
8487 changed files with 1317246 additions and 0 deletions

View File

@@ -0,0 +1,30 @@
<?php namespace Propaganistas\LaravelPhone;
use Illuminate\Support\ServiceProvider;
class LaravelPhoneServiceProvider extends ServiceProvider
{
/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = false;
/**
* Bootstrap the application events.
*
* @return void
*/
public function boot()
{
$this->app['validator']->extend('phone', 'Propaganistas\LaravelPhone\Validator@phone');
}
/**
* Register the service provider.
*
* @return void
*/
public function register() {}
}

View File

@@ -0,0 +1,54 @@
<?php namespace Propaganistas\LaravelPhone;
class Validator
{
/**
* Validates a phone number field using libphonenumber.
*/
public function phone($attribute, $value, $parameters, $validator)
{
$data = $validator->getData();
// Check if we should validate using a default country or a *_country field.
if (!empty($parameters)) {
$countries = $parameters;
}
elseif (isset($data[$attribute.'_country'])) {
$countries = array($data[$attribute.'_country']);
}
else {
return FALSE;
}
// Filter out invalid countries.
foreach ($countries as $key => $country) {
if (!$this->phone_country($country)) {
unset($countries[$key]);
}
}
// Now try each country during validation.
foreach ($countries as $country) {
$phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance();
try {
$phoneProto = $phoneUtil->parse($value, $country);
if ($phoneUtil->isValidNumber($phoneProto)) {
return TRUE;
}
}
catch (\libphonenumber\NumberParseException $e) {}
}
return FALSE;
}
/**
* Provides some arbitrary validation regarding the _country field to only allow
* country codes libphonenumber can handle.
* If using a package based on umpirsky/country-list, invalidate the option 'ZZ => Unknown or invalid region'.
*/
public function phone_country($country)
{
return (strlen($country) === 2 && ctype_alpha($country) && ctype_upper($country) && $country != 'ZZ');
}
}