Skip to content

Commit

Permalink
Refactor Phone Module (#592)
Browse files Browse the repository at this point in the history
* change phone to use string_view

Signed-off-by: Uilian Ries <[email protected]>

* add empty phone data

Signed-off-by: Uilian Ries <[email protected]>

* adapt phone tests to use string view

Signed-off-by: Uilian Ries <[email protected]>

* fill phone data with vector files

Signed-off-by: Uilian Ries <[email protected]>

---------

Signed-off-by: Uilian Ries <[email protected]>
  • Loading branch information
uilianries authored May 26, 2024
1 parent 901ee78 commit e4fc0cc
Show file tree
Hide file tree
Showing 8 changed files with 313 additions and 90 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ set(FAKER_SOURCES
src/modules/number/Number.cpp
src/modules/person/Person.cpp
src/modules/phone/Phone.cpp
src/modules/phone/PhoneData.cpp
src/modules/science/Science.cpp
src/modules/sport/Sport.cpp
src/modules/string/String.cpp
Expand Down
9 changes: 5 additions & 4 deletions include/faker-cxx/Phone.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <optional>
#include <string_view>
#include <string>
#include <unordered_map>

Expand Down Expand Up @@ -35,7 +36,7 @@ class Phone
* Phone::platform() // "iOS"
* @endcode
*/
static std::string platform();
static std::string_view platform();

/**
* @brief Returns a random phone model.
Expand All @@ -46,7 +47,7 @@ class Phone
* Phone::modelName() // "Samsung Galaxy S22"
* @endcode
*/
static std::string modelName();
static std::string_view modelName();

/**
* @brief Returns a random phone manufacturer.
Expand All @@ -57,7 +58,7 @@ class Phone
* Phone::manufacturer() // "Sony"
* @endcode
*/
static std::string manufacturer();
static std::string_view manufacturer();

/**
* @brief Returns a random phone number based on country phone number template.
Expand Down Expand Up @@ -92,7 +93,7 @@ class Phone
* Phone::areaCode() // "+1"
* @endcode
*/
static std::string areaCode();
static std::string_view areaCode();

private:
static std::unordered_map<PhoneNumberCountryFormat, std::string> createPhoneNumberFormatMap();
Expand Down
24 changes: 11 additions & 13 deletions src/modules/phone/Phone.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#include "faker-cxx/Phone.h"

#include "data/AreaCodes.h"
#include "data/PhoneData.h"
#include "data/PhoneNumbers.h"
#include "PhoneData.h"
#include "faker-cxx/Helper.h"

namespace faker
Expand All @@ -16,7 +14,7 @@ std::string Phone::number(std::optional<std::string> format)

if (!format.has_value() || format->empty())
{
selectedFormat = Helper::arrayElement<std::string>(phoneNumbers);
selectedFormat = Helper::arrayElement(phone::phoneNumbers);
}
else
{
Expand All @@ -43,19 +41,19 @@ std::string Phone::imei()
return Helper::replaceCreditCardSymbols("##-######-######-L", '#');
}

std::string Phone::platform()
std::string_view Phone::platform()
{
return Helper::arrayElement(faker::data::PhonePlatforms);
return Helper::arrayElement(phone::PhonePlatforms);
}

std::string Phone::modelName()
std::string_view Phone::modelName()
{
return Helper::arrayElement(faker::data::PhoneModelNames);
return Helper::arrayElement(phone::PhoneModelNames);
}

std::string Phone::manufacturer()
std::string_view Phone::manufacturer()
{
return Helper::arrayElement(faker::data::PhoneManufacturers);
return Helper::arrayElement(phone::PhoneManufacturers);
}

std::unordered_map<PhoneNumberCountryFormat, std::string> Phone::createPhoneNumberFormatMap()
Expand All @@ -68,14 +66,14 @@ std::unordered_map<PhoneNumberCountryFormat, std::string> Phone::createPhoneNumb
{
auto formatEnum = static_cast<PhoneNumberCountryFormat>(i);

formatMap[formatEnum] = phoneNumbers[i];
formatMap[formatEnum] = phone::phoneNumbers[i];
}

return formatMap;
}

std::string Phone::areaCode()
std::string_view Phone::areaCode()
{
return Helper::arrayElement(faker::data::areaCodes);
return Helper::arrayElement(phone::areaCodes);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,267 @@
#pragma once
#include "PhoneData.h"

#include <string>
#include <vector>

namespace faker
namespace faker::phone
{
const std::vector<std::string> phoneNumbers = {
const std::array<std::string_view, 217> areaCodes = {
"+1",
"+144",
"+20",
"+210",
"+211",
"+212",
"+213",
"+214",
"+215",
"+216",
"+217",
"+218",
"+219",
"+220",
"+221",
"+222",
"+223",
"+224",
"+225",
"+226",
"+227",
"+228",
"+229",
"+230",
"+231",
"+232",
"+233",
"+234",
"+235",
"+236",
"+237",
"+238",
"+239",
"+240",
"+241",
"+242",
"+243",
"+244",
"+245",
"+246",
"+247",
"+248",
"+249",
"+250",
"+251",
"+252",
"+253",
"+254",
"+255",
"+256",
"+257",
"+258",
"+259",
"+260",
"+261",
"+262",
"+263",
"+264",
"+265",
"+266",
"+267",
"+268",
"+269",
"+27",
"+28",
"+290",
"+291",
"+292",
"+293",
"+294",
"+295",
"+296",
"+297",
"+298",
"+299",
"+30",
"+31",
"+32",
"+33",
"+34",
"+350",
"+351",
"+352",
"+353",
"+354",
"+355",
"+356",
"+357",
"+358",
"+359",
"+36",
"+370",
"+371",
"+373",
"+374",
"+375",
"+377",
"+38",
"+380",
"+381",
"+385",
"+387",
"+389",
"+39",
"+40",
"+41",
"+42",
"+43",
"+44",
"+45",
"+46",
"+47",
"+48",
"+49",
"+500",
"+501",
"+502",
"+503",
"+504",
"+505",
"+506",
"+507",
"+508",
"+509",
"+51",
"+52",
"+53",
"+54",
"+55",
"+56",
"+57",
"+58",
"+590",
"+591",
"+592",
"+593",
"+594",
"+595",
"+596",
"+597",
"+598",
"+599",
"+60",
"+61",
"+62",
"+63",
"+64",
"+65",
"+66",
"+670",
"+671",
"+672",
"+673",
"+674",
"+675",
"+676",
"+677",
"+678",
"+679",
"+680",
"+681",
"+682",
"+683",
"+684",
"+685",
"+686",
"+687",
"+688",
"+689",
"+690",
"+691",
"+692",
"+7",
"+808",
"+809",
"+81",
"+82",
"+84",
"+850",
"+852",
"+853",
"+855",
"+856",
"+86",
"+871",
"+872",
"+873",
"+874",
"+880",
"+886",
"+90 ",
"+91",
"+92",
"+93",
"+94",
"+95",
"+960",
"+961",
"+962",
"+963",
"+964",
"+965",
"+966",
"+967",
"+968",
"+969",
"+971",
"+972",
"+973",
"+974",
"+975",
"+976",
"+977",
"+98",
"+993",
"+994",
"+995"
};

const std::array<std::string_view, 6> PhonePlatforms = {
"Android OS",
"Palm OS",
"Symbian",
"Tizen",
"Windows Phone",
"iOS"
};

const std::array<std::string_view, 15> PhoneModelNames = {
"Google Pixel 4",
"Google Pixel 6",
"Huawei P50",
"Motorola Edge 20",
"OnePlus 9",
"Oppo Find X3",
"Samsung Galaxy S22",
"Samsung Galaxy S9",
"Sony Xperia 1 III",
"Xiaomi Mi 11",
"iPhone 13",
"iPhone 13",
"iPhone 14",
"iPhone 15",
"iPhone X"
};

const std::array<std::string_view, 9> PhoneManufacturers = {
"Apple",
"Google",
"Huawei",
"Motorola",
"OnePlus",
"Oppo",
"Samsung",
"Sony",
"Xiaomi"
};

const std::array<std::string_view, 230> phoneNumbers = {
"!##-###-###", // Default
"+93 (###) ###-####", // Afghanistan
"+358 (###) ###-####", // Aland Islands
Expand Down
Loading

0 comments on commit e4fc0cc

Please sign in to comment.