|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Micro framework package. |
|
7
|
|
|
* |
|
8
|
|
|
* (c) Stanislau Komar <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Micro\Library\DTO\Preparation\Processor; |
|
15
|
|
|
|
|
16
|
|
|
use Micro\Library\DTO\ClassDef\ClassDefinition; |
|
17
|
|
|
use Micro\Library\DTO\ClassDef\MethodDefinition; |
|
18
|
|
|
use Micro\Library\DTO\ClassDef\PropertyDefinition; |
|
19
|
|
|
use Micro\Library\DTO\Helper\NameNormalizerInterface; |
|
20
|
|
|
use Micro\Library\DTO\Preparation\PreparationProcessorInterface; |
|
21
|
|
|
|
|
22
|
|
|
class MethodGetProcessor implements PreparationProcessorInterface |
|
23
|
|
|
{ |
|
24
|
6 |
|
public function __construct(private NameNormalizerInterface $nameNormalizer) |
|
25
|
|
|
{ |
|
26
|
6 |
|
} |
|
27
|
|
|
|
|
28
|
1 |
|
public function process(array $classDef, ClassDefinition $classDefinition, array $classList): void |
|
29
|
|
|
{ |
|
30
|
1 |
|
$properties = $classDefinition->getProperties(); |
|
31
|
1 |
|
foreach ($properties as $property) { |
|
32
|
1 |
|
$this->provideGetMethod($classDefinition, $property); |
|
33
|
|
|
} |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
1 |
|
protected function provideGetMethod(ClassDefinition $classDefinition, PropertyDefinition $propertyDefinition): void |
|
37
|
|
|
{ |
|
38
|
1 |
|
$propertyName = $propertyDefinition->getName(); |
|
39
|
1 |
|
$methodSuffix = $this->nameNormalizer->normalize($propertyName); |
|
40
|
1 |
|
$methodDefinition = new MethodDefinition(); |
|
41
|
1 |
|
$methodDefinition->setName('get'.ucfirst($methodSuffix)); |
|
42
|
1 |
|
$methodDefinition->setTypesReturn($propertyDefinition->getTypes()); |
|
43
|
1 |
|
$methodDefinition->setVisibility('public'); |
|
44
|
1 |
|
$methodDefinition->setBody(sprintf('return $this->%s;', $propertyName)); |
|
45
|
|
|
// $methodDefinition->addComment(sprintf('@return %s', implode('|', $propertyDefinition->getTypes()))); |
|
46
|
|
|
|
|
47
|
1 |
|
$classDefinition->addMethod($methodDefinition); |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
|