public
Created

  • Download Gist
gistfile1.aw
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
<?php
 
interface DecoratorInterface {
public function decorate();
}
 
trait DecoratorTrait {
protected $decorators = array();
 
//public function addDecorator(DecoratorInterface $decorator)...
 
public function decorate($input)
{
$output = $input;
 
foreach ($this->decorators as $decorator) {
$output = $decorator->decorate($output);
}
 
return sprintf('Decorated String: %s', $output);
}
 
public function __toString()
{
return $this->decorate(parent::__toString());
}
}
 
class SomeObject {
protected $someText;
 
public function __construct($someText = '')
{
$this->someText = $someText;
}
 
public function __toString()
{
return $this->someText;
}
}
 
class DecoratedObject extends SomeObject {
use DecoratorTrait;
 
// maybe do other stuff
}
 
$object1 = new SomeObject('foo');
echo $object1 . PHP_EOL; // foo
 
$object = new DecoratedObject('foo');
echo $object . PHP_EOL; // Decorated String: foo

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.