XRL  latest
Simple XML-RPC Library (both client and server)
CallableObject.php
1 <?php
2 /*
3  * This file is part of XRL, a simple XML-RPC Library for PHP.
4  *
5  * Copyright (c) 2012, XRL Team. All rights reserved.
6  * XRL is licensed under the 3-clause BSD License.
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11 
12 namespace fpoirotte\XRL;
13 
24 {
26  protected $callableObj;
27 
29  protected $representation;
30 
48  public function __construct($callable)
49  {
50  if (!is_callable($callable, false, $representation)) {
51  throw new \InvalidArgumentException('Not a valid callable');
52  }
53 
54  // This happens for anonymous functions
55  // created with create_function().
56  if (is_string($callable) && $representation == "") {
57  $representation = $callable;
58  }
59 
60  $this->callableObj = $callable;
61  $this->representation = $representation;
62  }
63 
65  public function getCallable()
66  {
67  return $this->callableObj;
68  }
69 
71  public function getRepresentation()
72  {
73  return $this->representation;
74  }
75 
77  public function __invoke(&...$args)
78  {
79  return call_user_func_array($this->callableObj, $args);
80  }
81 
83  public function __toString()
84  {
85  return $this->representation;
86  }
87 
89  public function getReflector()
90  {
91  $parts = explode('::', $this->representation);
92 
93  // Did we wrap a function?
94  if (count($parts) == 1) {
95  return new \ReflectionFunction($this->callableObj);
96  }
97 
98  // Did we wrap a Closure or some invokable object?
99  if (!is_array($this->callableObj)) {
100  $callable = array($this->callableObj, $parts[1]);
101  } else {
102  $callable = $this->callableObj;
103  }
104  return new \ReflectionMethod($callable[0], $callable[1]);
105  }
106 }
$callableObj
Inner callable object, as used by PHP.
Interface for something that can be called.
$representation
Human representation of the inner callable.
Class used to represent anything that is callable.