Skip to content

Instantly share code, notes, and snippets.

@mbunge
Last active December 22, 2015 07:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbunge/6439415 to your computer and use it in GitHub Desktop.
Save mbunge/6439415 to your computer and use it in GitHub Desktop.
ImpEx is managing profiles as anonymous functions. Each profile receive current ImpEx object and data of previous profile and return processed data. All profiles will executed by running order.
<?php
class ImpEx {
private $data = null;
private $rawData = null;
private $processData = null;
private $profiles = null;
const CSV_DELIMITER = ',';
const CSV_DELIMITER_EXCEL = ';';
const CSV_ENCLOSURE = '"';
const CSV_ESCAPE = '\\';
const CSV_LINE_LENGTH = 4096;
public function __construct(){
$this->profiles = new ArrayObject();
}
public function addProfile($id, $callback){
$this->profiles[$id] = $callback;
}
protected function getRawDataFromFile($file){
if(!file_exists($file)){
return false;
}
return file_get_contents($file);
}
public function executeProfile($profileId){
$profile = $this->profiles[$profileId];
$this->data = $profile($this,$this->data);
return $this;
}
public function importFromJson($file){
$data = $this->getRawDataFromFile($file);
if($data === false){
return false;
}
$data = json_decode($data);
$jsonLastError = json_last_error();
if($jsonLastError !== JSON_ERROR_NONE){
$errorMsg = null;
switch($jsonLastError)
{
case JSON_ERROR_DEPTH:
$errorMsg = 'Maximale Stacktiefe überschritten';
break;
case JSON_ERROR_CTRL_CHAR:
$errorMsg = 'Unerwartetes Steuerzeichen gefunden';
break;
case JSON_ERROR_SYNTAX:
$errorMsg = 'Syntaxfehler, ungültiges JSON';
break;
default:
$errorMsg = 'Unknown error';
}
throw new Exception($errorMsg);
}
return $data;
}
public function exportToJSON($filename, $data){
$json = json_encode($data);
return file_put_contents($filename, $json);
}
public function exportToCSV($filename, $data, $delimiter=self::CSV_DELIMITER, $enclosure=self::CSV_ENCLOSURE, $arrayIndexAsHeader=false){
$fp = fopen($filename, 'w');
$hasHeader = false;
foreach ($data as $fields) {
if($arrayIndexAsHeader === true && !$hasHeader){
fputcsv($fp, array_keys($fields), $delimiter, $enclosure);
$hasHeader = true;
}
fputcsv($fp, $fields, $delimiter, $enclosure);
}
fclose($fp);
}
public function importFromCSV($filename, $length = self::CSV_LINE_LENGTH, $delimiter=self::CSV_DELIMITER, $enclosure=self::CSV_ENCLOSURE, $escape = self::CSV_ESCAPE){
$row = 1;
$result = array();
if (($handle = fopen($filename, "r")) !== FALSE) {
while (($data = fgetcsv($handle, $length, $delimiter, $enclosure, $escape)) !== FALSE) {
$result[] = $data;
}
fclose($handle);
}
return $result;
}
}
//Example
$impEx = new ImpEx();
//attach profiles
$impEx->addProfile('json.import', function($impEx,$data){
$data = $impEx->importFromJson(dirname(__FILE__) . '/data/import_address.json');
$newData = array();
foreach ($data as $key => $value) {
$street = $addressArray[0];
$postalcode = $addressArray[2];
$city = $addressArray[1];
$newData[] = array(
'postalcode' => mb_convert_encoding($postalcode, 'ISO-8859-1'),
'city' => mb_convert_encoding($city, 'ISO-8859-1'),
'street' => mb_convert_encoding($street, 'ISO-8859-1'),
);
}
//
return $newData;
});
$impEx->addProfile('csv.export', function($impEx,$data){
$filename = dirname(__FILE__) . '/data/export_address.csv';
$impEx->exportToCSV($filename,$data,ImpEx::CSV_DELIMITER_EXCEL, ImpEx::CSV_ENCLOSURE, true);
});
//executeprofiles
$impEx
->executeProfile('olddata.json.import')
->executeProfile('olddata.csv.export');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment