1
|
|
|
<?php |
2
|
|
|
namespace rtens\domin\reflection; |
3
|
|
|
|
4
|
|
|
use rtens\domin\Action; |
5
|
|
|
use rtens\domin\Parameter; |
6
|
|
|
|
7
|
|
|
class GenericAction implements Action { |
8
|
|
|
|
9
|
|
|
/** @var null|callable */ |
10
|
|
|
private $execute; |
11
|
|
|
|
12
|
|
|
/** @var null|callable */ |
13
|
|
|
private $afterExecute; |
14
|
|
|
|
15
|
|
|
/** @var null|callable */ |
16
|
|
|
private $fill; |
17
|
|
|
|
18
|
|
|
/** @var null|string */ |
19
|
|
|
private $caption; |
20
|
|
|
|
21
|
|
|
/** @var null|string */ |
22
|
|
|
private $description; |
23
|
|
|
|
24
|
|
|
/** @var array|callable[] by parameter name */ |
25
|
|
|
private $paramMap = []; |
26
|
|
|
|
27
|
|
|
/** @var null|Parameter[] */ |
28
|
|
|
private $parameters; |
29
|
|
|
|
30
|
|
|
/** @var Action */ |
31
|
|
|
private $action; |
32
|
|
|
|
33
|
|
|
public function __construct(Action $action) { |
34
|
|
|
$this->action = $action; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function setCaption($caption) { |
38
|
|
|
$this->caption = $caption; |
39
|
|
|
return $this; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function caption() { |
43
|
|
|
return !is_null($this->caption) ? $this->caption : $this->action->caption(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function setDescription($description) { |
47
|
|
|
$this->description = $description ?: ''; |
48
|
|
|
return $this; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function description() { |
52
|
|
|
return !is_null($this->description) ? $this->description : $this->action->description(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function setExecute(callable $execute) { |
56
|
|
|
$this->execute = $execute; |
57
|
|
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function execute(array $parameters) { |
61
|
|
|
if ($this->execute) { |
62
|
|
|
$result = call_user_func($this->execute, $parameters); |
63
|
|
|
} else { |
64
|
|
|
$result = $this->action->execute($parameters); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
if ($this->afterExecute) { |
68
|
|
|
$result = call_user_func($this->afterExecute, $result); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $result; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param callable $callback Filters the return value of execute |
76
|
|
|
* @return static |
77
|
|
|
*/ |
78
|
|
|
public function setAfterExecute(callable $callback) { |
79
|
|
|
$this->afterExecute = $callback; |
80
|
|
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param Parameter[] $parameters |
85
|
|
|
* @return static |
86
|
|
|
*/ |
87
|
|
|
public function setParameters(array $parameters) { |
88
|
|
|
$this->parameters = $parameters; |
89
|
|
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param string $name |
94
|
|
|
* @param callable $map Receives Parameter and returns Parameter |
95
|
|
|
* @return static |
96
|
|
|
*/ |
97
|
|
|
public function mapParameter($name, callable $map) { |
98
|
|
|
$this->paramMap[$name] = $map; |
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return \rtens\domin\Parameter[] |
104
|
|
|
* @throws \Exception |
105
|
|
|
*/ |
106
|
|
|
public function parameters() { |
107
|
|
|
return array_map(function (Parameter $parameter) { |
108
|
|
|
if (array_key_exists($parameter->getName(), $this->paramMap)) { |
109
|
|
|
return call_user_func($this->paramMap[$parameter->getName()], $parameter); |
110
|
|
|
} |
111
|
|
|
return $parameter; |
112
|
|
|
}, $this->parameters ?: $this->action->parameters()); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function setFill(callable $fill) { |
116
|
|
|
$this->fill = $fill; |
117
|
|
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function fill(array $parameters) { |
121
|
|
|
if ($this->fill) { |
122
|
|
|
$parameters = call_user_func($this->fill, $parameters); |
123
|
|
|
} else { |
124
|
|
|
$parameters = $this->action->fill($parameters); |
125
|
|
|
} |
126
|
|
|
return $parameters; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return Action |
131
|
|
|
*/ |
132
|
|
|
public function action() { |
133
|
|
|
return $this->action; |
134
|
|
|
} |
135
|
|
|
} |