Skip to content
This repository has been archived by the owner on Dec 11, 2020. It is now read-only.

Commit

Permalink
Added E.164 support as per issue #668
Browse files Browse the repository at this point in the history
Consolidated commit
  • Loading branch information
Dale Attree committed Mar 7, 2016
1 parent 6da7882 commit f2f6647
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ Each of the generator properties (like `name`, `address`, and `lorem`) are calle

phoneNumber // '201-886-0269 x3767'
tollFreePhoneNumber // '(888) 937-7238'
e164PhoneNumber // '+27113456789'

### `Faker\Provider\en_US\Company`

Expand Down
10 changes: 10 additions & 0 deletions src/Faker/Provider/PhoneNumber.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,14 @@ public function phoneNumber()
{
return static::numerify($this->generator->parse(static::randomElement(static::$formats)));
}

/**
* @example +27113456789
* @return string
*/
public function e164PhoneNumber()
{
$formats = array('+#############');
return static::numerify($this->generator->parse(static::randomElement($formats)));
}
}
3 changes: 3 additions & 0 deletions src/Faker/UniqueGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class UniqueGenerator

/**
* @param Generator $generator
* @param $maxRetries
*/
public function __construct(Generator $generator, $maxRetries)
{
Expand All @@ -24,6 +25,7 @@ public function __construct(Generator $generator, $maxRetries)
/**
* Catch and proxy all generator calls but return only unique values
* @param string $attribute
* @return mixed
*/
public function __get($attribute)
{
Expand All @@ -34,6 +36,7 @@ public function __get($attribute)
* Catch and proxy all generator calls with arguments but return only unique values
* @param string $name
* @param array $arguments
* @return mixed
*/
public function __call($name, $arguments)
{
Expand Down
28 changes: 28 additions & 0 deletions test/Faker/Provider/PhoneNumberTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Faker\Test\Provider;

use Faker\Generator;
use Faker\Provider\PhoneNumber;

class PhoneNumberTest extends \PHPUnit_Framework_TestCase
{

/**
* @var Generator
*/
private $faker;

public function setUp()
{
$faker = new Generator();
$faker->addProvider(new PhoneNumber($faker));
$this->faker = $faker;
}

public function testPhoneNumberFormat()
{
$number = $this->faker->e164PhoneNumber();
$this->assertRegExp('/^\+[0-9]{11,}$/', $number);
}
}
1 change: 1 addition & 0 deletions test/Faker/Provider/ProviderOverrideTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ public function testPhoneNumber($locale = null)
$faker = Faker\Factory::create($locale);

$this->assertRegExp(static::TEST_STRING_REGEX, $faker->phoneNumber);
$this->assertRegExp(static::TEST_STRING_REGEX, $faker->e164PhoneNumber);
}


Expand Down
1 change: 1 addition & 0 deletions test/test.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<?php for ($i=0; $i < 10; $i++): ?>
<contact firstName="<?php echo $faker->firstName ?>" lastName="<?php echo $faker->lastName ?>" email="<?php echo $faker->email ?>" >
<phone number="<?php echo $faker->phoneNumber ?>"/>
<phone number="<?php echo $faker->e164PhoneNumber ?>" format="E164"/>
<?php if ($faker->boolean(25)): ?>
<birth date="<?php echo $faker->dateTimeThisCentury->format('Y-m-d') ?>" place="<?php echo $faker->city ?>"/>
<?php endif; ?>
Expand Down

0 comments on commit f2f6647

Please sign in to comment.