Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved italian phone numbers #950

Merged
merged 2 commits into from
Feb 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
Loading