1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace WsdlToPhp\PhpGenerator\Component; |
6
|
|
|
|
7
|
|
|
use WsdlToPhp\PhpGenerator\Element\PhpDeclare; |
8
|
|
|
use WsdlToPhp\PhpGenerator\Element\PhpFile as PhpFileElement; |
9
|
|
|
use WsdlToPhp\PhpGenerator\Element\PhpVariable as PhpVariableElement; |
10
|
|
|
use WsdlToPhp\PhpGenerator\Element\PhpFunction as PhpFunctionElement; |
11
|
|
|
use WsdlToPhp\PhpGenerator\Component\PhpClass as PhpClassComponent; |
12
|
|
|
use WsdlToPhp\PhpGenerator\Component\PhpInterface as PhpInterfaceComponent; |
13
|
|
|
|
14
|
|
|
class PhpFile extends AbstractComponent |
15
|
|
|
{ |
16
|
12 |
|
public function __construct(string $name) |
17
|
|
|
{ |
18
|
12 |
|
$this->setMainElement(new PhpFileElement($name)); |
19
|
12 |
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @return PhpFileElement[] |
23
|
|
|
*/ |
24
|
10 |
|
public function getElements(): array |
25
|
|
|
{ |
26
|
|
|
return [ |
|
|
|
|
27
|
10 |
|
$this->getMainElement(), |
28
|
|
|
]; |
29
|
|
|
} |
30
|
|
|
|
31
|
4 |
|
public function addClassComponent(PhpClassComponent $class): self |
32
|
|
|
{ |
33
|
4 |
|
$this->mainElement->addChild($class->toString()); |
34
|
|
|
|
35
|
4 |
|
return $this; |
36
|
|
|
} |
37
|
|
|
|
38
|
2 |
|
public function addInterfaceComponent(PhpInterfaceComponent $interface): self |
39
|
|
|
{ |
40
|
2 |
|
$this->mainElement->addChild($interface->toString()); |
41
|
|
|
|
42
|
2 |
|
return $this; |
43
|
|
|
} |
44
|
|
|
|
45
|
2 |
|
public function addVariableElement(PhpVariableElement $variable): self |
46
|
|
|
{ |
47
|
2 |
|
$this->mainElement->addChild($variable); |
48
|
|
|
|
49
|
2 |
|
return $this; |
50
|
|
|
} |
51
|
|
|
|
52
|
2 |
|
public function addVariable(string $name, $value = null): self |
53
|
|
|
{ |
54
|
2 |
|
return $this->addVariableElement(new PhpVariableElement($name, $value)); |
55
|
|
|
} |
56
|
|
|
|
57
|
2 |
|
public function addFunctionElement(PhpFunctionElement $function): self |
58
|
|
|
{ |
59
|
2 |
|
$this->mainElement->addChild($function); |
60
|
|
|
|
61
|
2 |
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
2 |
|
public function addFunction(string $name, array $parameters = []): self |
65
|
|
|
{ |
66
|
2 |
|
return $this->addFunctionElement(new PhpFunctionElement($name, $parameters)); |
67
|
|
|
} |
68
|
|
|
|
69
|
6 |
|
public function addUse(string $use, string $as = null, bool $last = false): self |
70
|
|
|
{ |
71
|
6 |
|
$expression = empty($as) ? "use %1\$s;%3\$s" : "use %1\$s as %2\$s;%3\$s"; |
72
|
6 |
|
$this->mainElement->addChild(sprintf($expression, $use, $as, $last ? self::BREAK_LINE_CHAR : '')); |
73
|
|
|
|
74
|
6 |
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
6 |
|
public function setDeclareElement(PhpDeclare $phpDeclare): self |
78
|
|
|
{ |
79
|
6 |
|
$this->mainElement->addChild(sprintf('%s%s', self::BREAK_LINE_CHAR, $phpDeclare->toString())); |
80
|
|
|
|
81
|
6 |
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
2 |
|
public function setDeclare(string $name, $value): self |
85
|
|
|
{ |
86
|
2 |
|
return $this->setDeclareElement(new PhpDeclare($name, $value)); |
87
|
|
|
} |
88
|
|
|
|
89
|
6 |
|
public function setNamespace(string $namespace): self |
90
|
|
|
{ |
91
|
6 |
|
$this->mainElement->addChild( |
92
|
3 |
|
sprintf( |
93
|
6 |
|
"%snamespace %s;%s", |
94
|
6 |
|
self::BREAK_LINE_CHAR, |
95
|
|
|
$namespace, |
96
|
6 |
|
self::BREAK_LINE_CHAR |
97
|
|
|
) |
98
|
|
|
); |
99
|
6 |
|
return $this; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|