forked from jkapuscik2/design-patterns-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_csv.php
More file actions
61 lines (49 loc) · 1.86 KB
/
create_csv.php
File metadata and controls
61 lines (49 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php
class CSV {
const COLUMNS = ["id", "uuid", "location", "resolution", "producer", "operating_system", "ip"];
const LOCATIONS = ["Warsaw", "Berlin", "Amsterdam", "Paris"];
const RESOLUTIONS = ["Full HD", "4K"];
const PRODUCERS = ["LG", "Samsung", "Philips", "Sencor"];
const OPERATING_SYSTEMS = ["Linux", "Android", "Ubuntu"];
private $numItems;
private $fileName;
private $file;
public function __construct (string $fileName, int $numItems) {
$this->numItems = $numItems;
$this->fileName = $fileName;
}
protected function createHeader (): void {
fputcsv($this->file, self::COLUMNS);
}
protected function generateRandomString (int $length): string {
return substr(str_shuffle(str_repeat($x = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', ceil($length / strlen($x)))), 1, $length);
}
protected function getRand (array $arr): string {
$key = array_rand($arr);
return $arr[$key];
}
protected function getRandIp () {
return mt_rand(0, 255) . "." . mt_rand(0, 255) . "." . mt_rand(0, 255) . "." . mt_rand(0, 255);
}
protected function generateData (): void {
for ($idx = 0; $idx < $this->numItems; $idx++) {
fputcsv($this->file, [
$idx + 1,
$this->generateRandomString(rand(5, 10)),
$this->getRand(self::LOCATIONS),
$this->getRand(self::RESOLUTIONS),
$this->getRand(self::PRODUCERS),
$this->getRand(self::OPERATING_SYSTEMS),
$this->getRandIp()
]);
}
}
public function create (): void {
$this->file = fopen($this->fileName, 'w');
$this->createHeader();
$this->generateData();
fclose($this->file);
}
}
$csv = new CSV("demo.csv", 5000);
$csv->create();