1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace WsdlToPhp\PhpGenerator\Element; |
6
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
|
9
|
|
|
class PhpFunction extends AbstractElement |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var PhpFunctionParameter[]|string[] |
13
|
|
|
*/ |
14
|
|
|
protected array $parameters; |
15
|
|
|
|
16
|
|
|
protected ?string $returnType; |
17
|
|
|
|
18
|
104 |
|
public function __construct(string $name, array $parameters = [], ?string $returnType = null) |
19
|
|
|
{ |
20
|
104 |
|
parent::__construct($name); |
21
|
|
|
$this |
22
|
104 |
|
->setParameters($parameters) |
23
|
104 |
|
->setReturnType($returnType) |
24
|
|
|
; |
25
|
104 |
|
} |
26
|
|
|
|
27
|
104 |
|
public function setParameters(array $parameters): self |
28
|
|
|
{ |
29
|
104 |
|
if (!static::parametersAreValid($parameters)) { |
30
|
2 |
|
throw new InvalidArgumentException('Parameters are invalid'); |
31
|
|
|
} |
32
|
|
|
|
33
|
104 |
|
$this->parameters = static::transformParameters($parameters); |
34
|
|
|
|
35
|
104 |
|
return $this; |
36
|
|
|
} |
37
|
|
|
|
38
|
104 |
|
public static function transformParameters(array $parameters): array |
39
|
|
|
{ |
40
|
104 |
|
$finalParameters = []; |
41
|
104 |
|
foreach ($parameters as $parameter) { |
42
|
60 |
|
$finalParameters[] = static::transformParameter($parameter); |
43
|
|
|
} |
44
|
|
|
|
45
|
104 |
|
return $finalParameters; |
46
|
|
|
} |
47
|
|
|
|
48
|
60 |
|
public static function transformParameter($parameter): PhpFunctionParameter |
49
|
|
|
{ |
50
|
60 |
|
if ($parameter instanceof PhpFunctionParameter) { |
51
|
40 |
|
return $parameter; |
52
|
|
|
} |
53
|
60 |
|
if (is_array($parameter)) { |
54
|
24 |
|
return new PhpFunctionParameter($parameter['name'], array_key_exists('value', $parameter) ? $parameter['value'] : null, array_key_exists('type', $parameter) ? $parameter['type'] : null); |
55
|
|
|
} |
56
|
|
|
|
57
|
60 |
|
return new PhpFunctionParameter($parameter, PhpFunctionParameter::NO_VALUE); |
58
|
|
|
} |
59
|
|
|
|
60
|
104 |
|
public static function parametersAreValid(array $parameters): bool |
61
|
|
|
{ |
62
|
104 |
|
$valid = true; |
63
|
104 |
|
foreach ($parameters as $parameter) { |
64
|
62 |
|
$valid &= static::parameterIsValid($parameter); |
65
|
|
|
} |
66
|
|
|
|
67
|
104 |
|
return (bool) $valid; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param array|PhpFunctionParameter|string $parameter |
72
|
|
|
*/ |
73
|
62 |
|
public static function parameterIsValid($parameter): bool |
74
|
|
|
{ |
75
|
62 |
|
return static::stringIsValid($parameter) || (is_array($parameter) && array_key_exists('name', $parameter)) || $parameter instanceof PhpFunctionParameter; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return PhpFunctionParameter[]|string[] |
80
|
|
|
*/ |
81
|
70 |
|
public function getParameters(): array |
82
|
|
|
{ |
83
|
70 |
|
return $this->parameters; |
84
|
|
|
} |
85
|
|
|
|
86
|
104 |
|
public function setReturnType(?string $returnType): self |
87
|
|
|
{ |
88
|
104 |
|
$this->returnType = $returnType; |
89
|
|
|
|
90
|
104 |
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
56 |
|
public function getReturnType(): ?string |
94
|
|
|
{ |
95
|
56 |
|
return $this->returnType; |
96
|
|
|
} |
97
|
|
|
|
98
|
16 |
|
public function getPhpDeclaration(): string |
99
|
|
|
{ |
100
|
16 |
|
return sprintf( |
101
|
16 |
|
'function %s(%s)%s', |
102
|
16 |
|
$this->getPhpName(), |
103
|
16 |
|
$this->getPhpParameters(), |
104
|
16 |
|
$this->returnType ? sprintf(': %s', $this->returnType) : '' |
105
|
|
|
); |
106
|
|
|
} |
107
|
|
|
|
108
|
22 |
|
public function getChildrenTypes(): array |
109
|
|
|
{ |
110
|
|
|
return [ |
111
|
22 |
|
'string', |
112
|
|
|
PhpAnnotationBlock::class, |
113
|
|
|
PhpVariable::class, |
114
|
|
|
]; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Allows to indicate that children are contained by brackets, |
119
|
|
|
* in the case the method returns true, getBracketBeforeChildren |
120
|
|
|
* is called instead of getLineBeforeChildren and getBracketAfterChildren |
121
|
|
|
* is called instead of getLineAfterChildren, but be aware that these methods |
122
|
|
|
* call the two others. |
123
|
|
|
*/ |
124
|
12 |
|
public function useBracketsForChildren(): bool |
125
|
|
|
{ |
126
|
12 |
|
return true; |
127
|
|
|
} |
128
|
|
|
|
129
|
70 |
|
protected function getPhpParameters(): string |
130
|
|
|
{ |
131
|
70 |
|
$parameters = $this->getParameters(); |
132
|
70 |
|
$phpParameters = []; |
133
|
70 |
|
if (is_array($parameters) && !empty($parameters)) { |
134
|
60 |
|
foreach ($parameters as $parameter) { |
135
|
60 |
|
$phpParameters[] = $parameter->getPhpDeclaration(); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
70 |
|
return implode(', ', $phpParameters); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|