Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #13: [breaking] Column attribute interface changes #26

Open
wants to merge 4 commits into
base: 1.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/Cell/ShadowCell.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ function addClasses(array $classes) {
return $this;
}

/**
* {@inheritdoc}
*/
function setAttribute($key, $value) {
// Do nothing.
return $this;
}

/**
* @param int $rowspan
*
Expand Down
16 changes: 16 additions & 0 deletions src/Columns/ColumnAttributes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Donquixote\Cellbrush\Columns;

/**
* This class is not used anywhere.
*
* It only exists to prove that ColumnAttributesTrait is complete.
*
* @internal
*/
class ColumnAttributes implements ColumnAttributesInterface {

use ColumnAttributesTrait;

}
16 changes: 16 additions & 0 deletions src/Columns/ColumnAttributesInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Donquixote\Cellbrush\Columns;

interface ColumnAttributesInterface {

/**
* @param string $colName
* @param string $name
* @param string $value
*
* @return $this
*/
public function setColAttribute($colName, $name, $value);

}
24 changes: 24 additions & 0 deletions src/Columns/ColumnAttributesTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Donquixote\Cellbrush\Columns;

/**
* @see ColumnAttributesInterface
*/
trait ColumnAttributesTrait {

use ColumnClassesTrait;

function __constructColumnAttributes() {
$this->__constructColumnClasses();
}

/**
* {@inheritdoc}
*/
public function setColAttribute($colName, $name, $value) {
$this->colAttributes->nameSetAttribute($colName, $name, $value);
return $this;
}

}
8 changes: 8 additions & 0 deletions src/Html/AttributesBuilderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,12 @@ function addClass($class);
*/
function addClasses(array $classes);

/**
* @param string $key
* @param string $value
*
* @return static
*/
function setAttribute($key, $value);

}
8 changes: 7 additions & 1 deletion src/Html/Multiple/StaticAttributesMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,13 @@ public function enhanceAttributes(array &$rowCells) {
$rowCells[$name] = $rowCells[$name]->addClasses($classes);
}
}
// @todo Implement for attributes.
foreach ($this->attributes as $name => $attributes) {
if (isset($rowCells[$name])) {
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
3 changes: 2 additions & 1 deletion src/TSection/TableSectionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

namespace Donquixote\Cellbrush\TSection;

use Donquixote\Cellbrush\Columns\ColumnClassesInterface;
use Donquixote\Cellbrush\Html\MutableAttributesInterface;

/**
* Interface for a table section element.
*/
interface TableSectionInterface extends MutableAttributesInterface, TableSectionStructureInterface {
interface TableSectionInterface extends ColumnClassesInterface, MutableAttributesInterface, TableSectionStructureInterface {

}
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
4 changes: 2 additions & 2 deletions src/Table/TableInterface.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\ColumnClassesInterface;
use Donquixote\Cellbrush\Columns\ColumnAttributesInterface;
use Donquixote\Cellbrush\Columns\TableColumnsInterface;
use Donquixote\Cellbrush\Html\MutableAttributesInterface;
use Donquixote\Cellbrush\TSection\TableSectionStructureInterface;
Expand All @@ -11,6 +11,6 @@ interface TableInterface extends
MutableAttributesInterface,
TableSectionStructureInterface,
TableColumnsInterface,
ColumnClassesInterface {
ColumnAttributesInterface {

}
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