Skip to content

Commit

Permalink
Output a wikitable
Browse files Browse the repository at this point in the history
  • Loading branch information
addshore committed Oct 15, 2015
1 parent a785843 commit 32b0413
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/Slurper.php
Expand Up @@ -53,14 +53,24 @@ private function output( $result ) {
asort( $result );
$result = array_reverse( $result, true );

$tableRows = array();

$total = 0;
foreach( $result as $wishName => $voters ) {
if( $this->translate ) {
$wishName = $this->translate->translate( $wishName );
}
echo "$voters - $wishName\n";
$tableRows[] = array( $wishName, $voters );
$total += $voters;
}

$table = new WikiTable(
array( 'Wish', 'Votes' ),
$tableRows,
array( 'title' => 'GTWL Votes' )
);

echo $table . "\n";
echo "Total Votes: $total\n";
}

Expand Down
72 changes: 72 additions & 0 deletions src/WikiTable.php
@@ -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 '|}';
}

}

0 comments on commit 32b0413

Please sign in to comment.