Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
39 / 39
100.00% covered (success)
100.00%
22 / 22
CRAP
100.00% covered (success)
100.00%
1 / 1
AbstractCallable
100.00% covered (success)
100.00%
39 / 39
100.00% covered (success)
100.00%
22 / 22
29
100.00% covered (success)
100.00%
1 / 1
 setCallable
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getCallable
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getCallableType
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setParameters
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 addParameters
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 addParameter
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 addNamedParameter
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getParameters
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getParameter
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 hasParameters
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasParameter
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 removeParameter
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 removeParameters
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 setConstructorParams
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getConstructorParams
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getConstructorParam
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 hasConstructorParams
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasConstructorParam
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 removeConstructorParam
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 removeConstructorParams
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 isCallable
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 wasCalled
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 prepare
n/a
0 / 0
n/a
0 / 0
0
 prepareParameters
n/a
0 / 0
n/a
0 / 0
0
 call
n/a
0 / 0
n/a
0 / 0
0
1<?php
2/**
3 * Pop PHP Framework (http://www.popphp.org/)
4 *
5 * @link       https://github.com/popphp/popphp-framework
6 * @author     Nick Sagona, III <dev@nolainteractive.com>
7 * @copyright  Copyright (c) 2009-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com)
8 * @license    http://www.popphp.org/license     New BSD License
9 */
10
11/**
12 * @namespace
13 */
14namespace Pop\Utils;
15
16/**
17 * Pop utils callable object class
18 *
19 * @category   Pop
20 * @package    Pop\Utils
21 * @author     Nick Sagona, III <dev@nolainteractive.com>
22 * @copyright  Copyright (c) 2009-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com)
23 * @license    http://www.popphp.org/license     New BSD License
24 * @version    1.3.0
25 */
26abstract class AbstractCallable implements CallableInterface
27{
28
29    /**
30     * Call type constants
31     */
32    const FUNCTION                = 'FUNCTION';                // Function
33    const FUNCTION_PARAMS         = 'FUNCTION_PARAMS';         // Function w/ parameters
34    const CLOSURE                 = 'CLOSURE';                 // Closure
35    const CLOSURE_PARAMS          = 'CLOSURE_PARAMS';          // Closure w/ parameters
36    const STATIC_CALL             = 'STATIC_CALL';             // 'Class::method'
37    const STATIC_CALL_PARAMS      = 'STATIC_CALL_PARAMS';      // 'Class::method' w/ parameters
38    const INSTANCE_CALL           = 'INSTANCE_CALL';           // 'Class->method'
39    const INSTANCE_CALL_PARAMS    = 'INSTANCE_CALL_PARAMS';    // 'Class->method' w/ parameters
40    const CONSTRUCTOR_CALL        = 'CONSTRUCTOR_CALL';        // 'Class'
41    const CONSTRUCTOR_CALL_PARAMS = 'CONSTRUCTOR_CALL_PARAMS'; // 'Class' w/ parameters
42    const IS_CALLABLE             = 'IS_CALLABLE';             // Is a basic callable entity
43    const IS_CALLABLE_PARAMS      = 'IS_CALLABLE_PARAMS';      // Is a basic callable entity w/ parameters
44    const NEW_OBJECT              = 'NEW_OBJECT';              // 'new Class'
45    const OBJECT                  = 'OBJECT';                  // Already an instantiated object
46
47    /**
48     * Callable
49     * @var mixed
50     */
51    protected $callable = null;
52
53    /**
54     * Parameters
55     * @var array
56     */
57    protected $parameters = [];
58
59    /**
60     * Callable type
61     * @var string
62     */
63    protected $callableType = null;
64
65    /**
66     * Was called flag
67     * @var boolean
68     */
69    protected $wasCalled = false;
70
71    /**
72     * Callable class
73     * @var string
74     */
75    protected $class = null;
76
77    /**
78     * Callable method
79     * @var string
80     */
81    protected $method = null;
82
83    /**
84     * Constructor parameters for instance calls
85     * @var array
86     */
87    protected $constructorParams = [];
88
89    /**
90     * Set callable
91     *
92     * @param  mixed $callable
93     * @return AbstractCallable
94     */
95    public function setCallable($callable)
96    {
97        $this->callable = $callable;
98        return $this;
99    }
100
101    /**
102     * Get callable
103     *
104     * @return mixed
105     */
106    public function getCallable()
107    {
108        return $this->callable;
109    }
110
111    /**
112     * Get callable type
113     *
114     * @return string
115     */
116    public function getCallableType()
117    {
118        return $this->callableType;
119    }
120
121    /**
122     * Set parameters
123     *
124     * @param  array $parameters
125     * @return AbstractCallable
126     */
127    public function setParameters(array $parameters)
128    {
129        $this->parameters = $parameters;
130        return $this;
131    }
132
133    /**
134     * Add parameters
135     *
136     * @param  array $parameters
137     * @return AbstractCallable
138     */
139    public function addParameters(array $parameters)
140    {
141        foreach ($parameters as $key => $value) {
142            if (is_numeric($key)) {
143                $this->addParameter($value);
144            } else {
145                $this->addNamedParameter($key, $value);
146            }
147        }
148        return $this;
149    }
150
151    /**
152     * Add a parameter
153     *
154     * @param  mixed $parameter
155     * @return AbstractCallable
156     */
157    public function addParameter($parameter)
158    {
159        $this->parameters[] = $parameter;
160        return $this;
161    }
162
163    /**
164     * Add a parameter
165     *
166     * @param  string $name
167     * @param  mixed  $parameter
168     * @return AbstractCallable
169     */
170    public function addNamedParameter($name, $parameter)
171    {
172        $this->parameters[$name] = $parameter;
173        return $this;
174    }
175
176    /**
177     * Get parameters
178     *
179     * @return array
180     */
181    public function getParameters()
182    {
183        return $this->parameters;
184    }
185
186    /**
187     * Get a parameter
188     *
189     * @param  string $key
190     * @return mixed
191     */
192    public function getParameter($key)
193    {
194        return (isset($this->parameters[$key])) ? $this->parameters[$key] : null;
195    }
196
197    /**
198     * Has parameters
199     *
200     * @return boolean
201     */
202    public function hasParameters()
203    {
204        return !empty($this->parameters);
205    }
206
207    /**
208     * Has a parameter
209     *
210     * @param  string $key
211     * @return boolean
212     */
213    public function hasParameter($key)
214    {
215        return isset($this->parameters[$key]);
216    }
217
218    /**
219     * Remove a parameter
220     *
221     * @param  string $key
222     * @return AbstractCallable
223     */
224    public function removeParameter($key)
225    {
226        if (isset($this->parameters[$key])) {
227            unset($this->parameters[$key]);
228        }
229        return $this;
230    }
231
232    /**
233     * Remove all parameters
234     *
235     * @return AbstractCallable
236     */
237    public function removeParameters()
238    {
239        $this->parameters = [];
240        return $this;
241    }
242
243    /**
244     * Set constructor parameters
245     *
246     * @param  array $constructorParams
247     * @return AbstractCallable
248     */
249    public function setConstructorParams(array $constructorParams)
250    {
251        $this->constructorParams = $constructorParams;
252        return $this;
253    }
254
255    /**
256     * Get constructor parameters for instance call
257     *
258     * @return array
259     */
260    public function getConstructorParams()
261    {
262        return $this->constructorParams;
263    }
264
265    /**
266     * Get a constructor parameter for instance call
267     *
268     * @param  string $key
269     * @return mixed
270     */
271    public function getConstructorParam($key)
272    {
273        return (isset($this->constructorParams[$key])) ? $this->constructorParams[$key] : null;
274    }
275
276    /**
277     * Has constructor parameters for instance call
278     *
279     * @return boolean
280     */
281    public function hasConstructorParams()
282    {
283        return !empty($this->constructorParams);
284    }
285
286    /**
287     * Has a constructor parameter for instance call
288     *
289     * @param  string $key
290     * @return boolean
291     */
292    public function hasConstructorParam($key)
293    {
294        return isset($this->constructorParams[$key]);
295    }
296
297    /**
298     * Remove a constructor parameter for instance call
299     *
300     * @param  string $key
301     * @return AbstractCallable
302     */
303    public function removeConstructorParam($key)
304    {
305        if (isset($this->constructorParams[$key])) {
306            unset($this->constructorParams[$key]);
307        }
308        return $this;
309    }
310
311    /**
312     * Remove all constructor parameters for instance call
313     *
314     * @return AbstractCallable
315     */
316    public function removeConstructorParams()
317    {
318        $this->constructorParams = [];
319        return $this;
320    }
321
322    /**
323     * Check if object is callable
324     *
325     * @return boolean
326     */
327    public function isCallable()
328    {
329        if (null === $this->callableType) {
330            $this->prepare();
331        }
332
333        return (null !== $this->callableType);
334    }
335
336    /**
337     * Check if object was called
338     *
339     * @return boolean
340     */
341    public function wasCalled()
342    {
343        return $this->wasCalled;
344    }
345
346    /**
347     * Prepare object for call
348     *
349     * @return AbstractCallable
350     */
351    abstract public function prepare();
352
353    /**
354     * Prepare parameters
355     *
356     * @return AbstractCallable
357     */
358    abstract public function prepareParameters();
359
360    /**
361     * Execute the call
362     *
363     * @param  mixed $parameters
364     * @return mixed
365     */
366    abstract public function call($parameters = null);
367
368}