Skip to content
Open
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
43 changes: 43 additions & 0 deletions src/Extensions/CountryExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Xefi\Faker\Extensions;

class CountryExtension extends Extension
{
protected array $countryName = [
['name' => 'France', 'iso_country_code_alpha2' => 'FR', 'iso_country_code_alpha3' => 'FRA'],
['name' => 'Spain', 'iso_country_code_alpha2' => 'ES', 'iso_country_code_alpha3' => 'ESP'],
['name' => 'Italy', 'iso_country_code_alpha2' => 'IT', 'iso_country_code_alpha3' => 'ITA'],
['name' => 'Germany', 'iso_country_code_alpha2' => 'DE', 'iso_country_code_alpha3' => 'DEU'],
['name' => 'United Kingdom', 'iso_country_code_alpha2' => 'GB', 'iso_country_code_alpha3' => 'GBR'],
['name' => 'United States', 'iso_country_code_alpha2' => 'US', 'iso_country_code_alpha3' => 'USA'],
['name' => 'Portugal', 'iso_country_code_alpha2' => 'PT', 'iso_country_code_alpha3' => 'PRT'],
['name' => 'Switzerland', 'iso_country_code_alpha2' => 'CH', 'iso_country_code_alpha3' => 'CHE'],
['name' => 'Belgium', 'iso_country_code_alpha2' => 'BE', 'iso_country_code_alpha3' => 'BEL'],
['name' => 'Netherlands', 'iso_country_code_alpha2' => 'NL', 'iso_country_code_alpha3' => 'NLD'],
];

/**
* Returns a random country.
*/
public function country(): string
{
return $this->pickArrayRandomElement($this->countryName)['name'];
}

/**
* Returns the country code in ISO format 3166-1 alpha-2 (FR, NL, US).
*/
public function countryCodeISOAlpha2(): string
{
return $this->pickArrayRandomElement($this->countryName)['iso_country_code_alpha2'];
}

/**
* Returns the country code in ISO format 3166-1 alpha-3 (FRA, NLD, USA).
*/
public function countryCodeISOAlpha3(): string
{
return $this->pickArrayRandomElement($this->countryName)['iso_country_code_alpha3'];
}
}
2 changes: 2 additions & 0 deletions src/FakerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Xefi\Faker\Extensions\ArrayExtension;
use Xefi\Faker\Extensions\BooleanExtension;
use Xefi\Faker\Extensions\ColorsExtension;
use Xefi\Faker\Extensions\CountryExtension;
use Xefi\Faker\Extensions\DateTimeExtension;
use Xefi\Faker\Extensions\FinancialExtension;
use Xefi\Faker\Extensions\HashExtension;
Expand All @@ -31,6 +32,7 @@ public function boot(): void
FinancialExtension::class,
BooleanExtension::class,
ArrayExtension::class,
CountryExtension::class,
]);
}
}
23 changes: 23 additions & 0 deletions tests/Unit/Extensions/CountryExtensionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Extensions;

use Xefi\Faker\Tests\Unit\Extensions\TestCase;

final class CountryExtensionTest extends TestCase
{
public function testCountry(): void
{
$this->assertMatchesRegularExpression('/^[a-zA-Z]*$/', $this->faker->country());
}

public function testCountryCodeISOAlpha2(): void
{
$this->assertMatchesRegularExpression('/^[A-Z]{2}$/', $this->faker->countryCodeISOAlpha2());
}

public function testCountryCodeISOAlpha3(): void
{
$this->assertMatchesRegularExpression('/^[A-Z]{3}$/', $this->faker->countryCodeISOAlpha3());
}
}