Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- abstract class Component extends stdCLass
- {
- protected $Decorators = array();
- protected $Water = NULL;
- protected $Cup = NULL;
- public function GetProperties(Component $Child)
- {
- foreach($this as $k => $v)
- {
- $Child->{$k} = &$this->{$k};
- }
- if(!isset($this->datahash))
- {
- $Child->{'parent'} = &$this;
- $Child->{'datahash'} = spl_object_hash($this);
- }
- }
- public function AddDecorator($func, $args = array())
- {
- if(is_callable($func))
- {
- array_push($this->Decorators, array($func, $args));
- }
- else
- {
- throw new InvalidArgumentException(sprintf('%s::AddDecorator(callable $func, array $args): Invalid function ( %s ).', get_class($this), var_export($func, true)));
- }
- }
- public function RemoveDecorator($func, $args = array())
- {
- foreach($this->Decorators as & $decorator)
- {
- if($decorator === array($func, $args))
- {
- unset($decorator);
- return true;
- }
- }
- return false;
- }
- public function __call($Key, $Params)
- {
- $Key = (string)$Key;
- if(!isset($this->{$Key}))
- {
- throw new BadMethodCallException(sprintf('Call to undefined method `%s::%s()`', get_class($this), $Key));
- return;
- }
- if(!is_callable($this->{$Key}))
- {
- throw new BadMethodCallException(sprintf('Call to undefined method `%s::{%s}()`.', get_class($this), var_export($this->{$Key}, true)));
- return;
- }
- call_user_func_array($this->{$Key}, $Params);
- }
- function PourWater()
- {
- printf('Adding Water: %d%s', $this->Water, "\n");
- }
- }
- class SimpleCofee extends Component
- {
- protected $Water = 0;
- protected $Cup = array();
- public function __construct($Parent = NULL)
- {
- $this->Water = 100;
- $this->Cup['cofeee'] = 25;
- if($Parent instanceof Component)
- {
- $Parent->GetProperties($this);
- }
- else
- {
- $hash = spl_object_hash($this);
- $this->{$hash.'Water'} = &$this->Water;
- $this->{$hash.'Cup'} = &$this->Cup;
- $this->{$hash.'Decorators'} = &$this->Decorators;
- }
- }
- public function Produce()
- {
- print "Making coffee....\n";
- foreach($this->Decorators as $call)
- {
- call_user_func_array($call[0], $call[1]);
- }
- //.....
- $this->PourWater();
- //......
- printf('Making cofee: %s%s', var_export($this->Cup, true), "\n");
- }
- }
- class SimpleTea extends SimpleCofee
- {
- public function __construct($Parent = NULL)
- {
- $this->Water = 75;
- $this->Cup['tea'] = 25;
- if($Parent instanceof Component)
- {
- $Parent->GetProperties($this);
- }
- else
- {
- $hash = spl_object_hash($this);
- $this->{$hash.'Water'} = &$this->Water;
- $this->{$hash.'Cup'} = &$this->Cup;
- $this->{$hash.'Decorators'} = &$this->Decorators;
- }
- }
- public function Produce()
- {
- print "Making tea....\n";
- foreach($this->Decorators as $call)
- {
- call_user_func_array($call[0], $call[1]);
- }
- //.....
- $this->PourWater();
- //......
- printf('Making tea: %s%s', var_export($this->Cup, true), "\n");
- }
- }
- abstract class Decorator extends Component
- {
- public function __construct(Component $Parent)
- {
- $Parent->GetProperties($this);
- }
- }
- class SugarCube extends Decorator
- {
- public function __construct(Component $Parent)
- {
- parent::__construct($Parent);
- $Parent->AddDecorator(array($this, 'AddSugar'));
- }
- public function AddSugar()
- {
- $hash = $this->{'datahash'};
- $this->{$hash.'Cup'}['Spoon'] = 1;
- $this->{$hash.'Ammount'} = @$this->{$hash.'Ammount'} + 1;
- $this->{$hash.'Water'} -= 5;
- printf('Adding sugar: %d%s', 1, "\n");
- }
- }
- class DoubleSugarCube extends SugarCube
- {
- public function AddSugar()
- {
- $hash = $this->{'datahash'};
- $this->{$hash.'Cup'}['Spoon'] = 1;
- $this->{$hash.'Ammount'} = @$this->{$hash.'ammount'} + 2;
- $this->{$hash.'Water'} -= 10;
- printf('Adding sugar: %d%s', 2, "\n");
- }
- }
- $Sugar = 1;
- $DoubleSugar = 1;
- $Cofee = new SimpleCofee();
- $Tea = new SimpleTea();
- $Cofee->Produce();
- $Tea->Produce();
- print "\n============\n\n";
- if($Sugar)
- {
- new SugarCube($Cofee);
- $Cofee->Produce();
- new SugarCube($Cofee);
- $Cofee->Produce();
- }
- if($DoubleSugar)
- {
- new DoubleSugarCube($Tea);
- $Tea->Produce();
- new DoubleSugarCube($Tea);
- $Tea->Produce();
- }
Add Comment
Please, Sign In to add comment