Skip to content

Instantly share code, notes, and snippets.

@saksmt
Last active August 29, 2015 14:17
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save saksmt/68c39037ccabadbbd987 to your computer and use it in GitHub Desktop.
Save saksmt/68c39037ccabadbbd987 to your computer and use it in GitHub Desktop.
Abstraction on table rendering
<?php
/**
* @example
* $htmlRenderer = new HtmlRenderer();
* $consoleRenderer = new ConsoleRenderer();
* $htmlRenderer->renderToFile($view, 'some.html', [ TableRendererInterface::STREAMED ]);
* echo $consoleRenderer->renderToString($profilingView);
*/
interface TableRendererInterface
{
/**
* @const string Specifies that result should be returned as string
*/
const TO_STRING = 'string';
/**
* @const string Specifies that result should be saved to file
*/
const TO_FILE = 'file';
/**
* @const string Specifies that result should be written to stdout
*/
const TO_STDOUT = 'stdout';
/**
* @const string Specifies that write process should be done with streams
* @NOTE Only applicable with @link TableRendererInterface::TO_FILE or @link TableRendererInterface::TO_STDOUT
*/
const STREAMED = 'streamed';
/**
* Render view with specified options
* @param TableViewInterface $view View to render
* @param string $renderTo Where view should be rendered
* @param array $options Options like path to file where result should be saved
* @return string|null Rendering result
*/
public function render(TableViewInterface $view, $renderTo = self::TO_STDOUT, array $options = []);
/**
* Render view to specified file
* @param TableViewInterface $view View to render
* @param string $path Path to file where rendering result should be saved
* @param array $options Options like flushing, etc
* @return TableRendererInterface
*/
public function renderToFile(TableViewInterface $view, $path, array $options = []);
/**
* Render view to stdout
* @param TableViewInterface $view View to render
* @param array $options Options like flushing, etc
* @return TableRendererInterface
*/
public function renderToStdOut(TableViewInterface $view, array $options = []);
/**
* Render and return the view as string
* @param TableViewInterface $view View to render
* @param array $options Some options
* @return string Rendered view
*/
public function renderToString(TableViewInterface $view, array $options = []);
}
<?php
/**
* @example
* $view = TableView::create($someComplexData)
* ->row()
* ->column('Hello')
* ->column('World')
* ->column('!', [ 'attr' => [ 'style' => 'font-weight: bold;' ] ]) // only example,
* // I would never write code like this one
* ->end()
* ->row()
* ->complexChild($anotherComplexData)
* ->row()
* ->column($contents)
* ->column($anotherContents)
* ->end()
* ->row()
* ->addChild($complexDataAgain)
* ->end()
* ->end()
* ->column($lastComplexDataIPromise)
* ->end();
* $anotherView = TableView::create($iLied);
* $view->row()->addChild($anotherView)->end();
* var_dump($anotherView->parent() === $view); // true
*/
interface TableViewInterface
{
/**
* Add complex child
* @param array|TableViewInterface|TableViewInterface[] $child
* @return TableViewInterface
*/
public function addChild($child);
/**
* Start row
* @return TableViewInterface
*/
public function row();
/**
* Close row
* @return TableViewInterface
*/
public function closeRow();
/**
* Add column to current row
* @param string $contents Data to display
* @param array $options Render options like css-class and others
* @return TableViewInterface
*/
public function column($contents, array $options = []);
/**
* Set render options
* @param array $options Render options
* @return TableViewInterface
*/
public function setOptions(array $options);
/**
* Create new complex row
* @param array|TableViewInterface|TableViewInterface[] $children
* @return TableViewInterface
*/
public function complexColumn($children = []);
/**
* End currently opened column|row
* @return TableViewInterface
*/
public function end();
/**
* Get parent
* @return TableViewInterface|null
*/
public function parent();
/**
* Factory method
* @return TableViewInterface
*/
public static function create();
/**
* Get contents of current node
* @return string|TableViewInterface|TableViewInterface[]|null
*/
public function getContents();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment