Skip to content

Commit b878fe3

Browse files
authored
Fix #65: Add classes for table tags Table, Caption, Colgroup, Col, Thead, Tbody, Tfoot, Tr, Th, Td
1 parent 66e97aa commit b878fe3

30 files changed

Lines changed: 1767 additions & 3 deletions

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
# Yii Html Change Log
1+
# Yii HTML Change Log
22

33
## 1.0.2 under development
44

5+
- Enh #65: Add classes for table tags `Table`, `Caption`, `Colgroup`, `Col`, `Thead`, `Tbody`, `Tfoot`, `Tr`, `Th`, `Td` (vjik)
56
- Enh #69: Add class for tag `Br` (vjik)
67

78
## 1.0.1 April 04, 2021

README.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717

1818
The package provides:
1919

20-
- tag classes `A`, `Br`, `Button`, `Div`, `Img`, `Input` (and specialized `Checkbox`, `Radio`), `Label`, `Li`, `Link`, `Meta`, `Ol`,
21-
`Optgroup`, `Option`, `P`, `Script`, `Select`, `Span`, `Style`, `Textarea`, `Ul`;
20+
- tag classes `A`, `Br`, `Button`, `Div`, `Img`, `Input` (and specialized `Checkbox`, `Radio`), `Label`, `Li`, `Link`,
21+
`Meta`, `Ol`, `Optgroup`, `Option`, `P`, `Script`, `Select`, `Span`, `Style`, `Textarea`, `Ul`, `Table`,
22+
`Caption`, `Colgroup`, `Col`, `Thead`, `Tbody`, `Tfoot`, `Tr`, `Th`, `Td`;
2223
- `CustomTag` class that helps to generate custom tag with any attributes;
2324
- HTML widgets `CheckboxList` and `RadioList`;
2425
- `Html` helper that has static methods to generate HTML, create tag and HTML widget objects.
@@ -229,6 +230,19 @@ Overall the helper has the following method groups.
229230
- textInput
230231
- textarea
231232

233+
#### Table tags
234+
235+
- table
236+
- caption
237+
- colgroup
238+
- col
239+
- thead
240+
- tbody
241+
- tfoot
242+
- tr
243+
- th
244+
- td
245+
232246
### Generating tag parts
233247

234248
- openTag

src/Html.php

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
use Yiisoft\Html\Tag\A;
1111
use Yiisoft\Html\Tag\Br;
1212
use Yiisoft\Html\Tag\Button;
13+
use Yiisoft\Html\Tag\Caption;
14+
use Yiisoft\Html\Tag\Col;
15+
use Yiisoft\Html\Tag\Colgroup;
1316
use Yiisoft\Html\Tag\CustomTag;
1417
use Yiisoft\Html\Tag\Div;
1518
use Yiisoft\Html\Tag\Img;
@@ -28,7 +31,14 @@
2831
use Yiisoft\Html\Tag\Select;
2932
use Yiisoft\Html\Tag\Span;
3033
use Yiisoft\Html\Tag\Style;
34+
use Yiisoft\Html\Tag\Table;
35+
use Yiisoft\Html\Tag\Tbody;
36+
use Yiisoft\Html\Tag\Td;
3137
use Yiisoft\Html\Tag\Textarea;
38+
use Yiisoft\Html\Tag\Tfoot;
39+
use Yiisoft\Html\Tag\Th;
40+
use Yiisoft\Html\Tag\Thead;
41+
use Yiisoft\Html\Tag\Tr;
3242
use Yiisoft\Html\Tag\Ul;
3343
use Yiisoft\Html\Widget\CheckboxList\CheckboxList;
3444
use Yiisoft\Html\Widget\RadioList\RadioList;
@@ -867,6 +877,137 @@ public static function li(string $content = ''): Li
867877
return empty($content) ? $tag : $tag->content($content);
868878
}
869879

