|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is a part of "Axessors" library. |
|
4
|
|
|
* |
|
5
|
|
|
* @author <[email protected]> |
|
6
|
|
|
* @license GPL |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace NoOne4rever\Axessors; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class MethodsProcessor. |
|
13
|
|
|
* |
|
14
|
|
|
* Processes Axessors methods. |
|
15
|
|
|
* |
|
16
|
|
|
* @package NoOne4rever\Axessors |
|
17
|
|
|
*/ |
|
18
|
|
|
class MethodsProcessor |
|
19
|
|
|
{ |
|
20
|
|
|
/** @var string access modifier for setter */ |
|
21
|
|
|
private $writingAccess; |
|
22
|
|
|
/** @var string access modifier for getter */ |
|
23
|
|
|
private $readingAccess; |
|
24
|
|
|
/** @var string field name */ |
|
25
|
|
|
private $name; |
|
26
|
|
|
/** @var array generated methods */ |
|
27
|
|
|
private $methods = []; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* MethodsProcessor constructor. |
|
31
|
|
|
* |
|
32
|
|
|
* @param string $writingAccess access modifier for setter |
|
33
|
|
|
* @param string $readingAccess access modifier for getter |
|
34
|
|
|
* @param string $name field name |
|
35
|
|
|
*/ |
|
36
|
|
|
public function __construct(string $writingAccess, string $readingAccess, string $name) |
|
37
|
|
|
{ |
|
38
|
|
|
$this->name = $name; |
|
39
|
|
|
$this->writingAccess = $writingAccess; |
|
40
|
|
|
$this->readingAccess = $readingAccess; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Generates list of methods for property. |
|
45
|
|
|
* |
|
46
|
|
|
* @param array $typeTree type tree |
|
47
|
|
|
* |
|
48
|
|
|
* @return string[] methods' names |
|
49
|
|
|
*/ |
|
50
|
|
|
public function processMethods(array $typeTree): array |
|
51
|
|
|
{ |
|
52
|
|
|
$this->methods = []; |
|
53
|
|
|
$this->processAccessors(); |
|
54
|
|
|
foreach ($typeTree as $index => $type) { |
|
55
|
|
|
$class = is_int($index) ? $type : $index; |
|
56
|
|
|
foreach ((new \ReflectionClass($class))->getMethods() as $method) { |
|
57
|
|
|
if (!($method->isStatic() && $method->isPublic() && !$method->isAbstract())) { |
|
58
|
|
|
continue; |
|
59
|
|
|
} |
|
60
|
|
|
$this->processAxessorsMethod($method); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
return $this->methods; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Adds getter and setter to methods list. |
|
68
|
|
|
*/ |
|
69
|
|
|
private function processAccessors(): void |
|
70
|
|
|
{ |
|
71
|
|
|
if ($this->readingAccess !== '') { |
|
72
|
|
|
$this->methods[$this->readingAccess][] = 'get' . ucfirst($this->name); |
|
73
|
|
|
} |
|
74
|
|
|
if ($this->writingAccess !== '') { |
|
75
|
|
|
$this->methods[$this->writingAccess][] = 'set' . ucfirst($this->name); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Adds Axessors method to methods list. |
|
81
|
|
|
* |
|
82
|
|
|
* @param \ReflectionMethod $method reflection |
|
83
|
|
|
*/ |
|
84
|
|
|
private function processAxessorsMethod(\ReflectionMethod $method): void |
|
85
|
|
|
{ |
|
86
|
|
|
if (substr($method->name, 0, 5) == 'm_in_' && $this->writingAccess !== '') { |
|
87
|
|
|
$this->methods[$this->writingAccess][] = str_replace('PROPERTY', ucfirst($this->name), |
|
88
|
|
|
substr($method->name, 5)); |
|
89
|
|
|
} elseif (substr($method->name, 0, 6) == 'm_out_' && $this->readingAccess !== '') { |
|
90
|
|
|
$this->methods[$this->readingAccess][] = str_replace('PROPERTY', ucfirst($this->name), |
|
91
|
|
|
substr($method->name, 6)); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
} |