1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace WsdlToPhp\PhpGenerator\Component; |
6
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use WsdlToPhp\PhpGenerator\Element\AbstractElement; |
9
|
|
|
use WsdlToPhp\PhpGenerator\Element\PhpFile as PhpFileElement; |
10
|
|
|
use WsdlToPhp\PhpGenerator\Element\PhpClass as PhpClassElement; |
11
|
|
|
use WsdlToPhp\PhpGenerator\Element\PhpConstant as PhpConstantElement; |
12
|
|
|
use WsdlToPhp\PhpGenerator\Element\PhpAnnotationBlock as PhpAnnotationBlockElement; |
13
|
|
|
|
14
|
|
|
abstract class AbstractComponent implements GenerateableInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var PhpFileElement|PhpClassElement |
18
|
|
|
*/ |
19
|
|
|
protected $mainElement; |
20
|
|
|
|
21
|
|
|
public function __toString(): string |
22
|
|
|
{ |
23
|
|
|
return $this->toString(); |
24
|
|
|
} |
25
|
|
|
|
26
|
20 |
|
public function toString(): string |
27
|
|
|
{ |
28
|
20 |
|
$content = []; |
29
|
20 |
|
foreach ($this->getElements() as $element) { |
30
|
20 |
|
$content[] = $this->getElementString($element); |
31
|
|
|
} |
32
|
20 |
|
return implode('', $content); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @return AbstractElement[]|string[] |
37
|
|
|
*/ |
38
|
|
|
abstract public function getElements(): array; |
39
|
|
|
|
40
|
22 |
|
public function setMainElement(AbstractElement $element): self |
41
|
|
|
{ |
42
|
22 |
|
if ($element instanceof PhpFileElement || $element instanceof PhpClassElement) { |
43
|
22 |
|
$this->mainElement = $element; |
44
|
|
|
} else { |
45
|
2 |
|
throw new InvalidArgumentException(sprintf('Element of type "%s" must be of type Element\PhpClass or Element\PhpFile', get_class($element))); |
46
|
|
|
} |
47
|
22 |
|
return $this; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return PhpFileElement|PhpClassElement |
52
|
|
|
*/ |
53
|
20 |
|
public function getMainElement(): AbstractElement |
54
|
|
|
{ |
55
|
20 |
|
return $this->mainElement; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @throws InvalidArgumentException |
60
|
|
|
* @param string|AbstractElement $element |
61
|
|
|
* @return string |
62
|
|
|
*/ |
63
|
20 |
|
protected function getElementString($element): string |
64
|
|
|
{ |
65
|
20 |
|
$string = ''; |
66
|
20 |
|
if (is_scalar($element)) { |
67
|
|
|
$string = $element; |
68
|
20 |
|
} elseif ($element instanceof AbstractElement) { |
|
|
|
|
69
|
20 |
|
$string = $element->toString(); |
70
|
|
|
} |
71
|
20 |
|
return $string; |
72
|
|
|
} |
73
|
|
|
|
74
|
16 |
|
public function addConstantElement(PhpConstantElement $constant): self |
75
|
|
|
{ |
76
|
16 |
|
if (!$constant->getClass() instanceof PhpClassElement && $this->mainElement instanceof PhpClassElement) { |
77
|
16 |
|
$constant->setClass($this->mainElement); |
78
|
|
|
} |
79
|
16 |
|
$this->mainElement->addChild($constant); |
80
|
16 |
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param string $name |
85
|
|
|
* @param mixed $value |
86
|
|
|
* @param PhpClassElement|null $class |
87
|
|
|
* @return AbstractComponent |
88
|
|
|
*/ |
89
|
16 |
|
public function addConstant(string $name, $value = null, ?PhpClassElement $class = null): self |
90
|
|
|
{ |
91
|
16 |
|
return $this->addConstantElement(new PhpConstantElement($name, $value, $class)); |
92
|
|
|
} |
93
|
|
|
|
94
|
16 |
|
public function addAnnotationBlockElement(PhpAnnotationBlockElement $annotationBlock): self |
95
|
|
|
{ |
96
|
16 |
|
$this->mainElement->addChild($annotationBlock); |
97
|
16 |
|
return $this; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param array|string|PhpAnnotationElement $annotations |
|
|
|
|
102
|
|
|
* @return AbstractComponent |
103
|
|
|
*/ |
104
|
16 |
|
public function addAnnotationBlock($annotations): self |
105
|
|
|
{ |
106
|
16 |
|
return $this->addAnnotationBlockElement(new PhpAnnotationBlockElement(is_array($annotations) ? $annotations : [ |
107
|
16 |
|
$annotations, |
108
|
|
|
])); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|