1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace WsdlToPhp\PhpGenerator\Element; |
6
|
|
|
|
7
|
|
|
abstract class AbstractAssignedValueElement extends AbstractAccessRestrictedElement |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Use this constant as value to ensure element has not assigned value |
11
|
|
|
* @var unknown |
12
|
|
|
*/ |
13
|
|
|
const NO_VALUE = '##NO_VALUE##'; |
14
|
|
|
/** |
15
|
|
|
* @var mixed |
16
|
|
|
*/ |
17
|
|
|
protected $value; |
18
|
|
|
/** |
19
|
|
|
* @param string $name |
20
|
|
|
* @param mixed $value |
21
|
|
|
* @param string $access |
22
|
|
|
*/ |
23
|
162 |
|
public function __construct(string $name, $value = null, string $access = parent::ACCESS_PUBLIC) |
24
|
|
|
{ |
25
|
162 |
|
parent::__construct($name, $access); |
26
|
162 |
|
$this->setValue($value); |
27
|
158 |
|
} |
28
|
|
|
/** |
29
|
|
|
* @throws \InvalidArgumentException |
30
|
|
|
* @param mixed $value |
31
|
|
|
* @return AbstractAssignedValueElement |
32
|
|
|
*/ |
33
|
162 |
|
public function setValue($value): AbstractAssignedValueElement |
34
|
|
|
{ |
35
|
162 |
|
if ($this->getAcceptNonScalarValue() === false && !is_scalar($value) && $value !== null) { |
36
|
4 |
|
throw new \InvalidArgumentException(sprintf('Value of type "%s" is not a valid scalar value for %s object', gettype($value), $this->getCalledClass())); |
37
|
|
|
} |
38
|
158 |
|
$this->value = $value; |
39
|
158 |
|
return $this; |
40
|
|
|
} |
41
|
|
|
/** |
42
|
|
|
* @return mixed |
43
|
|
|
*/ |
44
|
128 |
|
public function getValue() |
45
|
|
|
{ |
46
|
128 |
|
return $this->value; |
47
|
|
|
} |
48
|
|
|
/** |
49
|
|
|
* @return bool |
50
|
|
|
*/ |
51
|
128 |
|
public function hasValue(): bool |
52
|
|
|
{ |
53
|
128 |
|
return $this->getValue() !== self::NO_VALUE; |
54
|
|
|
} |
55
|
|
|
/** |
56
|
|
|
* @return mixed |
57
|
|
|
*/ |
58
|
128 |
|
public function getPhpValue() |
59
|
|
|
{ |
60
|
128 |
|
if (!$this->hasValue()) { |
61
|
40 |
|
return ''; |
62
|
|
|
} |
63
|
110 |
|
return $this->getFinalValue(); |
64
|
|
|
} |
65
|
|
|
/** |
66
|
|
|
* @return mixed |
67
|
|
|
*/ |
68
|
110 |
|
protected function getFinalValue() |
69
|
|
|
{ |
70
|
110 |
|
if (is_scalar($this->getValue()) && ($scalarValue = $this->getScalarValue($this->getValue())) !== null) { |
71
|
12 |
|
return $scalarValue; |
72
|
98 |
|
} elseif (is_null($this->getValue())) { |
73
|
40 |
|
return 'null'; |
74
|
|
|
} |
75
|
78 |
|
return $this->getAnyValue($this->getValue()); |
76
|
|
|
} |
77
|
|
|
/** |
78
|
|
|
* @param mixed $value |
79
|
|
|
* @return mixed |
80
|
|
|
*/ |
81
|
62 |
|
protected function getScalarValue($value) |
82
|
|
|
{ |
83
|
62 |
|
$scalarValue = null; |
84
|
62 |
|
if (stripos((string) $value, '::') === 0) { |
85
|
2 |
|
$scalarValue = substr($value, 2); |
86
|
60 |
|
} elseif (stripos((string) $value, '::') !== false || stripos((string) $value, 'new ') !== false || stripos((string) $value, '(') !== false || stripos((string) $value, ')') !== false) { |
87
|
10 |
|
$scalarValue = $value; |
88
|
|
|
} |
89
|
62 |
|
return $scalarValue; |
90
|
|
|
} |
91
|
|
|
/** |
92
|
|
|
* @param mixed $value |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
76 |
|
protected function getAnyValue($value): string |
96
|
|
|
{ |
97
|
76 |
|
$exportedValue = var_export($value, true); |
98
|
|
|
// work around for known bug https://bugs.php.net/bug.php?id=66866 |
99
|
76 |
|
if (is_float($value) && strlen((string) $value) !== strlen((string) $exportedValue)) { |
100
|
|
|
$exportedValue = substr($exportedValue, 0, strlen($value)); |
101
|
|
|
} |
102
|
76 |
|
return $exportedValue; |
103
|
|
|
} |
104
|
|
|
/** |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
128 |
|
public function getPhpDeclaration(): string |
108
|
|
|
{ |
109
|
128 |
|
return sprintf('%s%s%s%s%s%s%s', $this->getPhpAccess(), $this->getAssignmentDeclarator(), $this->getPhpName(), $this->getAssignmentSign(), $this->getPhpValue(), $this->getAssignmentFinishing(), $this->endsWithSemicolon() === true ? ';' : ''); |
110
|
|
|
} |
111
|
|
|
/** |
112
|
|
|
* returns the way the assignment is declared |
113
|
|
|
* @return string |
114
|
|
|
*/ |
115
|
|
|
abstract public function getAssignmentDeclarator(): string; |
116
|
|
|
/** |
117
|
|
|
* returns the way the value is assigned to the element |
118
|
|
|
* @returns string |
119
|
|
|
*/ |
120
|
|
|
abstract public function getAssignmentSign(): string; |
121
|
|
|
/** |
122
|
|
|
* returns the way the assignment is finished |
123
|
|
|
* @return string |
124
|
|
|
*/ |
125
|
|
|
abstract public function getAssignmentFinishing(): string; |
126
|
|
|
/** |
127
|
|
|
* indicates if the element accepts non scalar value |
128
|
|
|
* @return bool |
129
|
|
|
*/ |
130
|
|
|
abstract public function getAcceptNonScalarValue(): bool; |
131
|
|
|
/** |
132
|
|
|
* indicates if the element finishes with a semicolon or not |
133
|
|
|
* @return bool |
134
|
|
|
*/ |
135
|
|
|
abstract public function endsWithSemicolon(): bool; |
136
|
|
|
} |
137
|
|
|
|