Advertisement
fruffl

Class FluentInterface

Mar 3rd, 2012
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 12.49 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(NULL === $object)
  155.                 throw new ArgumentNullException;
  156.                
  157.             if(!is_object($object))
  158.                 throw new ArgumentNullException(E::ARGUMENT_EXPECTED_OBJECT);
  159.            
  160.             if($object instanceOf ProtoFluent)
  161.                 return $this->__object = $object();
  162.            
  163.             $this->__object = $object;
  164.         }
  165.        
  166.         private function __invoke()
  167.         {
  168.             return $this->__object;
  169.         }
  170.        
  171.         public static function fluent($object)
  172.         {
  173.             if(NULL === $object)
  174.                 throw new ArgumentNullException;
  175.                
  176.             if(!is_object($object))
  177.                 throw new ArgumentNullException(E::ARGUMENT_EXPECTED_OBJECT);
  178.            
  179.             if($object instanceOf ProtoFluent)
  180.                 return $object;
  181.                
  182.             $class = get_called_class();
  183.             return new $class($object);
  184.         }
  185.        
  186.         /**
  187.          * Generally faster than using call_user_func_array.
  188.          */
  189.         public function __call($method, $args)
  190.         {
  191.             $result = NULL;
  192.            
  193.             switch(sizeOf($args)):
  194.                 case 0:
  195.                     $result = $this->__object->$method();
  196.                     break;
  197.                 case 1:
  198.                     $result = $this->__object->$method
  199.                     (
  200.                         $arg[0]
  201.                     );
  202.                     break;
  203.                 case 2:
  204.                     $result = $this->__object->$method
  205.                     (
  206.                         $arg[0], $arg[1]
  207.                     );
  208.                     break;
  209.                 case 3:
  210.                     $result = $this->__object->$method
  211.                     (
  212.                         $arg[0], $arg[1], $arg[2]
  213.                     );
  214.                     break;
  215.                 case 4:
  216.                     $result = $this->__object->$method
  217.                     (
  218.                         $arg[0], $arg[1], $arg[2], $arg[3]
  219.                     );
  220.                     break;
  221.                 case 5:
  222.                     $result = $this->__object->$method
  223.                     (
  224.                         $arg[0], $arg[1], $arg[2], $arg[3], $arg[4]
  225.                     );
  226.                     break;
  227.                 case 6:
  228.                     $result = $this->__object->$method
  229.                     (
  230.                         $arg[0], $arg[1], $arg[2], $arg[3], $arg[4], $arg[5]
  231.                     );
  232.                     break;
  233.                 case 7:
  234.                     $result = $this->__object->$method
  235.                     (
  236.                         $arg[0], $arg[1], $arg[2], $arg[3], $arg[4], $arg[5],
  237.                         $arg[6]
  238.                     );
  239.                     break;
  240.                 case 8:
  241.                     $result = $this->__object->$method
  242.                     (
  243.                         $arg[0], $arg[1], $arg[2], $arg[3], $arg[4], $arg[5],
  244.                         $arg[6], $arg[7]
  245.                     );
  246.                     break;
  247.                 case 9:
  248.                     $result = $this->__object->$method
  249.                     (
  250.                         $arg[0], $arg[1], $arg[2], $arg[3], $arg[4], $arg[5],
  251.                         $arg[6], $arg[7], $arg[8]
  252.                     );
  253.                     break;
  254.                 case 10:
  255.                     $result = $this->__object->$method
  256.                     (
  257.                         $arg[0], $arg[1], $arg[2], $arg[3], $arg[4], $arg[5],
  258.                         $arg[6], $arg[7], $arg[8], $arg[9]
  259.                     );
  260.                     break;
  261.                 case 11:
  262.                     $result = $this->__object->$method
  263.                     (
  264.                         $arg[0], $arg[1], $arg[2], $arg[3], $arg[4], $arg[5],
  265.                         $arg[6], $arg[7], $arg[8], $arg[9], $arg[10]
  266.                     );
  267.                     break;
  268.                 case 12:
  269.                     $result = $this->__object->$method
  270.                     (
  271.                         $arg[0], $arg[1], $arg[2], $arg[3], $arg[4], $arg[5],
  272.                         $arg[6], $arg[7], $arg[8], $arg[9], $arg[10], $arg[11]
  273.                     );
  274.                     break;
  275.                 case 13:
  276.                     $result = $this->__object->$method
  277.                     (
  278.                         $arg[0], $arg[1], $arg[2], $arg[3], $arg[4], $arg[5],
  279.                         $arg[6], $arg[7], $arg[8], $arg[9], $arg[10], $arg[11],
  280.                         $arg[12]
  281.                     );
  282.                     break;
  283.                 case 14:
  284.                     $result = $this->__object->$method
  285.                     (
  286.                         $arg[0], $arg[1], $arg[2], $arg[3], $arg[4], $arg[5],
  287.                         $arg[6], $arg[7], $arg[8], $arg[9], $arg[10], $arg[11],
  288.                         $arg[12], $arg[13]
  289.                     );
  290.                     break;
  291.                 case 15:
  292.                     $result = $this->__object->$method
  293.                     (
  294.                         $arg[0], $arg[1], $arg[2], $arg[3], $arg[4], $arg[5],
  295.                         $arg[6], $arg[7], $arg[8], $arg[9], $arg[10], $arg[11],
  296.                         $arg[12], $arg[13], $arg[14]
  297.                     );
  298.                     break;
  299.                 case 16:
  300.                     $result = $this->__object->$method
  301.                     (
  302.                         $arg[0], $arg[1], $arg[2], $arg[3], $arg[4], $arg[5],
  303.                         $arg[6], $arg[7], $arg[8], $arg[9], $arg[10], $arg[11],
  304.                         $arg[12], $arg[13], $arg[14], $arg[15]
  305.                     );
  306.                     break;
  307.                 case 17:
  308.                     $result = $this->__object->$method
  309.                     (
  310.                         $arg[0], $arg[1], $arg[2], $arg[3], $arg[4], $arg[5],
  311.                         $arg[6], $arg[7], $arg[8], $arg[9], $arg[10], $arg[11],
  312.                         $arg[12], $arg[13], $arg[14], $arg[15], $arg[16]
  313.                     );
  314.                     break;
  315.                 case 18:
  316.                     $result = $this->__object->$method
  317.                     (
  318.                         $arg[0], $arg[1], $arg[2], $arg[3], $arg[4], $arg[5],
  319.                         $arg[6], $arg[7], $arg[8], $arg[9], $arg[10], $arg[11],
  320.                         $arg[12], $arg[13], $arg[14], $arg[15], $arg[16], $arg[17]
  321.                     );
  322.                     break;
  323.                 case 19:
  324.                     $result = $this->__object->$method
  325.                     (
  326.                         $arg[0], $arg[1], $arg[2], $arg[3], $arg[4], $arg[5],
  327.                         $arg[6], $arg[7], $arg[8], $arg[9], $arg[10], $arg[11],
  328.                         $arg[12], $arg[13], $arg[14], $arg[15], $arg[16], $arg[17],
  329.                         $arg[18]
  330.                     );
  331.                     break;
  332.                 case 20:
  333.                     $result = $this->__object->$method
  334.                     (
  335.                         $arg[0], $arg[1], $arg[2], $arg[3], $arg[4], $arg[5],
  336.                         $arg[6], $arg[7], $arg[8], $arg[9], $arg[10], $arg[11],
  337.                         $arg[12], $arg[13], $arg[14], $arg[15], $arg[16], $arg[17],
  338.                         $arg[18], $arg[19]
  339.                     );
  340.                     break;
  341.                 case 21:
  342.                     $result = $this->__object->$method
  343.                     (
  344.                         $arg[0], $arg[1], $arg[2], $arg[3], $arg[4], $arg[5],
  345.                         $arg[6], $arg[7], $arg[8], $arg[9], $arg[10], $arg[11],
  346.                         $arg[12], $arg[13], $arg[14], $arg[15], $arg[16], $arg[17],
  347.                         $arg[18], $arg[19], $arg[20]
  348.                     );
  349.                     break;
  350.                 case 22:
  351.                     $result = $this->__object->$method
  352.                     (
  353.                         $arg[0], $arg[1], $arg[2], $arg[3], $arg[4], $arg[5],
  354.                         $arg[6], $arg[7], $arg[8], $arg[9], $arg[10], $arg[11],
  355.                         $arg[12], $arg[13], $arg[14], $arg[15], $arg[16], $arg[17],
  356.                         $arg[18], $arg[19], $arg[20], $arg[21]
  357.                     );
  358.                     break;
  359.                 case 23:
  360.                     $result = $this->__object->$method
  361.                     (
  362.                         $arg[0], $arg[1], $arg[2], $arg[3], $arg[4], $arg[5],
  363.                         $arg[6], $arg[7], $arg[8], $arg[9], $arg[10], $arg[11],
  364.                         $arg[12], $arg[13], $arg[14], $arg[15], $arg[16], $arg[17],
  365.                         $arg[18], $arg[19], $arg[20], $arg[21], $arg[22]
  366.                     );
  367.                     break;
  368.                 case 24:
  369.                     $result = $this->__object->$method
  370.                     (
  371.                         $arg[0], $arg[1], $arg[2], $arg[3], $arg[4], $arg[5],
  372.                         $arg[6], $arg[7], $arg[8], $arg[9], $arg[10], $arg[11],
  373.                         $arg[12], $arg[13], $arg[14], $arg[15], $arg[16], $arg[17],
  374.                         $arg[18], $arg[19], $arg[20], $arg[21], $arg[22], $arg[23]
  375.                     );
  376.                     break;
  377.                 case 25:
  378.                     $result = $this->__object->$method
  379.                     (
  380.                         $arg[0], $arg[1], $arg[2], $arg[3], $arg[4], $arg[5],
  381.                         $arg[6], $arg[7], $arg[8], $arg[9], $arg[10], $arg[11],
  382.                         $arg[12], $arg[13], $arg[14], $arg[15], $arg[16], $arg[17],
  383.                         $arg[18], $arg[19], $arg[20], $arg[21], $arg[22], $arg[23],
  384.                         $arg[24]
  385.                     );
  386.                     break;
  387.                 case 26:
  388.                     $result = $this->__object->$method
  389.                     (
  390.                         $arg[0], $arg[1], $arg[2], $arg[3], $arg[4], $arg[5],
  391.                         $arg[6], $arg[7], $arg[8], $arg[9], $arg[10], $arg[11],
  392.                         $arg[12], $arg[13], $arg[14], $arg[15], $arg[16], $arg[17],
  393.                         $arg[18], $arg[19], $arg[20], $arg[21], $arg[22], $arg[23],
  394.                         $arg[24], $arg[25]
  395.                        
  396.                     );
  397.                     break;
  398.                 case 27:
  399.                     $result = $this->__object->$method
  400.                     (
  401.                         $arg[0], $arg[1], $arg[2], $arg[3], $arg[4], $arg[5],
  402.                         $arg[6], $arg[7], $arg[8], $arg[9], $arg[10], $arg[11],
  403.                         $arg[12], $arg[13], $arg[14], $arg[15], $arg[16], $arg[17],
  404.                         $arg[18], $arg[19], $arg[20], $arg[21], $arg[22], $arg[23],
  405.                         $arg[24], $arg[25], $arg[26]
  406.                        
  407.                     );
  408.                     break;
  409.                 case 28:
  410.                     $result = $this->__object->$method
  411.                     (
  412.                         $arg[0], $arg[1], $arg[2], $arg[3], $arg[4], $arg[5],
  413.                         $arg[6], $arg[7], $arg[8], $arg[9], $arg[10], $arg[11],
  414.                         $arg[12], $arg[13], $arg[14], $arg[15], $arg[16], $arg[17],
  415.                         $arg[18], $arg[19], $arg[20], $arg[21], $arg[22], $arg[23],
  416.                         $arg[24], $arg[25], $arg[26], $arg[27]
  417.                        
  418.                     );
  419.                     break;
  420.                 case 29:
  421.                     $result = $this->__object->$method
  422.                     (
  423.                         $arg[0], $arg[1], $arg[2], $arg[3], $arg[4], $arg[5],
  424.                         $arg[6], $arg[7], $arg[8], $arg[9], $arg[10], $arg[11],
  425.                         $arg[12], $arg[13], $arg[14], $arg[15], $arg[16], $arg[17],
  426.                         $arg[18], $arg[19], $arg[20], $arg[21], $arg[22], $arg[23],
  427.                         $arg[24], $arg[25], $arg[26], $arg[27], $arg[28]
  428.                        
  429.                     );
  430.                     break;
  431.                 case 30:
  432.                     $result = $this->__object->$method
  433.                     (
  434.                         $arg[0], $arg[1], $arg[2], $arg[3], $arg[4], $arg[5],
  435.                         $arg[6], $arg[7], $arg[8], $arg[9], $arg[10], $arg[11],
  436.                         $arg[12], $arg[13], $arg[14], $arg[15], $arg[16], $arg[17],
  437.                         $arg[18], $arg[19], $arg[20], $arg[21], $arg[22], $arg[23],
  438.                         $arg[24], $arg[25], $arg[26], $arg[27], $arg[28], $arg[29]
  439.                        
  440.                     );
  441.                     break;
  442.                 case 31:
  443.                     $result = $this->__object->$method
  444.                     (
  445.                         $arg[0], $arg[1], $arg[2], $arg[3], $arg[4], $arg[5],
  446.                         $arg[6], $arg[7], $arg[8], $arg[9], $arg[10], $arg[11],
  447.                         $arg[12], $arg[13], $arg[14], $arg[15], $arg[16], $arg[17],
  448.                         $arg[18], $arg[19], $arg[20], $arg[21], $arg[22], $arg[23],
  449.                         $arg[24], $arg[25], $arg[26], $arg[27], $arg[28], $arg[29],
  450.                         $arg[30]
  451.                        
  452.                     );
  453.                     break;
  454.                 case 32:
  455.                     $result = $this->__object->$method
  456.                     (
  457.                         $arg[0], $arg[1], $arg[2], $arg[3], $arg[4], $arg[5],
  458.                         $arg[6], $arg[7], $arg[8], $arg[9], $arg[10], $arg[11],
  459.                         $arg[12], $arg[13], $arg[14], $arg[15], $arg[16], $arg[17],
  460.                         $arg[18], $arg[19], $arg[20], $arg[21], $arg[22], $arg[23],
  461.                         $arg[24], $arg[25], $arg[26], $arg[27], $arg[28], $arg[29],
  462.                         $arg[30], $arg[32]
  463.                        
  464.                     );
  465.                     break;
  466.                 default:
  467.                     $result = call_user_func_array([$this->__object, $method], $args);
  468.             endswitch;
  469.            
  470.             return (NULL === $result)
  471.                 ? $this
  472.                 : $result;
  473.         }
  474.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement