Skip to content

Commit

Permalink
[2022-05-05] chore: cleanup code
Browse files Browse the repository at this point in the history
  • Loading branch information
manh-gntvn committed May 5, 2022
1 parent b2f7622 commit aecd509
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 18 deletions.
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@
"name": "MaDnh",
"email": "[email protected]"
}
]
],
"require": {
"ext-json": "*"
}
}
45 changes: 28 additions & 17 deletions export.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ function isStartWith($str, $start_with)
return $start_with === substr($str, 0, strlen($start_with));
}

function exportContent(string $fileName, $data)
{
$writeData = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_FORCE_OBJECT);
file_put_contents(DIST_DIR . DS . $fileName, $writeData);
}

function readExcelFile($file)
{
$objPHPExcel = PHPExcel_IOFactory::load($file);
Expand Down Expand Up @@ -59,9 +65,9 @@ function readExcelFile($file)
$row_data['ten_tinh_tp_slug'] = slug($row_data['ten_tinh_tp']);

$row_data['qh_la_tp'] = isStartWith(mb_strtolower($row_data['ten_quan_huyen_va_cap']), mb_strtolower($tp_label));
$row_data['qh_la_thi_xa'] = !$row_data['qh_la_tp'] && isStartWith(mb_strtolower($row_data['ten_quan_huyen_va_cap']), mb_strtolower($thi_xa_label));
$row_data['qh_la_quan'] = !$row_data['qh_la_thi_xa'] && !$row_data['qh_la_thi_xa'] && isStartWith(mb_strtolower($row_data['ten_quan_huyen_va_cap']), mb_strtolower($quan_label));
$row_data['qh_la_huyen'] = !($row_data['qh_la_thi_xa'] || $row_data['qh_la_thi_xa'] || $row_data['qh_la_quan']);
$row_data['qh_la_thi_xa'] = ! $row_data['qh_la_tp'] && isStartWith(mb_strtolower($row_data['ten_quan_huyen_va_cap']), mb_strtolower($thi_xa_label));
$row_data['qh_la_quan'] = ! $row_data['qh_la_thi_xa'] && ! $row_data['qh_la_thi_xa'] && isStartWith(mb_strtolower($row_data['ten_quan_huyen_va_cap']), mb_strtolower($quan_label));
$row_data['qh_la_huyen'] = ! ($row_data['qh_la_thi_xa'] || $row_data['qh_la_thi_xa'] || $row_data['qh_la_quan']);

switch (true) {
case $row_data['qh_la_tp']:
Expand All @@ -82,7 +88,6 @@ function readExcelFile($file)
break;
}

//Fix bug một vài chỗ dùng "Thị Xã" thay vì "Thị xã"
if ($row_data['qh_la_thi_xa']) {
$row_data['ten_quan_huyen_va_cap'] = 'Thị xã ' . $row_data['ten_qh'];
}
Expand All @@ -92,8 +97,8 @@ function readExcelFile($file)
$row_data['dia_chi_qh_full'] = implode(', ', [$row_data['ten_quan_huyen_va_cap'], $row_data['ten_tinh_tp_va_cap']]);

$row_data['px_la_phuong'] = $row_data['cap_px'] === 'Phường';
$row_data['px_la_thi_tran'] = !$row_data['px_la_phuong'] && $row_data['cap_px'] === $thi_tran_label;
$row_data['px_la_xa'] = !($row_data['px_la_phuong'] || $row_data['px_la_thi_tran']);
$row_data['px_la_thi_tran'] = ! $row_data['px_la_phuong'] && $row_data['cap_px'] === $thi_tran_label;
$row_data['px_la_xa'] = ! ($row_data['px_la_phuong'] || $row_data['px_la_thi_tran']);

switch (true) {
case $row_data['px_la_phuong']:
Expand Down Expand Up @@ -195,20 +200,26 @@ function readExcelFile($file)
$tree[$tinh_tp_data['code']]['quan-huyen'][$current_quan_huyen_code]['xa-phuong'][$xa_phuong_data['code']] = $xa_phuong_data;
}

file_put_contents(DIST_DIR . DS . 'quan-huyen' . DS . $tinh_tp_data['code'] . '.json', json_encode($current_quan_huyen, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
exportContent('quan-huyen' . DS . $tinh_tp_data['code'] . '.json', $current_quan_huyen);

foreach (array_keys($current_quan_huyen) as $temp_qh_id) {
$_xa_phuong_by_quan = $current_xa_phuong_by_quan[$temp_qh_id];

if (count($_xa_phuong_by_quan) === 1 && array_values($_xa_phuong_by_quan)[0]['name'] === '') {
$_xa_phuong_by_quan = [];
}
$_xa_phuong_by_quan = $current_xa_phuong_by_quan[$temp_qh_id];

file_put_contents(DIST_DIR . DS . 'xa-phuong' . DS . $temp_qh_id . '.json', json_encode($_xa_phuong_by_quan, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_FORCE_OBJECT));
if (count($_xa_phuong_by_quan) === 1 && array_values($_xa_phuong_by_quan)[0]['name'] === '') {
exportContent('xa-phuong' . DS . $temp_qh_id . '.json', '{}');
} else {
exportContent('xa-phuong' . DS . $temp_qh_id . '.json', $_xa_phuong_by_quan);
}
}
}

file_put_contents(DIST_DIR . DS . 'tree.json', json_encode($tree, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
file_put_contents(DIST_DIR . DS . 'tinh_tp.json', json_encode($tinh_tp, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
file_put_contents(DIST_DIR . DS . 'quan_huyen.json', json_encode($quan_huyen, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
file_put_contents(DIST_DIR . DS . 'xa_phuong.json', json_encode($xa_phuong, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));

ksort($tree, SORT_NUMERIC);
ksort($tinh_tp, SORT_NUMERIC);
ksort($quan_huyen, SORT_NUMERIC);
ksort($xa_phuong, SORT_NUMERIC);

exportContent('tree.json', $tree);
exportContent('tinh_tp.json', $tinh_tp);
exportContent('quan_huyen.json', $quan_huyen);
exportContent('xa_phuong.json', $xa_phuong);

0 comments on commit aecd509

Please sign in to comment.