add generic decorator class

This commit is contained in:
Uwe Steinmann 2019-08-08 08:41:12 +02:00
parent 9a0b6c4d3d
commit cebf0e3796
2 changed files with 47 additions and 0 deletions

View File

@ -26,6 +26,11 @@ else
*/
require_once('Core/inc.ClassDMS.php');
/**
* @uses SeedDMS_Decorator
*/
require_once('Core/inc.ClassDecorator.php');
/**
* @uses SeedDMS_Object
*/

View File

@ -0,0 +1,42 @@
<?php
/**
* Implementation of the decorator pattern
*
* @category DMS
* @package SeedDMS_Core
* @license GPL 2
* @version @version@
* @author Uwe Steinmann <uwe@steinmann.cx>
* @copyright Copyright (C) 2010, Uwe Steinmann
* @version Release: @package_version@
*/
/**
* Class which implements a simple decorator pattern
*
* @category DMS
* @package SeedDMS_Core
* @version @version@
* @author Uwe Steinmann <uwe@steinmann.cx>
* @copyright Copyright (C) 2010, Uwe Steinmann
* @version Release: @package_version@
*/
class SeedDMS_Core_Decorator {
protected $o;
public function __construct($object) {
$this->o = $object;
}
public function __call($method, $args)
{
if (!method_exists($this->o, $method)) {
throw new Exception("Undefined method $method attempt.");
}
/* In case the called method returns the object itself, then return this object */
$result = call_user_func_array(array($this->o, $method), $args);
return $result === $this->o ? $this : $result;
}
}