|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Faker\Provider; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use InvalidArgumentException; |
|
7
|
|
|
use Random\RandomException; |
|
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
class FakeCarHelper |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @throws Exception |
|
13
|
|
|
*/ |
|
14
|
7 |
|
public static function getArrayData(array $arrayData, int $count = 1) |
|
15
|
|
|
{ |
|
16
|
7 |
|
$data = static::isWeighted($arrayData) |
|
17
|
4 |
|
? static::getWeighted($arrayData, $count) |
|
18
|
3 |
|
: static::getRandomElementsFromArray($arrayData, $count); |
|
19
|
|
|
|
|
20
|
7 |
|
if (is_array($data) && $count === 1) { |
|
21
|
2 |
|
return array_values($data)[0]; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
5 |
|
return $data; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Determines if an array is weighted(associative). |
|
29
|
|
|
* |
|
30
|
|
|
* An array is "associative" if it doesn't have sequential numerical keys beginning with zero. |
|
31
|
|
|
* |
|
32
|
|
|
* @param array $array |
|
33
|
|
|
* @return bool |
|
34
|
|
|
*/ |
|
35
|
7 |
|
public static function isWeighted(array $array): bool |
|
36
|
|
|
{ |
|
37
|
7 |
|
$keys = array_keys($array); |
|
38
|
|
|
|
|
39
|
7 |
|
return array_keys($keys) !== $keys; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Returns a random element from a passed array |
|
44
|
|
|
* |
|
45
|
|
|
* @param array $values |
|
46
|
|
|
* |
|
47
|
|
|
* @throws Exception |
|
48
|
|
|
*/ |
|
49
|
5 |
|
public static function getRandomElementFromArray($values) |
|
50
|
|
|
{ |
|
51
|
5 |
|
$elements = static::getRandomElementsFromArray($values, 1); |
|
52
|
|
|
|
|
53
|
5 |
|
return array_values($elements)[0]; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Get random elements from input array |
|
58
|
|
|
* |
|
59
|
|
|
* @param array $values The input array |
|
60
|
|
|
* @param int $count The number of random elements you want to get in your response. |
|
61
|
|
|
* Leave out or set to 0 for random. |
|
62
|
|
|
* |
|
63
|
|
|
* @return mixed |
|
64
|
|
|
* @throws InvalidArgumentException|Exception |
|
65
|
|
|
*/ |
|
66
|
8 |
|
public static function getRandomElementsFromArray(array $values, ?int $count = 1): array |
|
67
|
|
|
{ |
|
68
|
|
|
//dd('getRandomElementsFromArray', $values,$count); |
|
69
|
8 |
|
$valuesLength = count($values); |
|
70
|
|
|
|
|
71
|
8 |
|
if ($count > $valuesLength) { |
|
72
|
1 |
|
throw new InvalidArgumentException('Count larger than array length.'); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
8 |
|
if ($count === 0) { |
|
76
|
2 |
|
return []; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
8 |
|
if (!$count) { |
|
|
|
|
|
|
80
|
1 |
|
$count = random_int(1, $valuesLength); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
8 |
|
return array_intersect_key( |
|
84
|
8 |
|
$values, |
|
85
|
8 |
|
array_flip( |
|
86
|
8 |
|
(array) array_rand($values, $count) |
|
87
|
8 |
|
) |
|
88
|
8 |
|
); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Get one element out of an input array with specified weights to get the distribution |
|
93
|
|
|
* of the generated elements as you want them. |
|
94
|
|
|
* |
|
95
|
|
|
* @param array $values Input array with values as key and weight as value. ['value 1' => 30, 'value 7' => 70] |
|
96
|
|
|
* @param int $count |
|
97
|
|
|
* |
|
98
|
|
|
* @return string |
|
99
|
|
|
* @throws Exception |
|
100
|
|
|
*/ |
|
101
|
5 |
|
public static function getWeighted(array $values, int $count = 1): string |
|
|
|
|
|
|
102
|
|
|
{ |
|
103
|
5 |
|
$currentTotal = 0; |
|
104
|
5 |
|
$firstRand = random_int(1, 100); |
|
105
|
|
|
|
|
106
|
5 |
|
$total = array_sum($values); |
|
107
|
|
|
|
|
108
|
5 |
|
$rand = ($firstRand / 100) * $total; |
|
109
|
|
|
|
|
110
|
5 |
|
foreach ($values as $key => $weight) { |
|
111
|
5 |
|
$currentTotal += $weight; |
|
112
|
|
|
|
|
113
|
5 |
|
if ($rand <= $currentTotal) { |
|
114
|
5 |
|
return $key; |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
1 |
|
return ''; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @param array<int> $range |
|
123
|
|
|
* @param string $unit |
|
124
|
|
|
* |
|
125
|
|
|
* @return string |
|
126
|
|
|
* @throws RandomException |
|
127
|
|
|
*/ |
|
128
|
1 |
|
public static function getRangeWithUnit(array $range, string $unit): string |
|
129
|
|
|
{ |
|
130
|
1 |
|
return random_int($range[0], $range[1]) . ' ' . $unit; |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths