Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
83 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
namespace GtwlSlurp; | ||
|
||
class WikiTable { | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private $headings; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private $rows; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private $config; | ||
|
||
/** | ||
* @param string[] $headings | ||
* @param array[] $rows | ||
* @param array $config | ||
* string title (optional) | ||
*/ | ||
public function __construct( $headings, $rows, $config ) { | ||
$this->headings = $headings; | ||
$this->rows = $rows; | ||
$this->config = $config; | ||
} | ||
|
||
public function __toString() { | ||
return $this->getTableStart() . | ||
$this->getTableHeadings() . | ||
$this->getTableRows() . | ||
$this->getTableEnd(); | ||
} | ||
|
||
private function getTableStart() { | ||
$string = '{| class="wikitable" style="text-align: center;"' . "\n"; | ||
if ( array_key_exists( 'title', $this->config ) ) { | ||
$string .= '|+ ' . $this->config['title'] . "\n"; | ||
} | ||
return $string; | ||
} | ||
|
||
private function getTableHeadings() { | ||
$headings = ''; | ||
foreach( $this->headings as $heading ) { | ||
$headings .= '! ' . $heading . "\n"; | ||
} | ||
return $headings; | ||
} | ||
|
||
private function getTableRows() { | ||
$rowsString = ''; | ||
foreach( $this->rows as $values ) { | ||
$rowsString .= "|-\n"; | ||
foreach( $values as $value ) { | ||
$rowsString .= "| $value\n"; | ||
} | ||
} | ||
return $rowsString; | ||
} | ||
|
||
private function getTableEnd() { | ||
return '|}'; | ||
} | ||
|
||
} |