-
Notifications
You must be signed in to change notification settings - Fork 2
/
data.php
100 lines (91 loc) · 2.55 KB
/
data.php
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
class SampleData
{
public $categories;
public $products;
public $users;
public $orders;
public $strings;
function __construct ($n)
{
if ($n < 10)
throw new InvalidArgumentException('n must be 10 or larger');
$this->categories = $this->generateProductCategories($n / 10);
$this->products = $this->generateProducts($n);
$this->users = $this->generateUsers($n / 10);
$this->orders = $this->generateOrders($n);
$this->strings = $this->generateStrings($n);
}
private function generateProductCategories ($n)
{
$categories = [ ];
for ($i = 1; $i <= $n; $i++) {
$categories[] = array(
'id' => $i,
'name' => $this->randomString('category'),
'desc' => $this->randomString('category-desc') . $this->randomString(),
);
}
return $categories;
}
private function generateProducts ($n)
{
$products = [ ];
for ($i = 1; $i <= $n; $i++) {
$products[] = [
'id' => $i,
'name' => $this->randomString('product'),
'catId' => array_rand($this->categories)['id'],
'quantity' => rand(1, 100),
];
}
return $products;
}
private function generateUsers ($n)
{
$users = [ ];
for ($i = 1; $i <= $n; $i++) {
$users[] = [
'id' => $i,
'name' => $this->randomString('user'),
'rating' => rand(0, 10),
];
}
return $users;
}
private function generateOrders ($n)
{
$orders = [ ];
for ($i = 1; $i <= $n; $i++) {
$orders[] = [
'id' => $i,
'customerId' => array_rand($this->users)['id'],
'items' => $this->generateOrderItems(rand(1, 10)),
];
}
return $orders;
}
private function generateOrderItems ($n)
{
$items = [ ];
for ($i = 1; $i <= $n; $i++) {
$items[] = [
'prodId' => array_rand($this->products)['id'],
'quantity' => rand(0, 10),
];
}
return $items;
}
private function generateStrings ($n)
{
$strings = [ ];
for ($i = 1; $i <= $n; $i++) {
$strings[] = $this->randomString('s');
}
return $strings;
}
private function randomString ($prefix = '')
{
return uniqid("$prefix-", true);
}
}