|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace WsdlToPhp\PhpGenerator\Element; |
|
6
|
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
|
8
|
|
|
|
|
9
|
|
|
class PhpDeclare extends AbstractElement |
|
10
|
|
|
{ |
|
11
|
|
|
const STATEMENT = 'declare(%s);'; |
|
12
|
|
|
|
|
13
|
|
|
const DIRECTIVE_ENCODING = 'encoding'; |
|
14
|
|
|
|
|
15
|
|
|
const DIRECTIVE_STRICT_TYPES = 'strict_types'; |
|
16
|
|
|
|
|
17
|
|
|
const DIRECTIVE_TICKS = 'ticks'; |
|
18
|
|
|
|
|
19
|
|
|
const ALLOWED_DIRECTIVES = [ |
|
20
|
|
|
self::DIRECTIVE_ENCODING, |
|
21
|
|
|
self::DIRECTIVE_STRICT_TYPES, |
|
22
|
|
|
self::DIRECTIVE_TICKS, |
|
23
|
|
|
]; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var string|int |
|
27
|
|
|
*/ |
|
28
|
|
|
private $value; |
|
29
|
|
|
|
|
30
|
28 |
|
public function __construct(string $name, $value = null) |
|
31
|
|
|
{ |
|
32
|
28 |
|
parent::__construct($name); |
|
33
|
26 |
|
$this->setValue($value); |
|
34
|
24 |
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param int|string $value |
|
38
|
|
|
* @return PhpDeclare |
|
39
|
|
|
*/ |
|
40
|
26 |
|
public function setValue($value): self |
|
41
|
|
|
{ |
|
42
|
26 |
|
if (!is_null($value) && !is_scalar($value)) { |
|
|
|
|
|
|
43
|
2 |
|
throw new InvalidArgumentException(sprintf('Value must be a scalar value, %s given', var_export($value, true))); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
24 |
|
$this->value = $value; |
|
47
|
|
|
|
|
48
|
24 |
|
return $this; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @return int|string |
|
53
|
|
|
*/ |
|
54
|
16 |
|
public function getValue() |
|
55
|
|
|
{ |
|
56
|
16 |
|
return $this->value; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
28 |
|
public static function nameIsValid(string $name, bool $allowBackslash = false): bool |
|
60
|
|
|
{ |
|
61
|
28 |
|
return parent::nameIsValid($name, $allowBackslash) && in_array($name, self::ALLOWED_DIRECTIVES, true); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
16 |
|
public function getPhpDeclaration(): string |
|
65
|
|
|
{ |
|
66
|
16 |
|
$directives = array_merge([$this], $this->getChildren()); |
|
67
|
|
|
|
|
68
|
16 |
|
$strings = []; |
|
69
|
|
|
/** @var PhpDeclare $directive */ |
|
70
|
16 |
|
foreach ($directives as $directive) { |
|
71
|
16 |
|
if (is_null($directive->getValue())) { |
|
72
|
2 |
|
continue; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
14 |
|
$strings[] = sprintf('%s=%s', $directive->getName(), var_export($directive->getValue(), true)); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
16 |
|
return empty($strings) ? '' : sprintf(self::STATEMENT, implode(', ', $strings)); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Children are handled before in getPhpDeclaration in order to have one line per declare directive |
|
83
|
|
|
* @param $child |
|
84
|
|
|
* @param int|null $indentation |
|
85
|
|
|
* @return string |
|
86
|
|
|
*/ |
|
87
|
4 |
|
protected function getChildContent($child, int $indentation = null): string |
|
88
|
|
|
{ |
|
89
|
4 |
|
return ''; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
14 |
|
public function addChild($child): AbstractElement |
|
93
|
|
|
{ |
|
94
|
|
|
/** @var AbstractElement $child */ |
|
95
|
14 |
|
if (static::childIsValid($child) && $child->getName() === $this->getName()) { |
|
|
|
|
|
|
96
|
2 |
|
throw new InvalidArgumentException(sprintf('The current directive named %s can\'t contain a child of same directive name', $this->getName())); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
12 |
|
return parent::addChild($child); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
14 |
|
public function getChildrenTypes(): array |
|
103
|
|
|
{ |
|
104
|
|
|
return [ |
|
105
|
14 |
|
PhpDeclare::class, |
|
106
|
|
|
]; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|