An Adapter in Table Generator is a key component that allows data transformation from various sources into a structured table format. It provides a seamless way to map raw data (e.g., arrays, database results) into a table representation without manual row and cell creation.
In other words, an adapter is responsible for extracting table structure from a given data source. It does not render a table but rather converts the received data into table components and configures pagination settings.
Adapter Interface
Every adapter implements the AdapterInterface
, which defines the following methods:
interface AdapterInterface {
public function getTheadTr(): Tr;
public function getTbodyTrCollection(): TrCollection;
public function getPaginator(): Paginator;
}
Key Responsibilities
- Accept a specific type of data.
- Extract column headers and data rows.
- Return a
Tr
instance forThead
whengetTheadTr()
is called. - Return a paginated
TrCollection
forTbody
whengetTbodyTrCollection()
is called. - Configure pagination settings within the Paginator class.
Pagination Handling
The adapter does not handle table rendering but configures the paginator based on the dataset.
For instance, if there is a total of 100 rows extracted from a data source and the user requests page 3 with 10 rows per page, the adapter will provide a TrCollection
containing data of the 30th - 39th rows.
Available Adapters
Table Generator provides built-in adapters for common data sources:
Adapter | Description |
---|---|
AssocArrayAdapter |
Converts an associative array into a table format. |
CsvArrayAdapter |
Processes CSV-like data (array-based CSV format). |
MysqliResultAdapter |
Converts a MySQLi result set into a table. |
Anticipated Adapters
The following adapters are planned for future development and will be available soon. Nevertheless, you can contribute by creating one of them (or a non-listed adapter) and make a pull request to help accelerate the process and support the project's growth.
Adapter | Description |
---|---|
PDOStatementAdapter |
Converts a PostgreSQL result set into a table. |
DoctrineORMAdapter |
Adapts Doctrine ORM entity results into a table. |
DoctrineDBALAdapter |
Converts Doctrine DBAL query results into a table. |
How the Adapter works
Most adapters extend the AbstractAdapter
class, whose constructor accepts:
$param1
(mixed): The data source.$param2
(optional): APaginator
instance.
Example:
$assocArray = [
['id' => 1, 'name' => 'John Doe', 'email' => 'john@example.com'],
['id' => 2, 'name' => 'Jane Doe', 'email' => 'jane@example.com'],
['id' => 3, 'name' => 'Alice Smith', 'email' => 'alice@example.com'],
['id' => 4, 'name' => 'Bob Johnson', 'email' => 'bob@example.com'],
['id' => 5, 'name' => 'Charlie Brown', 'email' => 'charlie@example.com'],
];
$adapter = new AssocArrayAdapter(
$assocArray,
new Paginator(null, 3, 1, 'http://example.com/page/(:num)')
);
Here:
null
— Total items (the adapter internally updates this value).3
— Number of rows per page.1
— Current page (Page 1).'http://example.com/page/(:num)'
— URL pattern for pagination.
With this, the adapter will provide a TrCollection
containing only data of row 1, 2 and 3.
You can get the updated paginator and switch to the next page.
$adapter->getTbodyTrCollection()->count(); // 3
$adapter->getPaginator()->getTotalItems(); // 5
$adapter->getPaginator()->getNextUrl(); // http://example.com/page/2
Example Usage
Using the AssocArrayAdapter
to generate a table from an associative array:
$data = [
['id' => 1, 'name' => 'John Doe', 'email' => 'john@example.com'],
['id' => 2, 'name' => 'Jane Doe', 'email' => 'jane@example.com'],
];
$adapter = new AssocArrayAdapter($data);
$tableGenerator = new TableGenerator($adapter);
echo $tableGenerator->render();
This will produce:
<table>
<thead>
<tr>
<th>id</th>
<th>name</th>
<th>email</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>John Doe</td>
<td>john@example.com</td>
</tr>
<tr>
<td>2</td>
<td>Jane Doe</td>
<td>jane@example.com</td>
</tr>
</tbody>
</table>
Adapters simplify table generation by automating the transformation of structured data into an HTML table. Custom adapters can be created for additional flexibility, making Table Generator highly adaptable to various data sources.