880+
/**
881+
* Generates a {@see Caption} tag.
882+
*
883+
* @param string $content Tag content.
884+
* @param array $attributes The tag attributes in terms of name-value pairs.
885+
*/
886+
public static function caption(string $content = '', array $attributes = []): Caption
887+
{
888+
$tag = Caption::tag();
889+
if ($content !== '') {
890+
$tag = $tag->content($content);
891+
}
892+
if ($attributes !== []) {
893+
$tag = $tag->replaceAttributes($attributes);
894+
}
895+
return $tag;
896+
}
897+
898+
/**
899+
* Generates a {@see Col} tag.
900+
*
901+
* @param array $attributes The tag attributes in terms of name-value pairs.
902+
*/
903+
public static function col(array $attributes = []): Col
904+
{
905+
$tag = Col::tag();
906+
return $attributes === [] ? $tag : $tag->replaceAttributes($attributes);
907+
}
908+
909+
/**
910+
* Generates a {@see Colgroup} tag.
911+
*
912+
* @param array $attributes The tag attributes in terms of name-value pairs.
913+
*/
914+
public static function colgroup(array $attributes = []): Colgroup
915+
{
916+
$tag = Colgroup::tag();
917+
return $attributes === [] ? $tag : $tag->replaceAttributes($attributes);
918+
}
919+
920+
/**
921+
* Generates a {@see Table} tag.
922+
*
923+
* @param array $attributes The tag attributes in terms of name-value pairs.
924+
*/
925+
public static function table(array $attributes = []): Table
926+
{
927+
$tag = Table::tag();
928+
return $attributes === [] ? $tag : $tag->replaceAttributes($attributes);
929+
}
930+
931+
/**
932+
* Generates a {@see Thead} tag.
933+
*
934+
* @param array $attributes The tag attributes in terms of name-value pairs.
935+
*/
936+
public static function thead(array $attributes = []): Thead
937+
{
938+
$tag = Thead::tag();
939+
return $attributes === [] ? $tag : $tag->replaceAttributes($attributes);
940+
}
941+
942+
/**
943+
* Generates a {@see Tbody} tag.
944+
*
945+
* @param array $attributes The tag attributes in terms of name-value pairs.
946+
*/
947+
public static function tbody(array $attributes = []): Tbody
948+
{
949+
$tag = Tbody::tag();
950+
return $attributes === [] ? $tag : $tag->replaceAttributes($attributes);
951+
}
952+
953+
/**
954+
* Generates a {@see Tfoot} tag.
955+
*
956+
* @param array $attributes The tag attributes in terms of name-value pairs.
957+
*/
958+
public static function tfoot(array $attributes = []): Tfoot
959+
{
960+
$tag = Tfoot::tag();
961+
return $attributes === [] ? $tag : $tag->replaceAttributes($attributes);
962+
}
963+
964+
/**
965+
* Generates a {@see Tr} tag.
966+
*
967+
* @param array $attributes The tag attributes in terms of name-value pairs.
968+
*/
969+
public static function tr(array $attributes = []): Tr
970+
{
971+
$tag = Tr::tag();
972+
return $attributes === [] ? $tag : $tag->replaceAttributes($attributes);
973+
}
974+
975+
/**
976+
* Generates a {@see Td} tag.
977+
*
978+
* @param string $content Tag content.
979+
* @param array $attributes The tag attributes in terms of name-value pairs.
980+
*/
981+
public static function td(string $content = '', array $attributes = []): Td
982+
{
983+
$tag = Td::tag();
984+
if ($content !== '') {
985+
$tag = $tag->content($content);
986+
}
987+
if ($attributes !== []) {
988+
$tag = $tag->replaceAttributes($attributes);
989+
}
990+
return $tag;
991+
}
992+
993+
/**
994+
* Generates a {@see Th} tag.
995+
*
996+
* @param string $content Tag content.
997+
* @param array $attributes The tag attributes in terms of name-value pairs.
998+
*/
999+
public static function th(string $content = '', array $attributes = []): Th
1000+
{
1001+
$tag = Th::tag();
1002+
if ($content !== '') {
1003+
$tag = $tag->content($content);
1004+
}
1005+
if ($attributes !== []) {
1006+
$tag = $tag->replaceAttributes($attributes);
1007+
}
1008+
return $tag;
1009+
}
1010+
8701011
/**
8711012
* Generates a {@see Br} tag.
8721013
*/

