How to format cell values

In many cases, table data needs to be formatted before display. For example:

  • Masking sensitive data (e.g., secret_key should be displayed as ****).
  • Converting raw values to readable formats (e.g., price stored in cents should be formatted as USD currency).

This can be efficiently handled using a middleware that modifies specific columns.


Create a FormatCellMiddleware

The iterateCellsIn() method simplifies iterating over all cells in a table, making it easier to modify their values.

use Ucscode\HtmlComponent\TableGenerator\TableGenerator;
use Ucscode\HtmlComponent\TableGenerator\Abstraction\AbstractMiddleware;
use Ucscode\HtmlComponent\TableGenerator\Component\Section\Td;
use Ucscode\HtmlComponent\TableGenerator\Component\Section\Th;
use Ucscode\HtmlComponent\TableGenerator\Contracts\TableSegmentInterface;
use Ucscode\HtmlComponent\TableGenerator\Component\Section\Tr;

class FormatCellMiddleware extends AbstractMiddleware
{
    public function process(Table $table): void
    {
        // Iterate through all cells
        $this->iterateCellsIn($table, function (CellInterface $cell, Tr $tr, TableSegmentInterface $segment) {

            // Check if it's the header row
            if ($segment instanceof Thead) {
                $this->formatHeading($cell);
                return;
            }

            // Format values in body 
            $this->formatValue($cell);
        });
    }

    // Most built-in adapters will store the original value of a cell 
    // in the Cell::Meta instance.

    // If you use a custom adapter, you have to get the value based on how
    // you stored it. It's recommended to use the Meta instance as well

    private function formatHeading(CellInterface $cell): void
    {
        // The cell is most probably (Th)

        if ($cell->getMeta()->get('columnName') === 'secret_key') {
            $cell->setData('Secret Key');
        }

        if ($cell->getMeta()->get('columnName') === 'price') {
            $cell->setData('Price (USD)');
        }
    }

    private function formatValue(CellInterface $cell)
    {
        // The cell is most probably (Td)

        if ($cell->getMeta()->get('columnName') === 'secret_key') {
            // Mask secret_key data
            $cell->setData('****');
        }

        if ($cell->getMeta()->get('columnName') === 'price') {
            // Convert price from cents to USD
            $cell->setData('$' . number_format($value / 100, 2));
        }
    }
}

Use the FormatCellMiddleware on the TableGenerator
// Load data (assuming $adapter fetches product data)
$tableGenerator = new TableGenerator($adapter, new FormatCellMiddleware());

// Render the table
echo $tableGenerator->render();

Expected Output

This middleware ensures sensitive data is masked and numerical values are displayed in a more user-friendly format.

Secret Key Price (USD)
**** $435.50
**** $299.99
Source Code
If you find this project useful, consider leaving a on GitHub! Thank you!