Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
21 / 21 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
Phone | |
100.00% |
21 / 21 |
|
100.00% |
1 / 1 |
5 | |
100.00% |
1 / 1 |
generatePhone | |
100.00% |
21 / 21 |
|
100.00% |
1 / 1 |
5 |
1 | <?php |
2 | /** |
3 | * Jingga |
4 | * |
5 | * PHP Version 8.1 |
6 | * |
7 | * @package phpOMS\Utils\RnG |
8 | * @copyright Dennis Eichhorn |
9 | * @license OMS License 2.0 |
10 | * @version 1.0.0 |
11 | * @link https://jingga.app |
12 | */ |
13 | declare(strict_types=1); |
14 | |
15 | namespace phpOMS\Utils\RnG; |
16 | |
17 | /** |
18 | * Phone generator. |
19 | * |
20 | * @package phpOMS\Utils\RnG |
21 | * @license OMS License 2.0 |
22 | * @link https://jingga.app |
23 | * @since 1.0.0 |
24 | */ |
25 | class Phone |
26 | { |
27 | /** |
28 | * Get a random phone number. |
29 | * |
30 | * @param bool $isInt This number uses a country code |
31 | * @param string $struct Number layout |
32 | * @param array<null|array> $size Digits per placeholder [min, max] |
33 | * @param null|array $countries Country codes |
34 | * |
35 | * @return string |
36 | * |
37 | * @since 1.0.0 |
38 | */ |
39 | public static function generatePhone( |
40 | bool $isInt = true, |
41 | string $struct = '+$1 ($2) $3-$4', |
42 | array $size = [null, [3, 4], [3, 5], [3, 8],], |
43 | array $countries = null |
44 | ) : string |
45 | { |
46 | $numberString = $struct; |
47 | |
48 | if ($isInt) { |
49 | if ($countries === null) { |
50 | $countries = ['de' => 49, 'us' => 1]; |
51 | } |
52 | |
53 | $numberString = \str_replace( |
54 | '$1', |
55 | (string) $countries[\array_keys($countries)[\mt_rand(0, \count($countries) - 1)]], |
56 | $numberString |
57 | ); |
58 | } |
59 | |
60 | $numberParts = \substr_count($struct, '$'); |
61 | |
62 | for ($i = ($isInt ? 2 : 1); $i <= $numberParts; ++$i) { |
63 | $numberString = \str_replace( |
64 | '$' . $i, |
65 | StringUtils::generateString( |
66 | $size[$i - 1][0] ?? 0, |
67 | $size[$i - 1][1] ?? 0, |
68 | '0123456789' |
69 | ), |
70 | $numberString |
71 | ); |
72 | } |
73 | |
74 | return $numberString; |
75 | } |
76 | } |