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
|
2 |
|
public function __construct(string $writingAccess, string $readingAccess, string $name) |
37
|
|
|
{ |
38
|
2 |
|
$this->name = $name; |
39
|
2 |
|
$this->writingAccess = $writingAccess; |
40
|
2 |
|
$this->readingAccess = $readingAccess; |
41
|
2 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Generates list of methods for property. |
45
|
|
|
* |
46
|
|
|
* @param array $typeTree type tree |
47
|
|
|
* |
48
|
|
|
* @return string[] methods' names |
49
|
|
|
*/ |
50
|
2 |
|
public function processMethods(array $typeTree): array |
51
|
|
|
{ |
52
|
2 |
|
$this->methods = []; |
53
|
2 |
|
$this->processAccessors(); |
54
|
2 |
|
foreach ($typeTree as $index => $type) { |
55
|
2 |
|
$class = is_int($index) ? $type : $index; |
56
|
2 |
|
foreach ((new \ReflectionClass($class))->getMethods() as $method) { |
57
|
2 |
|
if (!($method->isStatic() && $method->isPublic() && !$method->isAbstract())) { |
58
|
2 |
|
continue; |
59
|
|
|
} |
60
|
2 |
|
$this->processAxessorsMethod($method); |
61
|
|
|
} |
62
|
|
|
} |
63
|
2 |
|
return $this->methods; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Adds getter and setter to methods list. |
68
|
|
|
*/ |
69
|
2 |
|
private function processAccessors(): void |
70
|
|
|
{ |
71
|
2 |
|
if ($this->readingAccess !== '') { |
72
|
2 |
|
$this->methods[$this->readingAccess][] = 'get' . ucfirst($this->name); |
73
|
|
|
} |
74
|
2 |
|
if ($this->writingAccess !== '') { |
75
|
2 |
|
$this->methods[$this->writingAccess][] = 'set' . ucfirst($this->name); |
76
|
|
|
} |
77
|
2 |
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Adds Axessors method to methods list. |
81
|
|
|
* |
82
|
|
|
* @param \ReflectionMethod $method reflection |
83
|
|
|
*/ |
84
|
2 |
|
private function processAxessorsMethod(\ReflectionMethod $method): void |
85
|
|
|
{ |
86
|
2 |
|
if (substr($method->name, 0, 5) == 'm_in_' && $this->writingAccess !== '') { |
87
|
2 |
|
$this->methods[$this->writingAccess][] = str_replace('PROPERTY', ucfirst($this->name), |
88
|
2 |
|
substr($method->name, 5)); |
89
|
2 |
|
} elseif (substr($method->name, 0, 6) == 'm_out_' && $this->readingAccess !== '') { |
90
|
2 |
|
$this->methods[$this->readingAccess][] = str_replace('PROPERTY', ucfirst($this->name), |
91
|
2 |
|
substr($method->name, 6)); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
} |