Skip to content

Commit

Permalink
Improved italian phone numbers (#950)
Browse files Browse the repository at this point in the history
  • Loading branch information
Catlike14 authored Feb 22, 2025
1 parent 3593446 commit f7ac507
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 13 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

- Removed domain `gmail.com.au` from `Provider\en_AU\Internet` (#886)
- Refreshed ISO currencies (#919)
-
- Improved italian phone number formats

## [2024-11-09, v1.24.0](https://github.com/FakerPHP/Faker/compare/v1.23.1..v1.24.0)

- Fix internal deprecations in Doctrine's populator by @gnutix in (#889)
Expand Down
41 changes: 29 additions & 12 deletions src/Faker/Provider/it_IT/PhoneNumber.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,35 @@

class PhoneNumber extends \Faker\Provider\PhoneNumber
{
/**
* @see https://en.wikipedia.org/wiki/Telephone_numbers_in_Italy
*/
protected static $formats = [
'+## ### ## ## ####',
'+## ## #######',
'+## ## ########',
'+## ### #######',
'+## ### ########',
'+## #### #######',
'+## #### ########',
// According to http://it.wikipedia.org/wiki/Prefisso_telefonico#Elenco_degli_indicativi_in_Italia.2C_a_San_Marino_e_nel_Vaticano
'0## ### ####',
'+39 0## ### ###',
'3## ### ###',
'+39 3## ### ###',
// Landline numbers
'0%# ### ###',
'0%# ### ####',
'02 #### ###', // Milan
'02 #### ####',
'06 #### ###', // Rome
'06 #### ####',

// Mobile numbers
'3%# ### ####',

// International formats
'+39 0%# ### ###',
'+39 0%# ### ####',
'+39 02 #### ###',
'+39 02 #### ####',
'+39 06 #### ###',
'+39 06 #### ####',
'+39 3%# ### ####',
];

protected static $e164Formats = [
'+390%#######',
'+390%########',
'+393%#######',
'+393%########',
];
}
30 changes: 30 additions & 0 deletions src/Faker/Provider/it_IT/PhoneNumberTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Faker\Test\Provider\it_IT;

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

final class PhoneNumberTest extends TestCase
{
public function testPhoneNumber(): void
{
for ($i = 0; $i < 100; ++$i) {
$number = $this->faker->phoneNumber();
self::assertMatchesRegularExpression('/^(((\+39 )?((3[1-9]\d)|(0[1-9]\d{0,1})) \d{3,4} \d{3,4})|((\+39)?(0|3)[1-9]\d{7,9}))$/', $number);
}
}

public function testE164PhoneNumberFormat(): void
{
for ($i = 0; $i < 10; ++$i) {
$number = $this->faker->e164PhoneNumber();
self::assertMatchesRegularExpression('/^\+39(0|3)[1-9]\d{7,9}$/', $number);
}
}

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

0 comments on commit f7ac507

Please sign in to comment.