Skip to content

Commit

Permalink
Add Swedish mobileNumber() (#491)
Browse files Browse the repository at this point in the history
* Add Swedish mobileNumber()

* Add format reference

* php-cs-fixer

* Update src/Faker/Provider/sv_SE/PhoneNumber.php

Co-authored-by: Andreas Möller <am@localheinz.com>

* Update src/Faker/Provider/sv_SE/PhoneNumber.php

Co-authored-by: Andreas Möller <am@localheinz.com>

* Update src/Faker/Provider/sv_SE/PhoneNumber.php

Co-authored-by: Andreas Möller <am@localheinz.com>

* Update src/Faker/Provider/sv_SE/PhoneNumber.php

Co-authored-by: Andreas Möller <am@localheinz.com>

* Create sv_SE MobileNumber test

* added more patterns and fixed test

* str_starts_with php 7.4

Co-authored-by: Andreas Möller <am@localheinz.com>
  • Loading branch information
tanthammar and localheinz committed Dec 8, 2022
1 parent ffcf9b7 commit c8461d9
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/Faker/Provider/sv_SE/PhoneNumber.php
Expand Up @@ -2,6 +2,9 @@

namespace Faker\Provider\sv_SE;

/**
* @see https://www.pts.se/sv/bransch/telefoni/nummer-och-adressering/telefoninummerplanen/telefonnummers-struktur/
*/
class PhoneNumber extends \Faker\Provider\PhoneNumber
{
/**
Expand Down Expand Up @@ -34,4 +37,28 @@ class PhoneNumber extends \Faker\Provider\PhoneNumber
'+46(0)%######',
'+46%######',
];

/**
* @var array<int, string> Swedish mobile number formats
*/
protected static array $mobileFormats = [
'+467########',
'+46(0)7########',
'+46 (0)7## ## ## ##',
'+46 (0)7## ### ###',
'07## ## ## ##',
'07## ### ###',
'07##-## ## ##',
'07##-### ###',
'07# ### ## ##',
'07#-### ## ##',
'07#-#######',
];

public function mobileNumber(): string
{
$format = static::randomElement(static::$mobileFormats);

return self::numerify($this->generator->parse($format));
}
}
39 changes: 39 additions & 0 deletions test/Faker/Provider/sv_SE/PhoneNumberTest.php
@@ -0,0 +1,39 @@
<?php

namespace Faker\Test\Provider\sv_SE;

use Faker\Provider\sv_SE\PhoneNumber;
use Faker\Test\TestCase;

/**
* @group legacy
*/
final class PhoneNumberTest extends TestCase
{
public function testMobileNumber(): void
{
for ($i = 0; $i < 10; ++$i) {
$number = $this->faker->mobileNumber;

self::assertTrue(self::itStartsWithPrefix($number));
}
}

protected static function itStartsWithPrefix($number): bool
{
$prefixes = ['+467', '+46(0)7', '+46 (0)7', '+46 (0)7', '07'];

foreach ($prefixes as $prefix) {
if (strpos($number, (string) $prefix) === 0) {
return true;
}
}

return false;
}

protected function getProviders(): iterable
{
yield new PhoneNumber($this->faker);
}
}

0 comments on commit c8461d9

Please sign in to comment.