Skip to content

Commit

Permalink
Issue #13: Add setColAttribute().
Browse files Browse the repository at this point in the history
  • Loading branch information
donquixote committed Oct 31, 2024
1 parent 3f856f2 commit 36bd544
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 15 deletions.
11 changes: 11 additions & 0 deletions src/Cell/ShadowCell.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
25 changes: 25 additions & 0 deletions src/Columns/ColumnAttributesTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Donquixote\Cellbrush\Columns;

trait ColumnAttributesTrait {

use ColumnClassesTrait;

function __constructColumnAttributes() {
$this->__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;
}

}
9 changes: 8 additions & 1 deletion src/Html/Multiple/StaticAttributesMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/TSection/TableSection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -15,7 +15,7 @@

class TableSection implements TableSectionInterface {

use MutableAttributesTrait, ColumnClassesTrait;
use MutableAttributesTrait, ColumnAttributesTrait;

/**
* @var string
Expand Down Expand Up @@ -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();
Expand Down
6 changes: 3 additions & 3 deletions src/Table/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -11,7 +11,7 @@

class Table extends TBodyWrapper implements TableInterface {

use MutableAttributesTrait, TableColumnsTrait, ColumnClassesTrait;
use MutableAttributesTrait, TableColumnsTrait, ColumnAttributesTrait;

/**
* @var TableSection
Expand All @@ -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'));
Expand Down
20 changes: 12 additions & 8 deletions tests/src/CellbrushTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <<<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>
<tr><td>Diag 0</td><td title="col 1 title" attr="testattr"></td><td></td></tr>
<tr><td></td><td title="col 1 title">Diag 1</td><td></td></tr>
<tr><td></td><td title="col 1 title"></td><td attr2="testattr2">Diag 2</td></tr>
</tbody>
</table>
Expand Down Expand Up @@ -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')
Expand All @@ -981,13 +985,13 @@ function testRowAndColGroupsWithAttributes() {
$expected = <<<EOT
<table>
<thead>
<tr><th>Name</th><th>Color</th><th>Price</th></tr>
<tr><th>Name</th><th>Color</th><th col-attr="price col">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>
<tr><th title="banana name" rowspan="2">Banana</th><td title="banana description" col-attr="info col" colspan="2">A yellow fruit.</td></tr>
<tr><td col-attr="color tbody col">yellow</td><td title="banana price" col-attr="price col">60 cent</td></tr>
<tr><th rowspan="2">Coconut</th><td col-attr="info col" colspan="2">Has liquid inside.</td></tr>
<tr><td col-attr="color tbody col">brown</td><td col-attr="price col">3 dollar</td></tr>
</tbody>
</table>
Expand Down

0 comments on commit 36bd544

Please sign in to comment.