Guest User

mutable object

a guest
Jul 22nd, 2010
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.50 KB | None | 0 0
  1. class MutableDomainObject extends ImmutableDomainObject implements IObservable
  2. {
  3.     protected $modified = false;
  4.     protected $observable;
  5.    
  6.     protected function __construct()
  7.     {
  8.         $this->observable = new Observable();
  9.         $this->observable->registerEvents(array('invalidate','modified'));
  10.     }
  11.    
  12.     function __call($method, array $args)
  13.     {
  14.         if ( substr($method,0,3) == 'set' )
  15.         {
  16.             $propname = substr($method, 3);
  17.             if ( sizeof($args) <= 0 )
  18.             {
  19.                 throw new DomainObjectException
  20.                     ("Mutator $method requires an argument");
  21.             }
  22.             $validator = "validate$propname";
  23.             if ( method_exists($this, $validator) )
  24.             {
  25.                 if ( !$this->$validator($args[0]) )
  26.                 {
  27.                     $this->observable->fireEvent('invalidate',
  28.                         $propname, $args[0], $this);
  29.                     return false;
  30.                 }
  31.             }
  32.             $this->$propname = $args[0];
  33.             $this->observable->fireEvent('modified',$propname,$args[0],$this);
  34.             return $this->modified = true;
  35.         }
  36.         return parent::__call($method, $args);
  37.     }
  38.    
  39.     function on($eventId,$callback,$context=null,$addArg=null)
  40.     {
  41.         $this->observable->on($eventId,$callback,$context,$addArg);
  42.     }
  43.    
  44.     function un($eventId,$callback,$context=null,$addArg=null)
  45.     {
  46.         $this->observable->un($eventId,$callback,$context,$addArg);
  47.     }
  48.    
  49.     function isModified()
  50.     {
  51.         return $this->modified;
  52.     }
  53.    
  54. }
Advertisement
Add Comment
Please, Sign In to add comment