* @license PHP License * @package WB * @subpackage db */ WBClass::load('WBDatasource_Renderer', 'WBObserver', 'WBObserver_Observable'); /** * Datasource renderer Array * * @version 0.1.0 * @package WB * @subpackage db */ class WBDatasource_Renderer_Observable extends WBDatasource_Renderer implements WBObserver_Observable { /** * Current datasource path * @var string */ private $path; /** * Current datasource item * @var array */ private $item = array(); /** * Current position * @var int */ private $cnt = 0; /** * list of observer * @var array */ protected $observers = array(); /** * attach observer * * @param WBObserver $observer */ public function attach(WBObserver $observer) { $this->observers[$observer->getObjectId()] = $observer; } /** * detach observer * * @param WBObserver $observer */ public function detach(WBObserver $observer) { unset($this->observers[$observer->getObjectId()]); } /** * notify observer * */ public function notify() { foreach ($this->observers as $o) { $o->update($this); } } /** * start rendering * * @param string $datasource name separated with slashes */ public function start($datasource) { $this->path = $datasource; $this->item = array(); $this->cnt = 0; } /** * render single item * * The consumer for data source iteratrion * @param array $item */ public function renderItem($item) { ++$this->cnt; $this->item = $item; $this->notify(); } /** * finish rendering * * Things to do after iterating through datasource */ public function end() { $this->item = array(); } /** * Fetch current datasource path * @return string */ public function getPath() { return $this->path; } /** * Get current data item * @return array */ public function getItem() { return $this->item; } /** * Fetch current counter of items * @return int */ public function getCount() { return $this->cnt; } }