Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added PHP export #409

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/MLD/Console/Command/ExportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class ExportCommand extends Command
'csv' => ['class' => '\MLD\Converter\CsvConverter', 'output_file' => 'countries.csv'],
'xml' => ['class' => '\MLD\Converter\XmlConverter', 'output_file' => 'countries.xml'],
'yml' => ['class' => '\MLD\Converter\YamlConverter', 'output_file' => 'countries.yml'],
'php' => ['class' => '\MLD\Converter\PHPConverter', 'output_file' => 'countries.php']
];

/**
Expand Down
40 changes: 40 additions & 0 deletions src/MLD/Converter/PHPConverter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace MLD\Converter;

/**
* Class CsvConverter
*/
class PHPConverter extends AbstractConverter
{

/**
* @var string
*/
private $prefix = "<?php\nreturn [";

/**
* @var string
*/
private $suffix = '];';

/**
* @var array
*/
private $bodyChunks = [];

/**
* @return string data converted into CSV
*/
public function convert()
{
array_walk($this->countries, [$this, 'processCountry']);
return $this->prefix . "\n" . implode(",\n",$this->bodyChunks) . "\n" . $this->suffix;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should use the var_export function which produces a parsable string representation of a variable, that is what you want here.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dimitar-kunchev what do you think?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I forgot about that PR. Yes, var_export should do the trick.

}

private function processCountry(&$array)
{
$this->bodyChunks[] =
"'".$array['cca2'] ."' => " . var_export($array, true);
}
}