Skip to content

Commit

Permalink
Merge pull request #19 from donquixote/issue-17-cell-attribute
Browse files Browse the repository at this point in the history
Issue #17: Add attribute on specific cell
  • Loading branch information
donquixote authored Oct 31, 2024
2 parents 113b88e + a2f1ab1 commit 3ccd394
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/BuildContainer/BuildContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ protected function get_NamedCells() {
$cellContents = $this->CellContents;
$cellTagNames = $this->CellTagNames;
$cellClasses = $this->CellClasses;
$cellAttributes = $this->CellAttributes;
/** @var Cell\Cell[][] $namedCells */
$namedCells = [];
foreach ($cellContents as $rowName => $rowCellContents) {
Expand All @@ -89,6 +90,9 @@ protected function get_NamedCells() {
$rowCellClasses = isset($cellClasses[$rowName])
? $cellClasses[$rowName]
: [];
$rowCellAttributes = isset($cellAttributes[$rowName])
? $cellAttributes[$rowName]
: [];
$rowNamedCells = [];
foreach ($rowCellContents as $colName => $cellContent) {
$cellTagName = isset($rowCellTagNames[$colName])
Expand All @@ -98,6 +102,11 @@ protected function get_NamedCells() {
if (isset($rowCellClasses[$colName])) {
$cell = $cell->addClasses($rowCellClasses[$colName]);
}
if (isset($rowCellAttributes[$colName])) {
foreach ($rowCellAttributes[$colName] as $key => $value) {
$cell = $cell->setAttribute($key, $value);
}
}
$rowNamedCells[$colName] = $cell;
}
$namedCells[$rowName] = $rowNamedCells;
Expand Down Expand Up @@ -168,6 +177,23 @@ protected function get_CellMatrix() {

$matrix->setCellClasses($cellClassesIndexed);

$cellAttributesIndexed = [];
foreach ($this->CellAttributes as $rowName => $rowCellAttributes) {
if ($this->rows->nameIsLeaf($rowName)) {
$iRow = $this->rows->subtreeIndex($rowName);
$rowCellAttributesIndexed = [];
foreach ($rowCellAttributes as $colName => $attributes) {
if ($this->columns->nameIsLeaf($colName)) {
$iCol = $this->columns->subtreeIndex($colName);
$rowCellAttributesIndexed[$iCol] = $attributes;
}
}
$cellAttributesIndexed[$iRow] = $rowCellAttributesIndexed;
}
}

$matrix->setCellAttributes($cellAttributesIndexed);

foreach ($this->NamedCells as $rowName => $rowCells) {
$rowRange = $this->rows->subtreeRange($rowName);
foreach ($rowCells as $colName => $cell) {
Expand Down
11 changes: 11 additions & 0 deletions src/BuildContainer/BuildContainerBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* @property string[][] CellContents
* @property string[][] CellTagNames
* @property string[][][] CellClasses
* @property string[][][] CellAttributes
* @property bool[][] OpenEndCells
* Array marking those cells that are open-end.
* Format: $[$rowName][$colName] = true.
Expand All @@ -26,6 +27,7 @@ public function __construct() {
'CellContents' => [],
'CellTagNames' => [],
'CellClasses' => [],
'CellAttributes' => [],
'OpenEndCells' => [],
'RowAttributes' => new StaticAttributesMap(),
'RowStripings' => [],
Expand Down Expand Up @@ -61,6 +63,15 @@ protected function validate_CellClasses(array $cellClasses) {
// No validation, always accept.
}

/**
* @param string[][][] $cellClasses
*
* @see BuildContainerBase::$CellAttributes
*/
protected function validate_CellAttributes(array $cellAttributes) {
// No validation, always accept.
}

/**
* @param bool[][] $openEndCells
*
Expand Down
13 changes: 13 additions & 0 deletions src/Matrix/CellMatrix.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,19 @@ public function setCellClasses(array $cellClasses) {
}
}

/**
* @param string[][][] $cellAttributes
*/
public function setCellAttributes(array $cellAttributes) {
foreach ($cellAttributes as $iRow => $rowCellAttributes) {
foreach ($rowCellAttributes as $iCol => $attributes) {
foreach ($attributes as $key => $value) {
$this->cells[$iRow][$iCol] = $this->cells[$iRow][$iCol]->setAttribute($key, $value);
}
}
}
}

/**
* @return \Donquixote\Cellbrush\Cell\CellInterface[][]
*/
Expand Down
24 changes: 24 additions & 0 deletions src/TSection/TableSection.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@ class TableSection implements TableSectionInterface {
*/
private $cellClasses = [];

/**
* Attributes for table cells.
*
* These can be set even if no content is set for this cell yet.
*
* @var string[][][]
* Format: $[$rowName][$colName][$key] = $value
*/
private $cellAttributes = [];

/**
* @param string $tagName
*/
Expand Down Expand Up @@ -244,6 +254,19 @@ public function addCellClass($rowName, $colName, $class) {
return $this;
}

/**
* @param string $rowName
* @param string $colName
* @param string $name
* @param string $value
*
* @return $this
*/
public function setCellAttribute($rowName, $colName, $name, $value) {
$this->cellAttributes[$rowName][$colName][$name] = $value;
return $this;
}

/**
* @param Axis $columns
* Either 'thead' or 'tbody' or 'tfoot'.
Expand All @@ -265,6 +288,7 @@ function render(Axis $columns, StaticAttributesMap $tableColAttributes) {
/** @var BuildContainerBase $container */
$container->CellContents = $this->cellContents;
$container->CellClasses = $this->cellClasses;
$container->CellAttributes = $this->cellAttributes;
$container->CellTagNames = $this->cellTagNames;
$container->OpenEndCells = $this->openEndCells;
$container->RowAttributes = clone $this->rowAttributes->staticCopy();
Expand Down
13 changes: 13 additions & 0 deletions src/Table/TBodyWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,19 @@ public function addCellClass($rowName, $colName, $class) {
return $this;
}

/**
* @param string $rowName
* @param string $colName
* @param string $name
* @param string $value
*
* @return $this
*/
public function setCellAttribute($rowName, $colName, $name, $value) {
$this->tbody->setCellAttribute($rowName, $colName, $name, $value);
return $this;
}

/**
* @param Axis $columns
* @param StaticAttributesMap $tableColAttributes
Expand Down
72 changes: 72 additions & 0 deletions tests/src/CellbrushTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,78 @@ function testRowAndColGroupsWithClasses() {
</tbody>
</table>
EOT;

$this->assertEquals($expected, $table->render());
}

function testCellAttributes() {

$table = Table::create()
->addRowNames(['row0', 'row1', 'row2'])
->addColNames(['col0', 'col1', 'col2'])
->td('row0', 'col0', 'Diag 0')
->td('row1', 'col1', 'Diag 1')
->td('row2', 'col2', 'Diag 2')
->setCellAttribute('row0', 'col1', 'attr', 'testattr')
->setCellAttribute('row2', 'col2', 'attr2', 'testattr2')
;

$expected = <<<EOT
<table>
<tbody>
<tr><td>Diag 0</td><td attr="testattr"></td><td></td></tr>
<tr><td></td><td>Diag 1</td><td></td></tr>
<tr><td></td><td></td><td attr2="testattr2">Diag 2</td></tr>
</tbody>
</table>
EOT;

$this->assertEquals($expected, $table->render());
}

/**
* Tests row groups and column groups with attributes.
*
* @see self::testRowAndColGroups()
*/
function testRowAndColGroupsWithAttributes() {
$table = Table::create()
->addColNames(['name', 'info.color', 'info.price'])
->addRowNames(['banana.description', 'banana.info'])
->th('banana', 'name', 'Banana')
->td('banana.description', 'info', 'A yellow fruit.')
->td('banana.info', 'info.color', 'yellow')
->td('banana.info', 'info.price', '60 cent')
->addRowNames(['coconut.description', 'coconut.info'])
->th('coconut', 'name', 'Coconut')
->td('coconut.description', 'info', 'Has liquid inside.')
->td('coconut.info', 'info.color', 'brown')
->td('coconut.info', 'info.price', '3 dollar')
->setCellAttribute('banana', 'name', 'title', 'banana name')
->setCellAttribute('banana.description', 'info', 'title', 'banana description')
->setCellAttribute('banana.info', 'info.price', 'title', 'banana price')
;
$table->headRow()
->th('name', 'Name')
->th('info.color', 'Color')
->th('info.price', 'Price')
;

$expected = <<<EOT
<table>
<thead>
<tr><th>Name</th><th>Color</th><th>Price</th></tr>
</thead>
<tbody>
<tr><th title="banana name" rowspan="2">Banana</th><td title="banana description" colspan="2">A yellow fruit.</td></tr>
<tr><td>yellow</td><td title="banana price">60 cent</td></tr>
<tr><th rowspan="2">Coconut</th><td colspan="2">Has liquid inside.</td></tr>
<tr><td>brown</td><td>3 dollar</td></tr>
</tbody>
</table>
EOT;

$this->assertEquals($expected, $table->render());
Expand Down

0 comments on commit 3ccd394

Please sign in to comment.