1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace WsdlToPhp\PackageGenerator\Model; |
||
6 | |||
7 | use WsdlToPhp\PackageGenerator\ConfigurationReader\ServiceReservedMethod; |
||
8 | use WsdlToPhp\PackageGenerator\Generator\Generator; |
||
9 | |||
10 | /** |
||
11 | * Class Method stands for an available operation described in the WSDL. |
||
12 | */ |
||
13 | class Method extends AbstractModel |
||
14 | { |
||
15 | protected $parameterType; |
||
16 | |||
17 | protected $returnType; |
||
18 | |||
19 | protected bool $isUnique = true; |
||
20 | |||
21 | protected ?string $methodName = null; |
||
22 | |||
23 | 364 | public function __construct(Generator $generator, string $name, $parameterType = null, $returnType = null, ?Service $service = null, bool $isUnique = true) |
|
24 | { |
||
25 | 364 | parent::__construct($generator, $name); |
|
26 | 364 | $this |
|
27 | 364 | ->setParameterType($parameterType) |
|
28 | 364 | ->setReturnType($returnType) |
|
29 | 364 | ->setUnique($isUnique) |
|
30 | 364 | ->setOwner($service) |
|
31 | 364 | ; |
|
32 | } |
||
33 | |||
34 | /** |
||
35 | * Method name can't starts with numbers. |
||
36 | */ |
||
37 | 366 | public function getCleanName(bool $keepMultipleUnderscores = true): string |
|
38 | { |
||
39 | 366 | return preg_replace('/^(\d+)([a-zA-Z0-9]*)$/', '_$2', parent::getCleanName($keepMultipleUnderscores)); |
|
40 | } |
||
41 | |||
42 | /** |
||
43 | * Returns the name of the method that is used to call the operation |
||
44 | * It takes care of the fact that the method might not be the only one named as it is. |
||
45 | */ |
||
46 | 366 | public function getMethodName(): string |
|
47 | { |
||
48 | 366 | if (empty($this->methodName)) { |
|
49 | 364 | $methodName = $this->getCleanName(); |
|
50 | 364 | if (!$this->isUnique()) { |
|
51 | 8 | if (is_string($this->getParameterType())) { |
|
52 | 8 | $methodName .= ucfirst($this->getParameterType()); |
|
53 | } else { |
||
54 | 2 | $methodName .= '_'.md5(var_export($this->getParameterType(), true)); |
|
55 | } |
||
56 | } |
||
57 | 364 | $context = $this->getOwner()->getPackagedName(); |
|
58 | 364 | $methodName = $this->replaceReservedMethod($methodName, $context); |
|
59 | 364 | $methodName = self::replacePhpReservedKeyword($methodName, $context); |
|
60 | 364 | $this->methodName = self::uniqueName($methodName, $this->getOwner()->getPackagedName()); |
|
61 | } |
||
62 | |||
63 | 366 | return $this->methodName; |
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
64 | } |
||
65 | |||
66 | 98 | public function getParameterType() |
|
67 | { |
||
68 | 98 | return $this->parameterType; |
|
69 | } |
||
70 | |||
71 | 364 | public function setParameterType($parameterType): self |
|
72 | { |
||
73 | 364 | $this->parameterType = $parameterType; |
|
74 | |||
75 | 364 | return $this; |
|
76 | } |
||
77 | |||
78 | 52 | public function getReturnType() |
|
79 | { |
||
80 | 52 | return $this->returnType; |
|
81 | } |
||
82 | |||
83 | 364 | public function setReturnType($returnType): self |
|
84 | { |
||
85 | 364 | $this->returnType = $returnType; |
|
86 | |||
87 | 364 | return $this; |
|
88 | } |
||
89 | |||
90 | 366 | public function isUnique(): bool |
|
91 | { |
||
92 | 366 | return $this->isUnique; |
|
93 | } |
||
94 | |||
95 | 364 | public function setUnique(bool $isUnique = true): self |
|
96 | { |
||
97 | 364 | $this->isUnique = $isUnique; |
|
98 | |||
99 | 364 | return $this; |
|
100 | } |
||
101 | |||
102 | 366 | public function getOwner(): Service |
|
103 | { |
||
104 | 366 | return parent::getOwner(); |
|
0 ignored issues
–
show
|
|||
105 | } |
||
106 | |||
107 | 364 | public function getReservedMethodsInstance(?string $filename = null): ServiceReservedMethod |
|
108 | { |
||
109 | 364 | return ServiceReservedMethod::instance($filename); |
|
0 ignored issues
–
show
|
|||
110 | } |
||
111 | |||
112 | 2 | protected function toJsonSerialize(): array |
|
113 | { |
||
114 | 2 | return [ |
|
115 | 2 | 'unique' => $this->isUnique, |
|
116 | 2 | 'methodName' => $this->methodName, |
|
117 | 2 | 'parameterType' => $this->parameterType, |
|
118 | 2 | 'returnType' => $this->returnType, |
|
119 | 2 | ]; |
|
120 | } |
||
121 | } |
||
122 |