Refactor processing of raw rows and their cell data.

This commit is contained in:
Aaron van Geffen 2021-02-17 20:35:30 +01:00
parent 6c662481bc
commit ff808ba18d
1 changed files with 96 additions and 93 deletions

View File

@ -57,8 +57,8 @@ class GenericTable
// Okay, let's fetch the data!
$data = $options['get_data'](...$parameters);
// Clean up a bit.
$rows = $data['rows'];
// Extract data into local variables.
$rawRowData = $data['rows'];
$this->sort_order = $data['order'];
$this->sort_direction = $data['direction'];
unset($data);
@ -71,12 +71,11 @@ class GenericTable
if ($needsPageIndex)
$this->generatePageIndex($options);
// Not a single row in sight?
if (empty($rows))
$this->body = $options['no_items_label'] ?? '';
// Otherwise, parse it all!
// Process the data to be shown into rows.
if (!empty($rawRowData))
$this->processAllRows($rawRowData, $options);
else
$this->parseAllRows($rows, $options);
$this->body = $options['no_items_label'] ?? '';
// Got a title?
$this->title = $options['title'] ?? '';
@ -122,92 +121,6 @@ class GenericTable
]);
}
private function parseAllRows($rows, $options)
{
foreach ($rows as $i => $row)
{
$newRow = [
'cells' => [],
];
foreach ($options['columns'] as $column)
{
// The hard way?
if (isset($column['parse']))
{
if (!isset($column['parse']['type']))
$column['parse']['type'] = 'value';
// Parse the basic value first.
switch ($column['parse']['type'])
{
// value: easy as pie.
default:
case 'value':
$value = $row[$column['parse']['data']];
break;
// sprintf: filling the gaps!
case 'sprintf':
$parameters = [$column['parse']['data']['pattern']];
foreach ($column['parse']['data']['arguments'] as $identifier)
$parameters[] = $row[$identifier];
$value = sprintf(...$parameters);
break;
// timestamps: let's make them readable!
case 'timestamp':
if (empty($column['parse']['data']['pattern']) || $column['parse']['data']['pattern'] === 'long')
$pattern = '%F %H:%M';
elseif ($column['parse']['data']['pattern'] === 'short')
$pattern = '%F';
else
$pattern = $column['parse']['data']['pattern'];
if (!is_numeric($row[$column['parse']['data']['timestamp']]))
$timestamp = strtotime($row[$column['parse']['data']['timestamp']]);
else
$timestamp = (int) $row[$column['parse']['data']['timestamp']];
if (isset($column['parse']['data']['if_null']) && $timestamp == 0)
$value = $column['parse']['data']['if_null'];
else
$value = strftime($pattern, $timestamp);
break;
// function: the flexible way!
case 'function':
$value = $column['parse']['data']($row);
break;
}
// Generate a link, if requested.
if (!empty($column['parse']['link']))
{
// First, generate the replacement variables.
$keys = array_keys($row);
$values = array_values($row);
foreach ($keys as $keyKey => $keyValue)
$keys[$keyKey] = '{' . strtoupper($keyValue) . '}';
$value = '<a href="' . str_replace($keys, $values, $column['parse']['link']) . '">' . $value . '</a>';
}
}
// The easy way!
else
$value = $row[$column['value']];
// Append the cell to the row.
$newRow['cells'][] = [
'value' => $value,
];
}
// Append the new row in the body.
$this->body[] = $newRow;
}
}
public function getLink($start = null, $order = null, $dir = null)
{
if ($start === null)
@ -254,4 +167,94 @@ class GenericTable
{
return $this->title_class;
}
private function processAllRows($rows, $options)
{
foreach ($rows as $i => $row)
{
$newRow = [
'cells' => [],
];
foreach ($options['columns'] as $column)
{
// Process data for this particular cell.
if (isset($column['parse']))
$value = self::processCell($column['parse'], $row);
else
$value = $row[$column['value']];
// Append the cell to the row.
$newRow['cells'][] = [
'value' => $value,
];
}
// Append the new row in the body.
$this->body[] = $newRow;
}
}
private function processCell($options, $rowData)
{
if (!isset($options['type']))
$options['type'] = 'value';
// Parse the basic value first.
switch ($options['type'])
{
// Basic option: simply take a use a particular data property.
case 'value':
$value = $rowData[$options['data']];
break;
// Processing via a lambda function.
case 'function':
$value = $options['data']($rowData);
break;
// Using sprintf to fill out a particular pattern.
case 'sprintf':
$parameters = [$options['data']['pattern']];
foreach ($options['data']['arguments'] as $identifier)
$parameters[] = $rowData[$identifier];
$value = sprintf(...$parameters);
break;
// Timestamps get custom treatment.
case 'timestamp':
if (empty($options['data']['pattern']) || $options['data']['pattern'] === 'long')
$pattern = '%F %H:%M';
elseif ($options['data']['pattern'] === 'short')
$pattern = '%F';
else
$pattern = $options['data']['pattern'];
if (!is_numeric($rowData[$options['data']['timestamp']]))
$timestamp = strtotime($rowData[$options['data']['timestamp']]);
else
$timestamp = (int) $rowData[$options['data']['timestamp']];
if (isset($options['data']['if_null']) && $timestamp == 0)
$value = $options['data']['if_null'];
else
$value = strftime($pattern, $timestamp);
break;
}
// Generate a link, if requested.
if (!empty($options['link']))
{
// First, generate the replacement variables.
$keys = array_keys($rowData);
$values = array_values($rowData);
foreach ($keys as $keyKey => $keyValue)
$keys[$keyKey] = '{' . strtoupper($keyValue) . '}';
$value = '<a href="' . str_replace($keys, $values, $options['link']) . '">' . $value . '</a>';
}
return $value;
}
}