cvs: pear /Console_Table Table.php

From: Date: Mon, 17 Jan 2005 21:25:07 +0000
Subject: cvs: pear /Console_Table Table.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-28636@lists.php.net to get a copy of this message
richard Mon Jan 17 16:25:07 2005 EDT Modified files: /pear/Console_Table Table.php Log: Add column based output filters http://cvs.php.net/diff.php/pear/Console_Table/Table.php?r1=1.5&r2=1.6&ty=u Index: pear/Console_Table/Table.php diff -u pear/Console_Table/Table.php:1.5 pear/Console_Table/Table.php:1.6 --- pear/Console_Table/Table.php:1.5 Sat Nov 27 13:57:50 2004 +++ pear/Console_Table/Table.php Mon Jan 17 16:25:04 2005 @@ -32,7 +32,7 @@ // | Author: Richard Heyes <richard@phpguru.org> | // +-----------------------------------------------------------------------+ // -// $Id: Table.php,v 1.5 2004/11/27 18:57:50 richard Exp $ +// $Id: Table.php,v 1.6 2005/01/17 21:25:04 richard Exp $ // // Utility for printing tables from cmdline scripts // @@ -84,6 +84,12 @@ * @var integer */ var $_padding; + + /** + * Column filters + * @var array + */ + var $_filters; /** * Constructor @@ -96,6 +102,7 @@ $this->_max_cols = 0; $this->_max_rows = 0; $this->_padding = 1; + $this->_filters = array(); } /** @@ -123,6 +130,20 @@ } /** + * Adds a filter to the object. Filters are standard php callbacks which are + * run on the data before table generation is performed. Filters are applied + * in the order they're added. the callback function must accept a single + * argument, which is a single table cell. + * + * @param int $col Column to apply filter to + * @param callback $callback PHP callback to apply + */ + function addFilter($col, &$callback) + { + $this->_filters[] = array($col, &$callback); + } + + /** * Sets the headers for the columns * * @param array $headers The column headers @@ -214,11 +235,29 @@ */ function getTable() { + $this->_applyFilters(); $this->_validateTable(); return $this->_buildTable(); } /** + * Applies any column filters to the data + */ + function _applyFilters() + { + if (!empty($this->_filters)) { + foreach ($this->_filters as $filter) { + $column = $filter[0]; + $callback = $filter[1]; + + foreach ($this->_data as $row_id => $row_data) { + $this->_data[$row_id][$column] = call_user_func($callback, $row_data[$column]); + } + } + } + } + + /** * Ensures column and row counts are correct */ function _validateTable()

« previous php.pear.cvs (#28636) next »