Skip to content

Commit bdb9911

Browse files
authored
πŸ“¦ NEW: Export to YAML (#67)
1 parent 02032c7 commit bdb9911

12 files changed

Lines changed: 2803411 additions & 13 deletions

File tree

β€Ž.github/workflows/export.ymlβ€Ž

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ jobs:
3333
path: plist
3434

3535
export_json_xml:
36-
name: JSON/XML
36+
name: JSON/XML/YAML
3737
needs: export_plist
3838
runs-on: ubuntu-latest
3939

@@ -69,6 +69,7 @@ jobs:
6969
run: |
7070
php scripts/export.php
7171
php scripts/export_xml.php
72+
php scripts/export_yaml.php
7273
7374
- name: Commit changed files
7475
uses: stefanzweifel/git-auto-commit-action@v4.1.6

β€ŽREADME.mdβ€Ž

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,26 @@
44
![release](https://img.shields.io/github/v/release/dr5hn/countries-states-cities-database?style=flat-square)
55
![size](https://img.shields.io/github/repo-size/dr5hn/countries-states-cities-database?label=size&style=flat-square)
66

7-
Full Database of city state country available in JSON, SQL, XML & PLIST Format
7+
Full Database of city state country available in JSON, SQL, XML, YAML & PLIST Format
88
All Countries, States & Cities are Covered & Populated with Different Combinations & Versions.
99

1010
## Formats Available
1111
- JSON
1212
- SQL
1313
- XML
1414
- PLIST
15+
- YAML
1516
- CSV [WIP]
1617

1718
## Distribution Files Info
18-
File | JSON | SQL | XML | PLIST | CSV
19-
:------------ | :-------------| :-------------| :------------- |:-------------|:-------------
20-
Countries | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | WIP
21-
States | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | WIP
22-
Cities | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | WIP
23-
Country+States | :white_check_mark: | NA | :white_check_mark: | :white_check_mark: | WIP
24-
State+Cities | :white_check_mark: | NA | :white_check_mark: | :white_check_mark: | WIP
25-
Country+State+Cities/World | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | WIP
19+
File | JSON | SQL | XML | PLIST | YAML | CSV
20+
:------------ | :-------------| :-------------| :------------- |:-------------|:-------------|:-------------
21+
Countries | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | WIP
22+
States | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | WIP
23+
Cities | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | WIP
24+
Country+States | :white_check_mark: | NA | :white_check_mark: | :white_check_mark: | :white_check_mark: | WIP
25+
State+Cities | :white_check_mark: | NA | :white_check_mark: | :white_check_mark: | :white_check_mark: | WIP
26+
Country+State+Cities/World | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | WIP
2627

2728
## Demo
2829
https://dr5hn.github.io/countries-states-cities-database/

β€Žscripts/export_yaml.phpβ€Ž

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
// Require Array2XML class which takes a PHP array and changes it to XML
4+
require_once 'vendor/base.php';
5+
use Symfony\Component\Yaml\Yaml;
6+
7+
$rootDir = dirname(dirname(__FILE__));
8+
$files = array(
9+
'countries' => array(
10+
'from' => '/countries.json',
11+
'to' => '/yml/countries.yml',
12+
'singular' => 'country',
13+
),
14+
'states' => array(
15+
'from' => '/states.json',
16+
'to' => '/yml/states.yml',
17+
'singular' => 'state',
18+
),
19+
'cities' => array(
20+
'from' => '/cities.json',
21+
'to' => '/yml/cities.yml',
22+
'singular' => 'city',
23+
),
24+
'states_cities' => array(
25+
'from' => '/states+cities.json',
26+
'to' => '/yml/states+cities.yml',
27+
'singular' => 'state_city',
28+
),
29+
'countries_states' => array(
30+
'from' => '/countries+states.json',
31+
'to' => '/yml/countries+states.yml',
32+
'singular' => 'country_state',
33+
),
34+
'countries_states_cities' => array(
35+
'from' => '/countries+states+cities.json',
36+
'to' => '/yml/countries+states+cities.yml',
37+
'singular' => 'country_state_city',
38+
),
39+
);
40+
41+
foreach ($files as $root => $v) :
42+
// Gets JSON file
43+
$json = file_get_contents($rootDir.$v['from']);
44+
45+
$csc = array($v['singular'] => json_decode($json));
46+
47+
// Converts PHP Array to XML with the root element being 'root-element-here'
48+
$yml = Yaml::dump($csc, 7, 2, Yaml::DUMP_OBJECT_AS_MAP);
49+
50+
$fp = fopen($rootDir.$v['to'], 'w'); // Putting Array to XML
51+
fwrite($fp, $yml);
52+
fclose($fp);
53+
54+
echo memory_get_usage().PHP_EOL;
55+
echo 'YAML Exported to '.$v['to'].PHP_EOL;
56+
endforeach;

β€Žscripts/vendor/base.phpβ€Ž

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<?php
22
ini_set('display_errors', 1);
33
ini_set('memory_limit', '-1');
4-
ini_set('error_reporting', E_ALL);
4+
ini_set('max_execution_time', 300);
5+
ini_set('error_reporting', E_ALL & ~E_NOTICE);
56
date_default_timezone_set('Asia/Kolkata');
67

78
require_once __DIR__.'/vendor/autoload.php';

β€Žscripts/vendor/composer.jsonβ€Ž

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"version": "v0.2",
55
"require": {
66
"mashape/unirest-php": "^3.0",
7-
"spatie/array-to-xml": "^2.10"
7+
"spatie/array-to-xml": "^2.10",
8+
"symfony/yaml": "^5.0"
89
}
910
}

β€Žscripts/vendor/composer.lockβ€Ž

Lines changed: 118 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
Β (0)