-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtransformer.php
77 lines (59 loc) · 1.73 KB
/
transformer.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
<?php
$path = './names/';
$path_library = './application/library/';
$files = scandir($path);
$names = [];
// Each Language Type contains the following array:
// $output_array = [
// 'male' => [],
// 'female' => [],
// 'surname' => [],
// 'surname_male' => [],
// 'surname_female' => [],
// ];
foreach ($files as $file_name) {
if (!in_array($file_name, ['.', '..'])) {
$output_array = [
'male' => [],
'female' => [],
'surname' => [],
'surname_male' => [],
'surname_female' => [],
];
$gender = '';
$is_surname = FALSE;
$list = str_replace('.txt', '', $file_name);
$filename_expl = explode(' ', $list);
// Check for Surname.
if ($filename_expl[count($filename_expl) - 1] === 'Surname') {
$is_surname = TRUE;
}
// Name
$name_type = strtolower($filename_expl[0]);
// Gender
if (in_array($filename_expl[1], ['Male', 'Female'])) {
$gender = strtolower($filename_expl[1]);
}
$names_from_file = file($path . $file_name, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
if ($is_surname) {
$names[$name_type][($gender ? $gender . '_' : '') . 'surname'] = $names_from_file;
}
else {
$names[$name_type][$gender] = $names_from_file;
}
}
}
# Save Files.
foreach ($names as $name_type => $name_list) {
// foreach ($name_list as $key => $list) {
// $name_list[$key] = array_map('mbconverter', $list);
// }
$json = json_encode($name_list, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
$filename = $path_library . $name_type . '.json';
$export_file = fopen($filename, 'w');
fwrite($export_file, $json);
fclose($export_file);
}
function mbconverter($str) {
return mb_convert_encoding($str, 'UTF-8');
}