src/Tag/Base/TableCellTag.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yiisoft\Html\Tag\Base;
6+
7+
abstract class TableCellTag extends NormalTag
8+
{
9+
use TagContentTrait;
10+
11+
/**
12+
* Number of columns that the cell is to span.
13+
*
14+
* @return static
15+
*/
16+
final public function colSpan(?int $number): self
17+
{
18+
$new = clone $this;
19+
$new->attributes['colspan'] = $number;
20+
return $new;
21+
}
22+
23+
/**
24+
* Number of rows that the cell is to span.
25+
*
26+
* @return static
27+
*/
28+
final public function rowSpan(?int $number): self
29+
{
30+
$new = clone $this;
31+
$new->attributes['rowspan'] = $number;
32+
return $new;
33+
}
34+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yiisoft\Html\Tag\Base;
6+
7+
use Yiisoft\Html\Tag\Tr;
8+
9+
abstract class TableRowsContainerTag extends NormalTag
10+
{
11+
private array $rows = [];
12+
13+
/**
14+
* @param Tr ...$rows One or more rows ({@see Tr}).
15+
*
16+
* @return static
17+
*/
18+
final public function rows(Tr ...$rows): self
19+
{
20+
$new = clone $this;
21+
$new->rows = $rows;
22+
return $new;
23+
}
24+
25+
/**
26+
* @param Tr ...$rows One or more rows ({@see Tr}).
27+
*
28+
* @return static
29+
*/
30+
final public function addRows(Tr ...$rows): self
31+
{
32+
$new = clone $this;
33+
$new->rows = array_merge($new->rows, $rows);
34+
return $new;
35+
}
36+
37+
final protected function generateContent(): string
38+
{
39+
return $this->rows
40+
? "\n" . implode("\n", $this->rows) . "\n"
41+
: '';
42+
}
43+
}

src/Tag/Caption.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yiisoft\Html\Tag;
6+
7+
use Yiisoft\Html\Tag\Base\NormalTag;
8+
use Yiisoft\Html\Tag\Base\TagContentTrait;
9+
10+
/**
11+
* @link https://www.w3.org/TR/html52/tabular-data.html#the-caption-element
12+
*/
13+
final class Caption extends NormalTag
14+
{
15+
use TagContentTrait;
16+
17+
protected function getName(): string
18+
{
19+
return 'caption';
20+
}
21+
}

src/Tag/Col.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yiisoft\Html\Tag;
6+
7+
use Yiisoft\Html\Tag\Base\VoidTag;
8+
9+
/**
10+
* @link https://www.w3.org/TR/html52/tabular-data.html#the-col-element
11+
*/
12+
final class Col extends VoidTag
13+
{
14+
/**
15+
* @param int|null $number The number of consecutive columns the `<col>` element spans.
16+
*/
17+
public function span(?int $number): self
18+
{
19+
$new = clone $this;
20+
$new->attributes['span'] = $number;
21+
return $new;
22+
}
23+
24+
protected function getName(): string
25+
{
26+
return 'col';
27+
}
28+
}

src/Tag/Colgroup.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yiisoft\Html\Tag;
6+
7+
use Yiisoft\Html\Tag\Base\NormalTag;
8+
9+
/**
10+
* @link https://www.w3.org/TR/html52/tabular-data.html#the-colgroup-element
11+
*/
12+
final class Colgroup extends NormalTag
13+
{
14+
private array $columns = [];
15+
16+
/**
17+
* @param Col ...$columns One or more columns ({@see Col}).
18+
*/
19+
public function columns(Col ...$columns): self
20+
{
21+
$new = clone $this;
22+
$new->columns = $columns;
23+
return $new;
24+
}
25+
26+
/**
27+
* @param Col ...$columns One or more columns ({@see Col}).
28+
*/
29+
public function addColumns(Col ...$columns): self
30+
{
31+
$new = clone $this;
32+
$new->columns = array_merge($new->columns, $columns);
33+
return $new;
34+
}
35+
36+
/**
37+
* @param int|null $number The number of consecutive columns the `<colgroup>` element spans.
38+
*/
39+
public function span(?int $number): self
40+
{
41+
$new = clone $this;
42+
$new->attributes['span'] = $number;
43+
return $new;
44+
}
45+
46+
protected function generateContent(): string
47+
{
48+
return $this->columns
49+
? "\n" . implode("\n", $this->columns) . "\n"
50+
: '';
51+
}
52+
53+
protected function getName(): string
54+
{
55+
return 'colgroup';
56+
}
57+
}

0 commit comments

Comments
 (0)