Advertisement
fruffl

Fluent Interface

Mar 2nd, 2012
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.38 KB | None | 0 0
  1. <?PHP
  2.     /**
  3.      * ILLI
  4.      *
  5.      * @category   ILLI_System
  6.      * @package    ILLI
  7.      * @subpackage System
  8.      * @link       http://illi.be
  9.      * @license    http://l.illi.be
  10.      * @copyright  ILLI Conference
  11.      */
  12.     NAMESPACE ILLI\System;
  13.  
  14.     /**
  15.      * ILLI System Abstract Prototype Fluent Interface Handler
  16.      *
  17.      * returns the instance of object when the object::method() returns NULL
  18.      *
  19.      * dummy-class
  20.      *
  21.      * <code>
  22.      * <?PHP
  23.      *  use \ILLI\System\Collection\TFluent AS TFluent;
  24.      *
  25.      *  class test
  26.      *  {
  27.      *      private $__foo = 0;
  28.      *      public function add2()
  29.      *      {
  30.      *          $this->__foo += 2;
  31.      *      }
  32.      *     
  33.      *      public function add10()
  34.      *      {
  35.      *          $this->__foo += 10;
  36.      *      }
  37.      *  }
  38.      * ?>
  39.      * </code>
  40.      *
  41.      * as instance:
  42.      * - store the wrapped object permanent
  43.      *
  44.      * <code>
  45.      * <?PHP
  46.      *  $k = new TFluent(new test);
  47.      *
  48.      *  $k->add2()
  49.      *    ->add2()
  50.      *    ->add2()
  51.      *    ->add10();
  52.      *
  53.      *  var_dump($k); // __foo = 16
  54.      * ?>
  55.      * </code>
  56.      *
  57.      * static is implemented bt not useful:
  58.      * - stores the wrapped object temporary
  59.      * - this will create a new instance of ProtoFluent
  60.      *   on every ::fluent(obj)-call
  61.      *
  62.      * <code>
  63.      * <?PHP
  64.      *  $l = new test;
  65.      *
  66.      *  TFluent::fluent($l)
  67.      *  ->add2()
  68.      *  ->add2()
  69.      *  ->add2()
  70.      *  ->add2()
  71.      *  ->add2()
  72.      *  ->add10();
  73.      *  
  74.      *  var_dump($l); // __foo = 20
  75.      * ?>
  76.      * </code>
  77.      *
  78.      * nested has no effect:
  79.      * <code>
  80.      * <?PHP
  81.      *  $z = TFluent::fluent(new test)->add2()->add2()->add2()->add2()->add2();
  82.      *  var_dump($z);
  83.      *  $y = TFluent::fluent($z)->add10();
  84.      *  var_dump($y);
  85.      *  var_dump($z === $y);
  86.      * ?>
  87.      * </code>
  88.      *
  89.      * output:
  90.      * <code>
  91.      *  object(ILLI\System\Collection\TFluent)#10 (1) {
  92.      *    ["__object":"ILLI\System\ProtoFluent":private]=>
  93.      *    object(localhost\illiFW\dev\test)#9 (1) {
  94.      *      ["__foo":"localhost\illiFW\dev\test":private]=>
  95.      *      int(10)
  96.      *    }
  97.      *  }
  98.      *  object(ILLI\System\Collection\TFluent)#10 (1) {
  99.      *    ["__object":"ILLI\System\ProtoFluent":private]=>
  100.      *    object(localhost\illiFW\dev\test)#9 (1) {
  101.      *      ["__foo":"localhost\illiFW\dev\test":private]=>
  102.      *      int(20)
  103.      *    }
  104.      *  }
  105.      *  bool(true)
  106.      * </code>
  107.      *
  108.      * To resign this Proto, write your classes fluent:
  109.      * <code>
  110.      * <?PHP
  111.      *  class Fluent
  112.      *  {
  113.      *      private $__order = array();
  114.      *      public function hello()
  115.      *      {
  116.      *          $this->__order[] = 'hello';
  117.      *          return $this;
  118.      *      }
  119.      *     
  120.      *      public function world()
  121.      *      {
  122.      *          $this->__order[] = 'world';
  123.      *          return $this;
  124.      *      }
  125.      *     
  126.      *      public function __toString()
  127.      *      {
  128.      *          return implode(' ', $this->__order);
  129.      *      }
  130.      *  }
  131.      *
  132.      *  $fluent = new Fluent;
  133.      *  print $fluent->hello()->world();
  134.      * ?>
  135.      * </code>
  136.      *
  137.      * @category   ILLI_System
  138.      * @package    ILLI
  139.      * @subpackage System
  140.      * @namespace  ILLI\System
  141.      * @link       http://illi.be
  142.      * @license    http://l.illi.be
  143.      * @copyright  ILLI Conference
  144.      * @since      2.0.0-1
  145.      * @version    2.0.0-1
  146.      * @abstract
  147.      */
  148.     ABSTRACT CLASS ProtoFluent EXTENDS Proto
  149.     {
  150.         private $__object = NULL;
  151.        
  152.         public function __construct($object)
  153.         {
  154.             if($object instanceOf ProtoFluent)
  155.                 return $this->__object = $object();
  156.            
  157.             $this->__object = $object;
  158.         }
  159.        
  160.         private function __invoke()
  161.         {
  162.             return $this->__object;
  163.         }
  164.        
  165.         public static function fluent($object)
  166.         {
  167.             if($object instanceOf ProtoFluent)
  168.                 return $object;
  169.                
  170.             $class = get_called_class();
  171.             return new $class($object);
  172.         }
  173.        
  174.         public function __call($method, $args)
  175.         {
  176.             return (NULL === ($result = call_user_func_array([$this->__object, $method], $args)))
  177.                 ? $this : $result;
  178.         }
  179.     }
  180.  
  181. <?PHP
  182.     /**
  183.      * ILLI
  184.      *
  185.      * @category   ILLI_System_Collection
  186.      * @package    ILLI
  187.      * @subpackage SystemCollection
  188.      * @link       http://illi.be
  189.      * @license    http://l.illi.be
  190.      * @copyright  ILLI Conference
  191.      */
  192.     NAMESPACE ILLI\System\Collection;
  193.     USE ILLI\System as System;
  194.  
  195.     /**
  196.      * ILLI System Collection Datatype Fluent Interface Handler
  197.      *
  198.      * @category   ILLI_System_Collection
  199.      * @package    ILLI
  200.      * @subpackage SystemCollection
  201.      * @namespace  ILLI\System\Collection
  202.      * @link       http://illi.be
  203.      * @license    http://l.illi.be
  204.      * @copyright  ILLI Conference
  205.      * @since      2.0.0-1
  206.      * @version    2.0.0-1
  207.      * @abstract
  208.      */
  209.     CLASS TFluent EXTENDS System\ProtoFluent
  210.     {
  211.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement