|
1
|
|
|
<?php |
|
2
|
|
|
namespace rtens\domin\reflection; |
|
3
|
|
|
|
|
4
|
|
|
use rtens\domin\Action; |
|
5
|
|
|
use rtens\domin\Parameter; |
|
6
|
|
|
use rtens\domin\reflection\types\TypeFactory; |
|
7
|
|
|
use watoki\reflect\MethodAnalyzer; |
|
8
|
|
|
|
|
9
|
|
|
abstract class StaticMethodAction implements Action { |
|
10
|
|
|
|
|
11
|
|
|
/** @var \ReflectionMethod */ |
|
12
|
|
|
protected $method; |
|
13
|
|
|
|
|
14
|
|
|
/** @var TypeFactory */ |
|
15
|
|
|
protected $types; |
|
16
|
|
|
|
|
17
|
|
|
/** @var CommentParser */ |
|
18
|
|
|
protected $parser; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @param \ReflectionMethod $method |
|
22
|
|
|
* @param TypeFactory $types |
|
23
|
|
|
* @param CommentParser $parser |
|
24
|
|
|
*/ |
|
25
|
|
|
public function __construct(\ReflectionMethod $method, TypeFactory $types, CommentParser $parser) { |
|
26
|
|
|
$this->method = $method; |
|
27
|
|
|
$this->types = $types; |
|
28
|
|
|
$this->parser = $parser; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @return string |
|
33
|
|
|
*/ |
|
34
|
|
|
public function caption() { |
|
35
|
|
|
return |
|
36
|
|
|
$this->unCamelize($this->method->getDeclaringClass()->getShortName()) . |
|
37
|
|
|
': ' . $this->unCamelize($this->method->name); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
protected function unCamelize($camel) { |
|
41
|
|
|
return ucfirst(preg_replace('/(.)([A-Z0-9])/', '$1 $2', $camel)); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @return string|null |
|
46
|
|
|
*/ |
|
47
|
|
|
public function description() { |
|
48
|
|
|
$lines = array_slice(explode("\n", $this->method->getDocComment()), 1, -1); |
|
49
|
|
|
$lines = array_map(function ($line) { |
|
50
|
|
|
return ltrim($line, ' *'); |
|
51
|
|
|
}, $lines); |
|
52
|
|
|
$lines = array_filter($lines, function ($line) { |
|
53
|
|
|
return substr($line, 0, 1) != '@'; |
|
54
|
|
|
}); |
|
55
|
|
|
return $this->parser->parse(trim(implode("\n", $lines))); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @return Parameter[] |
|
60
|
|
|
*/ |
|
61
|
|
|
public function parameters() { |
|
62
|
|
|
$analyzer = new MethodAnalyzer($this->method); |
|
63
|
|
|
|
|
64
|
|
|
$parameters = $this->initParameters(); |
|
65
|
|
|
foreach ($this->method->getParameters() as $parameter) { |
|
66
|
|
|
$type = $analyzer->getType($parameter, $this->types); |
|
67
|
|
|
$parameters[] = (new Parameter($parameter->name, $type, !$parameter->isDefaultValueAvailable())) |
|
68
|
|
|
->setDescription($this->parser->parse($analyzer->getComment($parameter))); |
|
69
|
|
|
} |
|
70
|
|
|
return $parameters; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @return Parameter[] |
|
75
|
|
|
*/ |
|
76
|
|
|
protected function initParameters() { |
|
77
|
|
|
return []; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Fills out partially available parameters |
|
82
|
|
|
* |
|
83
|
|
|
* @param array $parameters Available values indexed by name |
|
84
|
|
|
* @return array Filled values indexed by name |
|
85
|
|
|
*/ |
|
86
|
|
|
public function fill(array $parameters) { |
|
87
|
|
|
foreach ($this->method->getParameters() as $parameter) { |
|
88
|
|
|
if ($parameter->isDefaultValueAvailable() && !array_key_exists($parameter->name, $parameters)) { |
|
89
|
|
|
$parameters[$parameter->name] = $parameter->getDefaultValue(); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
return $parameters; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @return boolean True if the action modifies the state of the application |
|
97
|
|
|
*/ |
|
98
|
|
|
public function isModifying() { |
|
99
|
|
|
return true; |
|
100
|
|
|
} |
|
101
|
|
|
} |