1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace WsdlToPhp\PhpGenerator\Element; |
6
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
|
9
|
|
|
trait AssignedValueElementTrait |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var mixed |
13
|
|
|
*/ |
14
|
|
|
protected $value; |
15
|
|
|
|
16
|
198 |
|
public function setValue($value): AbstractElement |
17
|
|
|
{ |
18
|
198 |
|
if (!$this->getAcceptNonScalarValue() && !is_scalar($value) && !is_null($value)) { |
19
|
4 |
|
throw new InvalidArgumentException(sprintf('Value of type "%s" is not a valid scalar value for %s object', gettype($value), $this->getCalledClass())); |
20
|
|
|
} |
21
|
194 |
|
$this->value = $value; |
22
|
|
|
|
23
|
194 |
|
return $this; |
|
|
|
|
24
|
|
|
} |
25
|
|
|
|
26
|
164 |
|
public function getValue() |
27
|
|
|
{ |
28
|
164 |
|
return $this->value; |
29
|
|
|
} |
30
|
|
|
|
31
|
164 |
|
public function hasValue(): bool |
32
|
|
|
{ |
33
|
164 |
|
return AssignedValueElementInterface::NO_VALUE !== $this->getValue(); |
34
|
|
|
} |
35
|
|
|
|
36
|
164 |
|
public function getPhpValue(): ?string |
37
|
|
|
{ |
38
|
164 |
|
if (!$this->hasValue()) { |
39
|
68 |
|
return ''; |
40
|
|
|
} |
41
|
|
|
|
42
|
138 |
|
return $this->getFinalValue(); |
43
|
|
|
} |
44
|
|
|
|
45
|
136 |
|
public function getPhpDeclaration(): string |
46
|
|
|
{ |
47
|
136 |
|
return implode('', [ |
48
|
136 |
|
$this->getAssignmentDeclarator(), |
49
|
136 |
|
$this->getPhpName(), |
50
|
136 |
|
$this->getAssignmentSign(), |
51
|
136 |
|
$this->getPhpValue(), |
52
|
136 |
|
$this->getAssignmentFinishing(), |
53
|
136 |
|
$this->endsWithSemicolon() ? ';' : '', |
54
|
|
|
]); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
abstract public function endsWithSemicolon(): bool; |
58
|
|
|
|
59
|
|
|
abstract public function getAssignmentDeclarator(): string; |
60
|
|
|
|
61
|
|
|
abstract public function getAssignmentSign(): string; |
62
|
|
|
|
63
|
|
|
abstract public function getAssignmentFinishing(): string; |
64
|
|
|
|
65
|
|
|
abstract public function getAcceptNonScalarValue(): bool; |
66
|
|
|
|
67
|
|
|
abstract public function getCalledClass(): string; |
68
|
|
|
|
69
|
|
|
abstract public function getPhpName(): string; |
70
|
|
|
|
71
|
138 |
|
protected function getFinalValue(): ?string |
72
|
|
|
{ |
73
|
138 |
|
if (is_scalar($this->getValue()) && !is_null($scalarValue = $this->getScalarValue($this->getValue()))) { |
74
|
12 |
|
return $scalarValue; |
75
|
|
|
} |
76
|
126 |
|
if (is_null($this->getValue())) { |
77
|
66 |
|
return 'null'; |
78
|
|
|
} |
79
|
|
|
|
80
|
100 |
|
return $this->getAnyValue($this->getValue()); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param mixed $value |
85
|
|
|
* |
86
|
|
|
* @return mixed |
87
|
|
|
*/ |
88
|
84 |
|
protected function getScalarValue($value) |
89
|
|
|
{ |
90
|
84 |
|
$scalarValue = null; |
91
|
84 |
|
if (0 === stripos((string) $value, '::')) { |
92
|
2 |
|
$scalarValue = substr($value, 2); |
93
|
82 |
|
} elseif (false !== stripos((string) $value, '::') || false !== stripos((string) $value, 'new ') || false !== stripos((string) $value, '(') || false !== stripos((string) $value, ')')) { |
94
|
10 |
|
$scalarValue = $value; |
95
|
|
|
} |
96
|
|
|
|
97
|
84 |
|
return $scalarValue; |
98
|
|
|
} |
99
|
|
|
|
100
|
98 |
|
protected function getAnyValue($value): string |
101
|
|
|
{ |
102
|
98 |
|
$exportedValue = var_export($value, true); |
103
|
|
|
// work around for known bug https://bugs.php.net/bug.php?id=66866 |
104
|
98 |
|
if (is_float($value) && strlen((string) $value) !== strlen((string) $exportedValue)) { |
105
|
2 |
|
$exportedValue = (string) $value; |
106
|
|
|
} |
107
|
|
|
|
108
|
98 |
|
return $exportedValue; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|