1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PEIP\Handler; |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the PEIP package. |
7
|
|
|
* (c) 2009-2016 Timo Michna <timomichna/yahoo.de> |
8
|
|
|
* |
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
10
|
|
|
* file that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* CallableHandler |
15
|
|
|
* Class to wrap a PHP callable in a handler. |
16
|
|
|
* |
17
|
|
|
* @author Timo Michna <timomichna/yahoo.de> |
18
|
|
|
* @implements \PEIP\INF\Handler\Handler |
19
|
|
|
*/ |
20
|
|
|
class CallableHandler implements \PEIP\INF\Handler\Handler |
21
|
|
|
{ |
22
|
|
|
protected $callable; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* constructor. |
26
|
|
|
* |
27
|
|
|
* @param callable $callable the callable to wrap with the handler |
28
|
|
|
* |
29
|
|
|
* @return |
30
|
|
|
*/ |
31
|
|
|
public function __construct($callable) |
32
|
|
|
{ |
33
|
|
|
$this->callable = $callable; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Handles a subject by calling the wrapped callable with the subject as argument. |
38
|
|
|
* |
39
|
|
|
* @implements \PEIP\INF\Handler\Handler |
40
|
|
|
* |
41
|
|
|
* @param mixed $subject the subject to handle |
42
|
|
|
* |
43
|
|
|
* @return mixed result of calling the registered callable with given subject |
44
|
|
|
*/ |
45
|
|
|
public function handle($subject) |
46
|
|
|
{ |
47
|
|
|
return call_user_func($this->callable, $subject); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Allows the handler instance to act as a lambda function. |
52
|
|
|
* Delegates to own 'handle' method. |
53
|
|
|
* |
54
|
|
|
* @param mixed $subject the subject to handle |
55
|
|
|
* |
56
|
|
|
* @return mixed result of calling the registered callable with given subject |
57
|
|
|
* |
58
|
|
|
* @see CallableHandler::handle |
59
|
|
|
*/ |
60
|
|
|
public function __invoke($subject) |
61
|
|
|
{ |
62
|
|
|
return $this->handle($subject); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Returns the callable wrapped by the handler instance. |
67
|
|
|
* |
68
|
|
|
* @return callable callable wrapped by the handler |
69
|
|
|
*/ |
70
|
|
|
public function getCallable() |
71
|
|
|
{ |
72
|
|
|
return $this->callable; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|