1#!/usr/bin/env php
2<?php
3
4namespace Sabre\VObject;
5
6// This sucks.. we have to try to find the composer autoloader. But chances
7// are, we can't find it this way. So we'll do our bestest
8$paths = [
9    __DIR__ . '/../vendor/autoload.php',  // In case vobject is cloned directly
10    __DIR__ . '/../../../autoload.php',   // In case vobject is a composer dependency.
11];
12
13foreach($paths as $path) {
14    if (file_exists($path)) {
15        include $path;
16        break;
17    }
18}
19
20if (!class_exists('Sabre\\VObject\\Version')) {
21    fwrite(STDERR, "Composer autoloader could not be properly loaded.\n");
22    die(1);
23}
24
25if ($argc < 2) {
26
27    $version = Version::VERSION;
28
29    $help = <<<HI
30sabre/vobject $version
31Usage:
32    generate_vcards [count]
33
34Options:
35    count   The number of random vcards to generate
36
37Examples:
38    generate_vcards 1000 > testdata.vcf
39
40HI;
41
42    fwrite(STDERR, $help);
43    exit(2);
44}
45
46$count = (int)$argv[1];
47if ($count < 1) {
48    fwrite(STDERR, "Count must be at least 1\n");
49    exit(2);
50}
51
52fwrite(STDERR, "sabre/vobject " . Version::VERSION . "\n");
53fwrite(STDERR, "Generating " . $count . " vcards in vCard 4.0 format\n");
54
55/**
56 * The following list is just some random data we compiled from various
57 * sources online.
58 *
59 * Very little thought went into compiling this list, and certainly nothing
60 * political or ethical.
61 *
62 * We would _love_ more additions to this to add more variation to this list.
63 *
64 * Send us PR's and don't be shy adding your own first and last name for fun.
65 */
66
67$sets = array(
68    "nl" => array(
69        "country" => "Netherlands",
70        "boys" => array(
71            "Anno",
72            "Bram",
73            "Daan",
74            "Evert",
75            "Finn",
76            "Jayden",
77            "Jens",
78            "Jesse",
79            "Levi",
80            "Lucas",
81            "Luuk",
82            "Milan",
83            "René",
84            "Sem",
85            "Sibrand",
86            "Willem",
87        ),
88        "girls" => array(
89            "Celia",
90            "Emma",
91            "Fenna",
92            "Geke",
93            "Inge",
94            "Julia",
95            "Lisa",
96            "Lotte",
97            "Mila",
98            "Sara",
99            "Sophie",
100            "Tess",
101            "Zoë",
102        ),
103        "last" => array(
104            "Bakker",
105            "Bos",
106            "De Boer",
107            "De Groot",
108            "De Jong",
109            "De Vries",
110            "Jansen",
111            "Janssen",
112            "Meyer",
113            "Mulder",
114            "Peters",
115            "Smit",
116            "Van Dijk",
117            "Van den Berg",
118            "Visser",
119            "Vos",
120        ),
121    ),
122    "us" => array(
123        "country" => "United States",
124        "boys" => array(
125            "Aiden",
126            "Alexander",
127            "Charles",
128            "David",
129            "Ethan",
130            "Jacob",
131            "James",
132            "Jayden",
133            "John",
134            "Joseph",
135            "Liam",
136            "Mason",
137            "Michael",
138            "Noah",
139            "Richard",
140            "Robert",
141            "Thomas",
142            "William",
143        ),
144        "girls" => array(
145            "Ava",
146            "Barbara",
147            "Chloe",
148            "Dorothy",
149            "Elizabeth",
150            "Emily",
151            "Emma",
152            "Isabella",
153            "Jennifer",
154            "Lily",
155            "Linda",
156            "Margaret",
157            "Maria",
158            "Mary",
159            "Mia",
160            "Olivia",
161            "Patricia",
162            "Roxy",
163            "Sophia",
164            "Susan",
165            "Zoe",
166        ),
167        "last" => array(
168            "Smith",
169            "Johnson",
170            "Williams",
171            "Jones",
172            "Brown",
173            "Davis",
174            "Miller",
175            "Wilson",
176            "Moore",
177            "Taylor",
178            "Anderson",
179            "Thomas",
180            "Jackson",
181            "White",
182            "Harris",
183            "Martin",
184            "Thompson",
185            "Garcia",
186            "Martinez",
187            "Robinson",
188        ),
189    ),
190);
191
192$current = 0;
193
194$r = function($arr) {
195
196    return $arr[mt_rand(0,count($arr)-1)];
197
198};
199
200$bdayStart = strtotime('-85 years');
201$bdayEnd = strtotime('-20 years');
202
203while($current < $count) {
204
205    $current++;
206    fwrite(STDERR, "\033[100D$current/$count");
207
208    $country = array_rand($sets);
209    $gender = mt_rand(0,1)?'girls':'boys';
210
211    $vcard = new Component\VCard(array(
212        'VERSION' => '4.0',
213        'FN' => $r($sets[$country][$gender]) . ' ' . $r($sets[$country]['last']),
214        'UID' => UUIDUtil::getUUID(),
215    ));
216
217    $bdayRatio = mt_rand(0,9);
218
219    if($bdayRatio < 2) {
220        // 20% has a birthday property with a full date
221        $dt = new \DateTime('@' . mt_rand($bdayStart, $bdayEnd));
222        $vcard->add('BDAY', $dt->format('Ymd'));
223
224    } elseif ($bdayRatio < 3) {
225        // 10% we only know the month and date of
226        $dt = new \DateTime('@' . mt_rand($bdayStart, $bdayEnd));
227        $vcard->add('BDAY', '--' . $dt->format('md'));
228    }
229    if ($result = $vcard->validate()) {
230        ob_start();
231        echo "\nWe produced an invalid vcard somehow!\n";
232        foreach($result as $message) {
233            echo "  " . $message['message'] . "\n";
234        }
235        fwrite(STDERR, ob_get_clean());
236    }
237    echo $vcard->serialize();
238
239}
240
241fwrite(STDERR,"\nDone.\n");
242