simplify CSV reader, optimize fixed codes checking

This commit is contained in:
Rodney Figaro 2017-01-24 15:19:32 +01:00
parent c07f8ba2e9
commit d0fd4cacf5
6 changed files with 46 additions and 62 deletions

View File

@ -48,14 +48,6 @@ class ArrayExtended implements IteratorAggregate, ArrayAccess
return isset($this->collection[$offset])?$this->collection[$offset]:null;
}
function addNode($parent_key, $key, $value)
{
if (!isset($this->collection[$parent_key])) {
$this->collection[$parent_key] = array();
}
$this->collection[$parent_key][$key] = $value;
}
function filterKeys(callable $predicat)
{
$filtered_collection = array();

View File

@ -3,7 +3,7 @@ require_once(__DIR__.'/ArrayExtended.php');
class CSVReader
{
private $file_path = '';
private $params = array('enable_skip_line' => false, 'separator' => ',', 'delimiter' => '', 'expected_header' => array());
private $params = array('enable_skip_line' => false, 'separator' => ',', 'enclosure' => '"', 'expected_header' => array());
private $handlers = array('start' => null, 'error' => null);
private $current_num_line = 0;
@ -22,9 +22,9 @@ class CSVReader
return $this;
}
function setDelimiter($delimiter)
function setEnclosure($enclosure)
{
$this->params['delimiter'] = $delimiter;
$this->params['enclosure'] = $enclosure;
return $this;
}
@ -66,27 +66,21 @@ class CSVReader
$ok = false;
$this->current_num_line = 0;
$f = null;
if (file_exists($this->file_path)) {
$f = fopen($this->file_path, 'r');
}
$f = fopen($this->file_path, 'r');
if ($f) {
call_user_func($this->handlers['start']);
$first_line = $this->params['enable_skip_line'] ? fgets($f) : '';
if ($first_line !== false) {
$ok = $this->checkHeader($first_line);
while ($ok && ($line = fgets($f))) {
$ok = call_user_func($this->handlers['readline'], $this->lineToArrayExtended($line));
$this->current_num_line++;
}
fclose($f);
$ok = true;
if ($this->params['enable_skip_line']) {
$ok = $this->checkHeader(fgetcsv($f, 0, $this->params['separator'], $this->params['enclosure']));
}
while ($ok && ($cols = fgetcsv($f, 0, $this->params['separator'], $this->params['enclosure']))) {
$ok = call_user_func($this->handlers['readline'], new ArrayExtended($cols));
$this->current_num_line++;
}
fclose($f);
}
if (!$ok) {
@ -101,36 +95,21 @@ class CSVReader
return $this->current_num_line;
}
private function checkHeader($line)
private function checkHeader(array $cols)
{
$ok = true;
if (count($this->params['expected_header'])>0 && is_string($line) && $line!='') {
$cols = $this->lineToArrayExtended($line);
foreach($this->params['expected_header'] as $expected_col => $expected_pos) {
$found = false;
foreach($cols as $index => $col) {
$found = (strtolower($col) == strtolower($expected_col) && $expected_pos == $index);
if ($found) {
break;
}
foreach($this->params['expected_header'] as $expected_col => $expected_pos) {
$found = false;
foreach($cols as $index => $col) {
if (strtolower($col) == strtolower($expected_col) && $expected_pos == $index) {
$found = true;
break;
}
$ok = $ok && $found;
}
$ok = $ok && $found;
}
return $ok;
}
private function lineToArrayExtended($line)
{
$cols = explode($this->params['separator'], $line);
if (!empty($this->params['delimiter'])) {
foreach($cols as $i => $col) {
$cols[$i] = str_replace($this->params['delimiter'], '', $col);
}
}
return new ArrayExtended($cols);
}
}

View File

@ -18,16 +18,19 @@ class CSVWriter
function setHeader(array $line)
{
$this->header = $line;
return $this;
}
function addLine(array $line)
{
$this->lines[] = $line;
return $this;
}
function setFooter(array $line)
{
$this->footer = $line;
return $this;
}
function sortBy($index_column, $ascendant=true)
@ -73,6 +76,7 @@ class CSVWriter
else {
$this->save();
}
return $this;
}
private function outputForDownload()

View File

@ -50,11 +50,11 @@ class TrainingBilan
self::$result = new ArrayExtended(self::$result);
$notFounds = self::$result->filterKeys('TrainingBilan::doesCodeClientExist');
TrainingFixedCodeClient::addCodes($notFounds->toKeys());
TrainingFixedCodeClient::addCodes($notFounds);
self::$csv_output = new CSVWriter($file_output);
if (self::exportResults()) {
self::$csv_output->sortBy(self::OUTPUT_COLUMN_CODECLIENT);
//self::$csv_output->sortBy(self::OUTPUT_COLUMN_CODECLIENT);
self::$csv_output->flush();
return true;
}

View File

@ -32,11 +32,17 @@ class TrainingFixedCodeClient extends ObjectModelExtended
return $result;
}
static function addCodes(array $code_clients)
static function addCodes(ArrayExtended $code_clients)
{
foreach ($code_clients as $code_client) {
$sql = 'SELECT COUNT(*) FROM `'._DB_PREFIX_.'training_fixedcodeclient` WHERE codeclient_from_results = \'%s\'';
if (Db::getInstance()->getValue(sprintf($sql, pSql((string)$code_client))) == 0) {
$already_notfound = array();
$sql = 'SELECT `codeclient_fixed`, `codeclient_from_results`
FROM `'._DB_PREFIX_.'training_fixedcodeclient`';
foreach (Db::getInstance()->executeS($sql) as $row) {
$already_notfound[$row['codeclient_from_results']] = $row['codeclient_fixed'];
}
foreach ($code_clients->toKeys() as $code_client) {
if (!isset($already_notfound[$code_client])) {
$sql = 'INSERT INTO `'._DB_PREFIX_.'training_fixedcodeclient` (codeclient_from_results, codeclient_fixed) VALUES(\'%s\', \'\')';
Db::getInstance()->execute(sprintf($sql, pSql((string)$code_client)));
}

View File

@ -31,16 +31,19 @@ class TrainingModule extends ObjectModelExtended
static function loadAll()
{
$result = new ArrayExtended();
$result = array();
$sql = 'SELECT `module_name`, `category_name`
FROM `'._DB_PREFIX_.'training_module`
ORDER BY `category_name` ASC, `module_name` ASC';
foreach (Db::getInstance()->executeS($sql) as $index => $row) {
$result->addNode($row['category_name'], $index, $row['module_name']);
foreach (Db::getInstance()->executeS($sql) as $row) {
if (!isset($result[$row['category_name']])) {
$result[$row['category_name']] = array();
}
$result[$row['category_name']][] = $row['module_name'];
}
return $result;
return new ArrayExtended($result);
}
static function getExpectedHeader()