1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
/** |
3
|
|
|
* Created by Vitaly Iegorov <[email protected]>. |
4
|
|
|
* on 03.09.16 at 11:30 |
5
|
|
|
*/ |
6
|
|
|
namespace samsonphp\generator; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Function generation class. |
10
|
|
|
* |
11
|
|
|
* @author Vitaly Egorov <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class FunctionGenerator extends AbstractGenerator |
14
|
|
|
{ |
15
|
|
|
use CodeTrait; |
16
|
|
|
|
17
|
|
|
/** @var string Function name */ |
18
|
|
|
protected $name; |
19
|
|
|
|
20
|
|
|
/** @var array Collection of function arguments */ |
21
|
|
|
protected $arguments = []; |
22
|
|
|
|
23
|
|
|
/** @var array Collection of function arguments descriptions */ |
24
|
|
|
protected $argumentDescriptions = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* FunctionGenerator constructor. |
28
|
|
|
* |
29
|
|
|
* @param string $name Function name |
30
|
|
|
* @param AbstractGenerator $parent Parent Generator |
31
|
|
|
*/ |
32
|
|
|
public function __construct(string $name, AbstractGenerator $parent = null) |
33
|
|
|
{ |
34
|
|
|
$this->name = $name; |
35
|
|
|
|
36
|
|
|
parent::__construct($parent); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Set function argument. |
41
|
|
|
* |
42
|
|
|
* @param string $name Argument name |
43
|
|
|
* @param string|null $type Argument type |
44
|
|
|
* @param string $description Argument description |
45
|
|
|
* |
46
|
|
|
* @return FunctionGenerator |
47
|
|
|
*/ |
48
|
|
|
public function defArgument(string $name, string $type = null, string $description = null) : FunctionGenerator |
49
|
|
|
{ |
50
|
|
|
$this->arguments[$name] = $type; |
51
|
|
|
$this->argumentDescriptions[$name] = $description; |
52
|
|
|
|
53
|
|
|
return $this; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritdoc} |
58
|
|
|
*/ |
59
|
|
|
public function code(int $indentation = 0) : string |
60
|
|
|
{ |
61
|
|
|
$innerIndentation = $this->indentation(1); |
62
|
|
|
|
63
|
|
|
$formattedCode = [ |
64
|
|
|
$this->buildDefinition() . '(' . $this->buildArguments($this->arguments) . ')', |
65
|
|
|
'{' |
66
|
|
|
]; |
67
|
|
|
|
68
|
|
|
// Prepend inner indentation to code |
69
|
|
|
foreach ($this->code as $codeLine) { |
70
|
|
|
$formattedCode[] = $innerIndentation.$codeLine; |
71
|
|
|
} |
72
|
|
|
$formattedCode[] = '}'; |
73
|
|
|
|
74
|
|
|
$code = implode("\n" . $this->indentation($indentation), $formattedCode); |
75
|
|
|
|
76
|
|
|
// Add comments |
77
|
|
|
if (array_key_exists(FunctionCommentsGenerator::class, $this->generatedCode)) { |
78
|
|
|
$code = $this->generatedCode[FunctionCommentsGenerator::class] . "\n" . $code; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $code; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Build function definition. |
86
|
|
|
* |
87
|
|
|
* @return string Function definition |
88
|
|
|
*/ |
89
|
|
|
protected function buildDefinition() |
90
|
|
|
{ |
91
|
|
|
return 'function ' . $this->name; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* {@inheritdoc} |
96
|
|
|
*/ |
97
|
|
|
public function defComment() : CommentsGenerator |
98
|
|
|
{ |
99
|
|
|
return (new FunctionCommentsGenerator($this->arguments, $this->argumentDescriptions, $this)) |
100
|
|
|
->setIndentation($this->indentation); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|