<?php
require_once ROGUE_DIR . '/types/Observer.php';
/**
* keeps track of object changes, similar to property change listener, however
* this is not ment to be extended and does not create member variables on changeValue
*
* @author Cory Marsh
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
*/
class ChangeTracker
{
protected $_oldValues;
protected $_newValues;
protected $_listeners;
protected $_observed;
/**
* Calls parent constructor to create array for listeners. Creates
* arrays to hold old and new values.
*/
public function __construct($observed)
{
$this->_oldValues = array();
$this->_newValues = array();
$this->_listeners = array();
$this->_observed = $observed;
}
/**
* @return array $oldValues Old values of properties listened to.
*/
public function getOldValues()
{
return $this->_oldValues;
}
/**
* @return array $newValues New values of properties listened to.
*/
public function getNewValues()
{
return $this->_newValues;
}
/**
* Notifies each listener that the value of a property has changed.
* @param string $name The name of the property that changed.
* @param string $oldValue The old value of the property.
* @param string $newValue The new value of the property.
* @return mixed $newValue the new property value is returned
*/
public function changeValue($name, $oldValue, $newValue)
{
//Logger::getLogger('boom')->warn("2 changeValue() called");
if ($oldValue !== $newValue)
{
// we want to save just the original value, not intermediates
if (!isset($this->_oldValues[$name]))
$this->_oldValues[$name] = $oldValue;
$this->_newValues[$name] = $newValue;
foreach ($this->_listeners as $listener)
{
$listener->notify($this->_observed, array($name, $oldValue, $newValue));
}
}
return $newValue;
}
/**
* clear all state changes in the listener, but NOT the observers
* @return PropertyChangeListener the class reference
*/
public function clearChanges()
{
$this->_oldValues = array();
$this->_newValues = array();
return $this;
}
/**
* @param observer $obs An object that will be listening to this object.
*/
public function addListener(iObserver $observer)
{
$this->_listeners[] = $observer;
}
}