* @license PHP License * @package WB * @subpackage db */ WBClass::load('WBDatasource_Decorator'); /** * Simple datasource decorator: Modal * * @version 0.1.0 * @package WB * @subpackage db */ class WBDatasource_Decorator_Modal extends WBDatasource_Decorator { /** * decorator's parameter list * * Parameters: * - columns: list of columns to decorate * - span: number of items to calculate average of * - format: number format as in sprintf() * * @var array */ protected $parameter = array( 'span' => 14, 'column' => array(), 'format' => '%d' ); /** * store values for each column to calculate average * @var array */ private $buffer = array(); /** * setup column buffers */ public function onStart(&$item) { $this->parameter['span'] = intval($this->parameter['span']); foreach ($this->parameter['column'] as $c) {# $this->buffer[$c] = array(); $item[$c . '_mod'] = null; } } /** * actually decorate list item * * @param array $item * @return void */ public function decorate(&$item) { foreach ($this->buffer as $c => &$b) { if (count($b) >= $this->parameter['span']) { array_shift($b); } $b[] = $item[$c]; if (count($b) != $this->parameter['span']) { $item[$c . '_mod'] = null; continue; } $count = array(); foreach($b as $v) { if (isset($count[$v])) { ++$count[$v]; } else { $count[$v] = 1; } } $max = 1; $modal = array(); foreach($count as $v => $c) { if ($c > $max) { $max = $c; $modal = array($v); } else if ($c == $max) { $modal[] = $v; } } $item[$c . '_mod'] = sprintf($this->parameter['format'], min($modal)); } } } ?>