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

76 lines
2.0 KiB
PHP
Raw Normal View History

2021-04-05 16:23:06 +02:00
<?php
namespace RprtCli\Utils\CsvReport;
2021-04-05 20:05:05 +02:00
use RprtCli\Utils\Configuration\ConfigurationInterface;
2021-04-05 16:23:06 +02:00
use RprtCli\Utils\CsvReport\CsvReportInterface;
class CsvReport implements CsvReportInterface {
2021-04-05 20:05:05 +02:00
protected $configurationService;
function __construct(ConfigurationInterface $config) {
$this->configurationService = $config;
}
2021-04-05 16:23:06 +02:00
public function getReportData(string $file_path): array {
$output = [];
// @TODO replace with config service.
2021-04-05 20:05:05 +02:00
// $config = $this->dummyConfig()['projects'];
$config = $this->configurationService->get('projects');
foreach (array_keys($config) as $key) {
2021-04-05 16:23:06 +02:00
$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 {
2021-04-05 20:05:05 +02:00
// $config = $this->dummyConfig();
$config = $this->configurationService->get('projects');
2021-04-05 16:23:06 +02:00
// var_dump($raw_data);
2021-04-05 20:05:05 +02:00
foreach ($config as $key => $project) {
2021-04-05 16:23:06 +02:00
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;
}
}