Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
Table
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
2 / 2
4
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 columnCount
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3declare(strict_types=1);
4
5namespace Phpdftk\Pdf\Writer;
6
7/**
8 * Tabular data placed via `Pdf::addTable()` (flow) or
9 * `Writer\Page::drawTable()` (positioned).
10 *
11 * Cells are plain strings; multi-line content is produced
12 * automatically by greedy word-wrapping at the column width.
13 * For richer content (mixed fonts, images, nested tables), drop to
14 * the writer's drawing API.
15 *
16 * Column widths are absolute points; if `$columnWidths` is null, the
17 * renderer divides the available width equally across columns.
18 */
19final class Table
20{
21    /**
22     * @param list<list<string>> $rows          Body rows; each row's length should match column count.
23     * @param list<float>|null    $columnWidths Per-column width in points, or null for equal columns.
24     * @param list<string>|null   $headerRow    Optional repeating header row.
25     */
26    public function __construct(
27        public readonly array $rows,
28        public readonly ?array $columnWidths = null,
29        public readonly ?array $headerRow = null,
30    ) {}
31
32    /**
33     * Number of columns in the table — taken from the header row when
34     * present, otherwise from the widest body row.
35     */
36    public function columnCount(): int
37    {
38        if ($this->headerRow !== null) {
39            return count($this->headerRow);
40        }
41        $max = 0;
42        foreach ($this->rows as $row) {
43            $max = max($max, count($row));
44        }
45        return $max;
46    }
47}