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 | 200 | public function setValue($value): AbstractElement |
|
17 | { |
||
18 | 200 | 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 | 196 | $this->value = $value; |
|
22 | |||
23 | 196 | return $this; |
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
24 | } |
||
25 | |||
26 | 166 | public function getValue() |
|
27 | { |
||
28 | 166 | return $this->value; |
|
29 | } |
||
30 | |||
31 | 166 | public function hasValue(): bool |
|
32 | { |
||
33 | 166 | return AssignedValueElementInterface::NO_VALUE !== $this->getValue(); |
|
34 | } |
||
35 | |||
36 | 166 | public function getPhpValue(): ?string |
|
37 | { |
||
38 | 166 | if (!$this->hasValue()) { |
|
39 | 68 | return ''; |
|
40 | } |
||
41 | |||
42 | 140 | return $this->getFinalValue(); |
|
43 | } |
||
44 | |||
45 | 138 | public function getPhpDeclaration(): string |
|
46 | { |
||
47 | 138 | return implode('', [ |
|
48 | 138 | $this->getAssignmentDeclarator(), |
|
49 | 138 | $this->getPhpName(), |
|
50 | 138 | $this->getAssignmentSign(), |
|
51 | 138 | $this->getPhpValue(), |
|
52 | 138 | $this->getAssignmentFinishing(), |
|
53 | 138 | $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 | 140 | protected function getFinalValue(): ?string |
|
72 | { |
||
73 | 140 | if (is_scalar($this->getValue()) && !is_null($scalarValue = $this->getScalarValue($this->getValue()))) { |
|
74 | 12 | return $scalarValue; |
|
75 | } |
||
76 | 128 | if (is_null($this->getValue())) { |
|
77 | 66 | return 'null'; |
|
78 | } |
||
79 | |||
80 | 102 | 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 | 102 | protected function getAnyValue($value): string |
|
101 | { |
||
102 | 102 | if (is_array($value)) { |
|
103 | 8 | $exportedValue = empty($value) ? '[]' : implode("\n", array_map(function ($line) { |
|
104 | 4 | return 'array (' === $line ? '[' : (')' === $line ? ']' : $line); |
|
105 | 6 | }, explode("\n", var_export($value, true)))); |
|
106 | } else { |
||
107 | 94 | $exportedValue = var_export($value, true); |
|
108 | } |
||
109 | |||
110 | // work around for known bug https://bugs.php.net/bug.php?id=66866 |
||
111 | 102 | if (is_float($value) && strlen((string) $value) !== strlen((string) $exportedValue)) { |
|
112 | 2 | $exportedValue = (string) $value; |
|
113 | } |
||
114 | |||
115 | 102 | return $exportedValue; |
|
116 | } |
||
117 | } |
||
118 |