1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright (c) 2016-present Ganbaro Digital Ltd |
5
|
|
|
* All rights reserved. |
6
|
|
|
* |
7
|
|
|
* Redistribution and use in source and binary forms, with or without |
8
|
|
|
* modification, are permitted provided that the following conditions |
9
|
|
|
* are met: |
10
|
|
|
* |
11
|
|
|
* * Redistributions of source code must retain the above copyright |
12
|
|
|
* notice, this list of conditions and the following disclaimer. |
13
|
|
|
* |
14
|
|
|
* * Redistributions in binary form must reproduce the above copyright |
15
|
|
|
* notice, this list of conditions and the following disclaimer in |
16
|
|
|
* the documentation and/or other materials provided with the |
17
|
|
|
* distribution. |
18
|
|
|
* |
19
|
|
|
* * Neither the names of the copyright holders nor the names of his |
20
|
|
|
* contributors may be used to endorse or promote products derived |
21
|
|
|
* from this software without specific prior written permission. |
22
|
|
|
* |
23
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
24
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
25
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
26
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
27
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
28
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
29
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
30
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
31
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
32
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
33
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
34
|
|
|
* POSSIBILITY OF SUCH DAMAGE. |
35
|
|
|
* |
36
|
|
|
* @category Libraries |
37
|
|
|
* @package Actionables/Values |
38
|
|
|
* @author Stuart Herbert <[email protected]> |
39
|
|
|
* @copyright 2016-present Ganbaro Digital Ltd www.ganbarodigital.com |
40
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
41
|
|
|
* @link http://code.ganbarodigital.com/php-data-containers |
42
|
|
|
*/ |
43
|
|
|
|
44
|
|
|
namespace GanbaroDigital\Actionables\Values; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* An Actionable represents: |
48
|
|
|
* |
49
|
|
|
* - a callable piece of PHP code |
50
|
|
|
* - with associated metadata |
51
|
|
|
* |
52
|
|
|
* It's the metadata that adds the real value. If your app doesn't need |
53
|
|
|
* the metadata, you're better off sticking with PHP's native callables. |
54
|
|
|
*/ |
55
|
|
|
class Actionable |
56
|
|
|
{ |
57
|
|
|
/** |
58
|
|
|
* the callable code that we represent |
59
|
|
|
* @var callable |
60
|
|
|
*/ |
61
|
|
|
private $action; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* the full path to the file where $action's code is defined |
65
|
|
|
* @var string |
66
|
|
|
*/ |
67
|
|
|
private $sourceFilename; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* a (possibly empty) list of tags that describe this Actionable |
71
|
|
|
* |
72
|
|
|
* @var array |
73
|
|
|
*/ |
74
|
|
|
private $tags = []; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* constructor |
78
|
|
|
* |
79
|
|
|
* @param callable $action |
80
|
|
|
* the callable code that we represent |
81
|
|
|
* @param string $sourceFilename |
82
|
|
|
* the full path to the file where $action's code is defined |
83
|
|
|
* @param array $tags |
84
|
|
|
* a list of tags that describe this Actionable |
85
|
|
|
*/ |
86
|
|
|
public function __construct(callable $action, $sourceFilename, $tags = []) |
87
|
|
|
{ |
88
|
|
|
$this->action = $action; |
89
|
|
|
$this->sourceFilename = $sourceFilename; |
90
|
|
|
$this->tags = array_combine(array_values($tags), array_values($tags)); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* execute this callable |
95
|
|
|
* |
96
|
|
|
* @param array $params |
97
|
|
|
* the parameters to pass into our callable |
98
|
|
|
* @return mixed |
99
|
|
|
* returns whatever our callable returns |
100
|
|
|
*/ |
101
|
|
|
public function __invoke(array $params) |
102
|
|
|
{ |
103
|
|
|
return call_user_func_array($this->action, $params); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* what is the full path to the PHP file where the action's code is defined? |
108
|
|
|
* |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
|
|
public function getSourceFilename() |
112
|
|
|
{ |
113
|
|
|
return $this->sourceFilename; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* what is the full list of this Actionable's tags? |
118
|
|
|
* |
119
|
|
|
* @return array |
120
|
|
|
*/ |
121
|
|
|
public function getTags() |
122
|
|
|
{ |
123
|
|
|
return array_values($this->tags); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* does this Actionable have a specific tag? |
128
|
|
|
* |
129
|
|
|
* @param string $tagName |
130
|
|
|
* the tag that we want to check for |
131
|
|
|
* @return boolean |
132
|
|
|
* TRUE if we have this tag |
133
|
|
|
* FALSE otherwise |
134
|
|
|
*/ |
135
|
|
|
public function hasTag($tagName) |
136
|
|
|
{ |
137
|
|
|
return isset($this->tags[$tagName]); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|