|
| 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; |
0 commit comments