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\PhpAnnotationBlock as PhpAnnotationBlockElement; |
10
|
|
|
use WsdlToPhp\PhpGenerator\Element\PhpClass as PhpClassElement; |
11
|
|
|
use WsdlToPhp\PhpGenerator\Element\PhpConstant as PhpConstantElement; |
12
|
|
|
use WsdlToPhp\PhpGenerator\Element\PhpFile as PhpFileElement; |
13
|
|
|
|
14
|
|
|
abstract class AbstractComponent implements GenerateableInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var PhpClassElement|PhpFileElement |
18
|
|
|
*/ |
19
|
|
|
protected $mainElement; |
20
|
|
|
|
21
|
16 |
|
public function __toString(): string |
22
|
|
|
{ |
23
|
16 |
|
return $this->toString(); |
24
|
|
|
} |
25
|
|
|
|
26
|
20 |
|
public function toString(): string |
27
|
|
|
{ |
28
|
20 |
|
return (string) $this->mainElement; |
29
|
|
|
} |
30
|
|
|
|
31
|
22 |
|
public function setMainElement(AbstractElement $element): self |
32
|
|
|
{ |
33
|
22 |
|
if ($element instanceof PhpFileElement || $element instanceof PhpClassElement) { |
34
|
22 |
|
$this->mainElement = $element; |
35
|
|
|
} else { |
36
|
2 |
|
throw new InvalidArgumentException(sprintf('Element of type "%s" must be of type Element\PhpClass or Element\PhpFile', get_class($element))); |
37
|
|
|
} |
38
|
|
|
|
39
|
22 |
|
return $this; |
40
|
|
|
} |
41
|
|
|
|
42
|
16 |
|
public function addConstantElement(PhpConstantElement $constant): self |
43
|
|
|
{ |
44
|
16 |
|
if (!$constant->getClass() instanceof PhpClassElement && $this->mainElement instanceof PhpClassElement) { |
45
|
16 |
|
$constant->setClass($this->mainElement); |
46
|
|
|
} |
47
|
16 |
|
$this->mainElement->addChild($constant); |
48
|
|
|
|
49
|
16 |
|
return $this; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param mixed $value |
54
|
|
|
*/ |
55
|
16 |
|
public function addConstant(string $name, $value = null, ?PhpClassElement $class = null): self |
56
|
|
|
{ |
57
|
16 |
|
return $this->addConstantElement(new PhpConstantElement($name, $value, $class)); |
58
|
|
|
} |
59
|
|
|
|
60
|
16 |
|
public function addAnnotationBlockElement(PhpAnnotationBlockElement $annotationBlock): self |
61
|
|
|
{ |
62
|
16 |
|
$this->mainElement->addChild($annotationBlock); |
63
|
|
|
|
64
|
16 |
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param array|PhpAnnotationBlockElement|string $annotations |
69
|
|
|
*/ |
70
|
16 |
|
public function addAnnotationBlock($annotations): self |
71
|
|
|
{ |
72
|
16 |
|
return $this->addAnnotationBlockElement(new PhpAnnotationBlockElement(is_array($annotations) ? $annotations : [ |
73
|
16 |
|
$annotations, |
74
|
|
|
])); |
75
|
|
|
} |
76
|
|
|
|
77
|
2 |
|
public function addString(string $string = ''): self |
78
|
|
|
{ |
79
|
2 |
|
$this->mainElement->addChild($string); |
80
|
|
|
|
81
|
2 |
|
return $this; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|