RprtCli/app/src/Utils/CsvReport/CsvReport.php

67 lines
1.7 KiB
PHP

<?php
namespace RprtCli\Utils\CsvReport;
use RprtCli\Utils\CsvReport\CsvReportInterface;
class CsvReport implements CsvReportInterface {
public function getReportData(string $file_path): array {
$output = [];
// @TODO replace with config service.
$config = $this->dummyConfig();
foreach (array_keys($config['projects']) as $key) {
$output[$key] = 0;
}
if ($file = fopen($file_path, 'r')) {
while (($line = fgetcsv($file)) !== FALSE) {
$parsed = $this->parseCsvFile($line);
// $key = reset(array_keys($parsed));
$key = array_key_first($parsed);
if (isset($output[$key])) {
$output[$key] += (int) reset($parsed);
}
}
}
return $output;
}
protected function parseCsvFile(array $raw_data): array {
$config = $this->dummyConfig();
// var_dump($raw_data);
foreach ($config['projects'] as $key => $project) {
if (preg_match('/'.$project['pattern'].'/', $raw_data[1])) {
return [$key => $raw_data[4]];
}
}
return [];
}
protected function dummyConfig(): array {
$config = [
'projects' => [
'LDP' => [
'name' => 'lupus.digital',
'pattern' => 'LDP-[0-9]+',
'price' => 26,
// optional specify columns
],
'WV' => [
'name' => 'Wirtschaftsverlag',
'pattern' => 'WV-[0-9]+',
'price' => 26,
// optional specify columns
],
'Other' => [
'name' => 'Other projects',
'pattern' => '(?!.\bLDP\b)(?!.\bWV\b)',
'price' => 26,
// optional specify columns
],
],
];
return $config;
}
}