Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?PHP
- /**
- * ILLI
- *
- * @category ILLI_System
- * @package ILLI
- * @subpackage System
- * @link http://illi.be
- * @license http://l.illi.be
- * @copyright ILLI Conference
- */
- NAMESPACE ILLI\System;
- /**
- * ILLI System Abstract Prototype Fluent Interface Handler
- *
- * returns the instance of object when the object::method() returns NULL
- *
- * dummy-class
- *
- * <code>
- * <?PHP
- * use \ILLI\System\Collection\TFluent AS TFluent;
- *
- * class test
- * {
- * private $__foo = 0;
- * public function add2()
- * {
- * $this->__foo += 2;
- * }
- *
- * public function add10()
- * {
- * $this->__foo += 10;
- * }
- * }
- * ?>
- * </code>
- *
- * as instance:
- * - store the wrapped object permanent
- *
- * <code>
- * <?PHP
- * $k = new TFluent(new test);
- *
- * $k->add2()
- * ->add2()
- * ->add2()
- * ->add10();
- *
- * var_dump($k); // __foo = 16
- * ?>
- * </code>
- *
- * static is implemented bt not useful:
- * - stores the wrapped object temporary
- * - this will create a new instance of ProtoFluent
- * on every ::fluent(obj)-call
- *
- * <code>
- * <?PHP
- * $l = new test;
- *
- * TFluent::fluent($l)
- * ->add2()
- * ->add2()
- * ->add2()
- * ->add2()
- * ->add2()
- * ->add10();
- *
- * var_dump($l); // __foo = 20
- * ?>
- * </code>
- *
- * nested has no effect:
- * <code>
- * <?PHP
- * $z = TFluent::fluent(new test)->add2()->add2()->add2()->add2()->add2();
- * var_dump($z);
- * $y = TFluent::fluent($z)->add10();
- * var_dump($y);
- * var_dump($z === $y);
- * ?>
- * </code>
- *
- * output:
- * <code>
- * object(ILLI\System\Collection\TFluent)#10 (1) {
- * ["__object":"ILLI\System\ProtoFluent":private]=>
- * object(localhost\illiFW\dev\test)#9 (1) {
- * ["__foo":"localhost\illiFW\dev\test":private]=>
- * int(10)
- * }
- * }
- * object(ILLI\System\Collection\TFluent)#10 (1) {
- * ["__object":"ILLI\System\ProtoFluent":private]=>
- * object(localhost\illiFW\dev\test)#9 (1) {
- * ["__foo":"localhost\illiFW\dev\test":private]=>
- * int(20)
- * }
- * }
- * bool(true)
- * </code>
- *
- * To resign this Proto, write your classes fluent:
- * <code>
- * <?PHP
- * class Fluent
- * {
- * private $__order = array();
- * public function hello()
- * {
- * $this->__order[] = 'hello';
- * return $this;
- * }
- *
- * public function world()
- * {
- * $this->__order[] = 'world';
- * return $this;
- * }
- *
- * public function __toString()
- * {
- * return implode(' ', $this->__order);
- * }
- * }
- *
- * $fluent = new Fluent;
- * print $fluent->hello()->world();
- * ?>
- * </code>
- *
- * @category ILLI_System
- * @package ILLI
- * @subpackage System
- * @namespace ILLI\System
- * @link http://illi.be
- * @license http://l.illi.be
- * @copyright ILLI Conference
- * @since 2.0.0-1
- * @version 2.0.0-1
- * @abstract
- */
- ABSTRACT CLASS ProtoFluent EXTENDS Proto
- {
- private $__object = NULL;
- public function __construct($object)
- {
- if(NULL === $object)
- throw new ArgumentNullException;
- if(!is_object($object))
- throw new ArgumentNullException(E::ARGUMENT_EXPECTED_OBJECT);
- if($object instanceOf ProtoFluent)
- return $this->__object = $object();
- $this->__object = $object;
- }
- private function __invoke()
- {
- return $this->__object;
- }
- public static function fluent($object)
- {
- if(NULL === $object)
- throw new ArgumentNullException;
- if(!is_object($object))
- throw new ArgumentNullException(E::ARGUMENT_EXPECTED_OBJECT);
- if($object instanceOf ProtoFluent)
- return $object;
- $class = get_called_class();
- return new $class($object);
- }
- /**
- * Generally faster than using call_user_func_array.
- */
- public function __call($method, $args)
- {
- $result = NULL;
- switch(sizeOf($args)):
- case 0:
- $result = $this->__object->$method();
- break;
- case 1:
- $result = $this->__object->$method
- (
- $arg[0]
- );
- break;
- case 2:
- $result = $this->__object->$method
- (
- $arg[0], $arg[1]
- );
- break;
- case 3:
- $result = $this->__object->$method
- (
- $arg[0], $arg[1], $arg[2]
- );
- break;
- case 4:
- $result = $this->__object->$method
- (
- $arg[0], $arg[1], $arg[2], $arg[3]
- );
- break;
- case 5:
- $result = $this->__object->$method
- (
- $arg[0], $arg[1], $arg[2], $arg[3], $arg[4]
- );
- break;
- case 6:
- $result = $this->__object->$method
- (
- $arg[0], $arg[1], $arg[2], $arg[3], $arg[4], $arg[5]
- );
- break;
- case 7:
- $result = $this->__object->$method
- (
- $arg[0], $arg[1], $arg[2], $arg[3], $arg[4], $arg[5],
- $arg[6]
- );
- break;
- case 8:
- $result = $this->__object->$method
- (
- $arg[0], $arg[1], $arg[2], $arg[3], $arg[4], $arg[5],
- $arg[6], $arg[7]
- );
- break;
- case 9:
- $result = $this->__object->$method
- (
- $arg[0], $arg[1], $arg[2], $arg[3], $arg[4], $arg[5],
- $arg[6], $arg[7], $arg[8]
- );
- break;
- case 10:
- $result = $this->__object->$method
- (
- $arg[0], $arg[1], $arg[2], $arg[3], $arg[4], $arg[5],
- $arg[6], $arg[7], $arg[8], $arg[9]
- );
- break;
- case 11:
- $result = $this->__object->$method
- (
- $arg[0], $arg[1], $arg[2], $arg[3], $arg[4], $arg[5],
- $arg[6], $arg[7], $arg[8], $arg[9], $arg[10]
- );
- break;
- case 12:
- $result = $this->__object->$method
- (
- $arg[0], $arg[1], $arg[2], $arg[3], $arg[4], $arg[5],
- $arg[6], $arg[7], $arg[8], $arg[9], $arg[10], $arg[11]
- );
- break;
- case 13:
- $result = $this->__object->$method
- (
- $arg[0], $arg[1], $arg[2], $arg[3], $arg[4], $arg[5],
- $arg[6], $arg[7], $arg[8], $arg[9], $arg[10], $arg[11],
- $arg[12]
- );
- break;
- case 14:
- $result = $this->__object->$method
- (
- $arg[0], $arg[1], $arg[2], $arg[3], $arg[4], $arg[5],
- $arg[6], $arg[7], $arg[8], $arg[9], $arg[10], $arg[11],
- $arg[12], $arg[13]
- );
- break;
- case 15:
- $result = $this->__object->$method
- (
- $arg[0], $arg[1], $arg[2], $arg[3], $arg[4], $arg[5],
- $arg[6], $arg[7], $arg[8], $arg[9], $arg[10], $arg[11],
- $arg[12], $arg[13], $arg[14]
- );
- break;
- case 16:
- $result = $this->__object->$method
- (
- $arg[0], $arg[1], $arg[2], $arg[3], $arg[4], $arg[5],
- $arg[6], $arg[7], $arg[8], $arg[9], $arg[10], $arg[11],
- $arg[12], $arg[13], $arg[14], $arg[15]
- );
- break;
- case 17:
- $result = $this->__object->$method
- (
- $arg[0], $arg[1], $arg[2], $arg[3], $arg[4], $arg[5],
- $arg[6], $arg[7], $arg[8], $arg[9], $arg[10], $arg[11],
- $arg[12], $arg[13], $arg[14], $arg[15], $arg[16]
- );
- break;
- case 18:
- $result = $this->__object->$method
- (
- $arg[0], $arg[1], $arg[2], $arg[3], $arg[4], $arg[5],
- $arg[6], $arg[7], $arg[8], $arg[9], $arg[10], $arg[11],
- $arg[12], $arg[13], $arg[14], $arg[15], $arg[16], $arg[17]
- );
- break;
- case 19:
- $result = $this->__object->$method
- (
- $arg[0], $arg[1], $arg[2], $arg[3], $arg[4], $arg[5],
- $arg[6], $arg[7], $arg[8], $arg[9], $arg[10], $arg[11],
- $arg[12], $arg[13], $arg[14], $arg[15], $arg[16], $arg[17],
- $arg[18]
- );
- break;
- case 20:
- $result = $this->__object->$method
- (
- $arg[0], $arg[1], $arg[2], $arg[3], $arg[4], $arg[5],
- $arg[6], $arg[7], $arg[8], $arg[9], $arg[10], $arg[11],
- $arg[12], $arg[13], $arg[14], $arg[15], $arg[16], $arg[17],
- $arg[18], $arg[19]
- );
- break;
- case 21:
- $result = $this->__object->$method
- (
- $arg[0], $arg[1], $arg[2], $arg[3], $arg[4], $arg[5],
- $arg[6], $arg[7], $arg[8], $arg[9], $arg[10], $arg[11],
- $arg[12], $arg[13], $arg[14], $arg[15], $arg[16], $arg[17],
- $arg[18], $arg[19], $arg[20]
- );
- break;
- case 22:
- $result = $this->__object->$method
- (
- $arg[0], $arg[1], $arg[2], $arg[3], $arg[4], $arg[5],
- $arg[6], $arg[7], $arg[8], $arg[9], $arg[10], $arg[11],
- $arg[12], $arg[13], $arg[14], $arg[15], $arg[16], $arg[17],
- $arg[18], $arg[19], $arg[20], $arg[21]
- );
- break;
- case 23:
- $result = $this->__object->$method
- (
- $arg[0], $arg[1], $arg[2], $arg[3], $arg[4], $arg[5],
- $arg[6], $arg[7], $arg[8], $arg[9], $arg[10], $arg[11],
- $arg[12], $arg[13], $arg[14], $arg[15], $arg[16], $arg[17],
- $arg[18], $arg[19], $arg[20], $arg[21], $arg[22]
- );
- break;
- case 24:
- $result = $this->__object->$method
- (
- $arg[0], $arg[1], $arg[2], $arg[3], $arg[4], $arg[5],
- $arg[6], $arg[7], $arg[8], $arg[9], $arg[10], $arg[11],
- $arg[12], $arg[13], $arg[14], $arg[15], $arg[16], $arg[17],
- $arg[18], $arg[19], $arg[20], $arg[21], $arg[22], $arg[23]
- );
- break;
- case 25:
- $result = $this->__object->$method
- (
- $arg[0], $arg[1], $arg[2], $arg[3], $arg[4], $arg[5],
- $arg[6], $arg[7], $arg[8], $arg[9], $arg[10], $arg[11],
- $arg[12], $arg[13], $arg[14], $arg[15], $arg[16], $arg[17],
- $arg[18], $arg[19], $arg[20], $arg[21], $arg[22], $arg[23],
- $arg[24]
- );
- break;
- case 26:
- $result = $this->__object->$method
- (
- $arg[0], $arg[1], $arg[2], $arg[3], $arg[4], $arg[5],
- $arg[6], $arg[7], $arg[8], $arg[9], $arg[10], $arg[11],
- $arg[12], $arg[13], $arg[14], $arg[15], $arg[16], $arg[17],
- $arg[18], $arg[19], $arg[20], $arg[21], $arg[22], $arg[23],
- $arg[24], $arg[25]
- );
- break;
- case 27:
- $result = $this->__object->$method
- (
- $arg[0], $arg[1], $arg[2], $arg[3], $arg[4], $arg[5],
- $arg[6], $arg[7], $arg[8], $arg[9], $arg[10], $arg[11],
- $arg[12], $arg[13], $arg[14], $arg[15], $arg[16], $arg[17],
- $arg[18], $arg[19], $arg[20], $arg[21], $arg[22], $arg[23],
- $arg[24], $arg[25], $arg[26]
- );
- break;
- case 28:
- $result = $this->__object->$method
- (
- $arg[0], $arg[1], $arg[2], $arg[3], $arg[4], $arg[5],
- $arg[6], $arg[7], $arg[8], $arg[9], $arg[10], $arg[11],
- $arg[12], $arg[13], $arg[14], $arg[15], $arg[16], $arg[17],
- $arg[18], $arg[19], $arg[20], $arg[21], $arg[22], $arg[23],
- $arg[24], $arg[25], $arg[26], $arg[27]
- );
- break;
- case 29:
- $result = $this->__object->$method
- (
- $arg[0], $arg[1], $arg[2], $arg[3], $arg[4], $arg[5],
- $arg[6], $arg[7], $arg[8], $arg[9], $arg[10], $arg[11],
- $arg[12], $arg[13], $arg[14], $arg[15], $arg[16], $arg[17],
- $arg[18], $arg[19], $arg[20], $arg[21], $arg[22], $arg[23],
- $arg[24], $arg[25], $arg[26], $arg[27], $arg[28]
- );
- break;
- case 30:
- $result = $this->__object->$method
- (
- $arg[0], $arg[1], $arg[2], $arg[3], $arg[4], $arg[5],
- $arg[6], $arg[7], $arg[8], $arg[9], $arg[10], $arg[11],
- $arg[12], $arg[13], $arg[14], $arg[15], $arg[16], $arg[17],
- $arg[18], $arg[19], $arg[20], $arg[21], $arg[22], $arg[23],
- $arg[24], $arg[25], $arg[26], $arg[27], $arg[28], $arg[29]
- );
- break;
- case 31:
- $result = $this->__object->$method
- (
- $arg[0], $arg[1], $arg[2], $arg[3], $arg[4], $arg[5],
- $arg[6], $arg[7], $arg[8], $arg[9], $arg[10], $arg[11],
- $arg[12], $arg[13], $arg[14], $arg[15], $arg[16], $arg[17],
- $arg[18], $arg[19], $arg[20], $arg[21], $arg[22], $arg[23],
- $arg[24], $arg[25], $arg[26], $arg[27], $arg[28], $arg[29],
- $arg[30]
- );
- break;
- case 32:
- $result = $this->__object->$method
- (
- $arg[0], $arg[1], $arg[2], $arg[3], $arg[4], $arg[5],
- $arg[6], $arg[7], $arg[8], $arg[9], $arg[10], $arg[11],
- $arg[12], $arg[13], $arg[14], $arg[15], $arg[16], $arg[17],
- $arg[18], $arg[19], $arg[20], $arg[21], $arg[22], $arg[23],
- $arg[24], $arg[25], $arg[26], $arg[27], $arg[28], $arg[29],
- $arg[30], $arg[32]
- );
- break;
- default:
- $result = call_user_func_array([$this->__object, $method], $args);
- endswitch;
- return (NULL === $result)
- ? $this
- : $result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement