From 36bd544c0d22a7cdb6e153eb639bff205768d901 Mon Sep 17 00:00:00 2001 From: Andreas Hennings Date: Sun, 27 Oct 2024 21:04:28 +0100 Subject: [PATCH] Issue #13: Add setColAttribute(). --- src/Cell/ShadowCell.php | 11 ++++++++++ src/Columns/ColumnAttributesTrait.php | 25 +++++++++++++++++++++++ src/Html/Multiple/StaticAttributesMap.php | 9 +++++++- src/TSection/TableSection.php | 6 +++--- src/Table/Table.php | 6 +++--- tests/src/CellbrushTest.php | 20 ++++++++++-------- 6 files changed, 62 insertions(+), 15 deletions(-) create mode 100644 src/Columns/ColumnAttributesTrait.php diff --git a/src/Cell/ShadowCell.php b/src/Cell/ShadowCell.php index 7960f66..2dfc577 100644 --- a/src/Cell/ShadowCell.php +++ b/src/Cell/ShadowCell.php @@ -27,6 +27,17 @@ function addClasses(array $classes) { return $this; } + /** + * @param string $key + * @param string $value + * + * @return static + */ + function setAttribute($key, $value) { + // Do nothing. + return $this; + } + /** * @param int $rowspan * diff --git a/src/Columns/ColumnAttributesTrait.php b/src/Columns/ColumnAttributesTrait.php new file mode 100644 index 0000000..85b20c2 --- /dev/null +++ b/src/Columns/ColumnAttributesTrait.php @@ -0,0 +1,25 @@ +__constructColumnClasses(); + } + + /** + * @param string $colName + * @param string $name + * @param string $value + * + * @return $this + */ + public function setColAttribute($colName, $name, $value) { + $this->colAttributes->nameSetAttribute($colName, $name, $value); + return $this; + } + +} diff --git a/src/Html/Multiple/StaticAttributesMap.php b/src/Html/Multiple/StaticAttributesMap.php index 451948b..1b6779d 100644 --- a/src/Html/Multiple/StaticAttributesMap.php +++ b/src/Html/Multiple/StaticAttributesMap.php @@ -32,7 +32,14 @@ public function enhanceAttributes(array &$rowCells) { $rowCells[$name] = $rowCells[$name]->addClasses($classes); } } - // @todo Implement for attributes. + foreach ($this->attributes as $name => $attributes) { + // Currently the ->setAttribute() method is not part of any interface. + if (isset($rowCells[$name]) && method_exists($rowCells[$name], 'setAttribute')) { + foreach ($attributes as $key => $value) { + $rowCells[$name] = $rowCells[$name]->setAttribute($key, $value); + } + } + } } /** diff --git a/src/TSection/TableSection.php b/src/TSection/TableSection.php index 51909ba..9685399 100644 --- a/src/TSection/TableSection.php +++ b/src/TSection/TableSection.php @@ -6,7 +6,7 @@ use Donquixote\Cellbrush\Axis\DynamicAxis; use Donquixote\Cellbrush\BuildContainer\BuildContainer; use Donquixote\Cellbrush\BuildContainer\BuildContainerBase; -use Donquixote\Cellbrush\Columns\ColumnClassesTrait; +use Donquixote\Cellbrush\Columns\ColumnAttributesTrait; use Donquixote\Cellbrush\Handle\RowHandle; use Donquixote\Cellbrush\Handle\SectionColHandle; use Donquixote\Cellbrush\Html\Multiple\DynamicAttributesMap; @@ -15,7 +15,7 @@ class TableSection implements TableSectionInterface { - use MutableAttributesTrait, ColumnClassesTrait; + use MutableAttributesTrait, ColumnAttributesTrait; /** * @var string @@ -83,7 +83,7 @@ class TableSection implements TableSectionInterface { */ function __construct($tagName) { $this->__constructMutableAttributes(); - $this->__constructColumnClasses(); + $this->__constructColumnAttributes(); $this->tagName = $tagName; $this->rows = new DynamicAxis(); $this->colAttributes = new DynamicAttributesMap(); diff --git a/src/Table/Table.php b/src/Table/Table.php index e48261d..628490d 100644 --- a/src/Table/Table.php +++ b/src/Table/Table.php @@ -2,7 +2,7 @@ namespace Donquixote\Cellbrush\Table; -use Donquixote\Cellbrush\Columns\ColumnClassesTrait; +use Donquixote\Cellbrush\Columns\ColumnAttributesTrait; use Donquixote\Cellbrush\Columns\TableColumnsTrait; use Donquixote\Cellbrush\Handle\RowHandle; use Donquixote\Cellbrush\Html\Multiple\DynamicAttributesMap; @@ -11,7 +11,7 @@ class Table extends TBodyWrapper implements TableInterface { - use MutableAttributesTrait, TableColumnsTrait, ColumnClassesTrait; + use MutableAttributesTrait, TableColumnsTrait, ColumnAttributesTrait; /** * @var TableSection @@ -37,7 +37,7 @@ class Table extends TBodyWrapper implements TableInterface { function __construct() { $this->__constructMutableAttributes(); $this->__constructTableColumns(); - $this->__constructColumnClasses(); + $this->__constructColumnAttributes(); $this->columns; $this->thead = new TableSection('thead'); parent::__construct(new TableSection('tbody')); diff --git a/tests/src/CellbrushTest.php b/tests/src/CellbrushTest.php index e135f30..1fe35bb 100644 --- a/tests/src/CellbrushTest.php +++ b/tests/src/CellbrushTest.php @@ -934,14 +934,15 @@ function testCellAttributes() { ->td('row2', 'col2', 'Diag 2') ->setCellAttribute('row0', 'col1', 'attr', 'testattr') ->setCellAttribute('row2', 'col2', 'attr2', 'testattr2') + ->setColAttribute('col1', 'title', 'col 1 title') ; $expected = << - Diag 0 - Diag 1 - Diag 2 + Diag 0 + Diag 1 + Diag 2 @@ -971,7 +972,10 @@ function testRowAndColGroupsWithAttributes() { ->setCellAttribute('banana', 'name', 'title', 'banana name') ->setCellAttribute('banana.description', 'info', 'title', 'banana description') ->setCellAttribute('banana.info', 'info.price', 'title', 'banana price') + ->setColAttribute('info', 'col-attr', 'info col') + ->setColAttribute('info.price', 'col-attr', 'price col') ; + $table->tbody()->setColAttribute('info.color', 'col-attr', 'color tbody col'); $table->headRow() ->th('name', 'Name') ->th('info.color', 'Color') @@ -981,13 +985,13 @@ function testRowAndColGroupsWithAttributes() { $expected = << - NameColorPrice + NameColorPrice - BananaA yellow fruit. - yellow60 cent - CoconutHas liquid inside. - brown3 dollar + BananaA yellow fruit. + yellow60 cent + CoconutHas liquid inside. + brown3 dollar