|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace WsdlToPhp\PackageGenerator\Model; |
|
6
|
|
|
|
|
7
|
|
|
use WsdlToPhp\PackageGenerator\Generator\Generator; |
|
8
|
|
|
use WsdlToPhp\PackageGenerator\Generator\Utils; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class StructValue stands for an enumeration value. |
|
12
|
|
|
*/ |
|
13
|
|
|
final class StructValue extends AbstractModel |
|
14
|
|
|
{ |
|
15
|
|
|
public const MATCH_PATTERN = '/([[:upper:]]+[[:lower:]]*)|([[:lower:]]+)|(\d+)/'; |
|
16
|
|
|
public const REPLACEMENT_PATTERN = '$1$2$3_'; |
|
17
|
|
|
public const GENERIC_NAME_PREFIX = 'ENUM_VALUE_'; |
|
18
|
|
|
public const VALUE_NAME_PREFIX = 'VALUE_'; |
|
19
|
|
|
|
|
20
|
|
|
protected static array $uniqueConstants = []; |
|
21
|
|
|
|
|
22
|
|
|
protected int $index = 0; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Cleaned name of the element stored in order to avoid multiple call that would generate incremental name. |
|
26
|
|
|
*/ |
|
27
|
|
|
private ?string $cleanedName = null; |
|
28
|
|
|
|
|
29
|
236 |
|
public function __construct(Generator $generator, $name, int $index = 0, ?Struct $struct = null) |
|
30
|
|
|
{ |
|
31
|
236 |
|
parent::__construct($generator, $name); |
|
32
|
236 |
|
$this |
|
33
|
236 |
|
->setIndex($index) |
|
34
|
236 |
|
->setOwner($struct) |
|
35
|
236 |
|
; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
46 |
|
public function getCleanName(bool $keepMultipleUnderscores = false): string |
|
39
|
|
|
{ |
|
40
|
46 |
|
if ($this->cleanedName) { |
|
41
|
42 |
|
return $this->cleanedName; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
46 |
|
if ($this->getGenerator()->getOptionGenericConstantsNames()) { |
|
45
|
2 |
|
return self::GENERIC_NAME_PREFIX.$this->getIndex(); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
44 |
|
$nameWithSeparatedWords = $this->getNameWithSeparatedWords($keepMultipleUnderscores); |
|
49
|
44 |
|
$key = self::constantSuffix($this->getOwner()->getName(), $nameWithSeparatedWords, $this->getIndex()); |
|
50
|
|
|
|
|
51
|
44 |
|
return $this->cleanedName = self::VALUE_NAME_PREFIX.mb_strtoupper($nameWithSeparatedWords.($key ? '_'.$key : '')); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
44 |
|
public function getNameWithSeparatedWords(bool $keepMultipleUnderscores = false): string |
|
55
|
|
|
{ |
|
56
|
44 |
|
return trim(self::cleanString(preg_replace(self::MATCH_PATTERN, self::REPLACEMENT_PATTERN, (string) $this->getName()), $keepMultipleUnderscores), '_'); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
46 |
|
public function getValue() |
|
60
|
|
|
{ |
|
61
|
46 |
|
return Utils::getValueWithinItsType($this->getName()); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
46 |
|
public function getIndex(): int |
|
65
|
|
|
{ |
|
66
|
46 |
|
return $this->index; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
236 |
|
public function setIndex(int $index): StructValue |
|
70
|
|
|
{ |
|
71
|
236 |
|
if (0 > $index) { |
|
72
|
4 |
|
throw new \InvalidArgumentException(sprintf('The value\'s index must be a positive integer, "%s" given', var_export($index, true))); |
|
73
|
|
|
} |
|
74
|
234 |
|
$this->index = $index; |
|
75
|
|
|
|
|
76
|
234 |
|
return $this; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
44 |
|
public function getOwner(): Struct |
|
80
|
|
|
{ |
|
81
|
44 |
|
return parent::getOwner(); |
|
|
|
|
|
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
44 |
|
protected static function constantSuffix(string $structName, string $value, int $index): int |
|
85
|
|
|
{ |
|
86
|
44 |
|
$key = mb_strtoupper($structName.'_'.$value); |
|
87
|
44 |
|
$indexedKey = $key.'_'.$index; |
|
88
|
44 |
|
if (array_key_exists($indexedKey, self::$uniqueConstants)) { |
|
89
|
18 |
|
return self::$uniqueConstants[$indexedKey]; |
|
90
|
|
|
} |
|
91
|
28 |
|
if (!array_key_exists($key, self::$uniqueConstants)) { |
|
92
|
28 |
|
self::$uniqueConstants[$key] = 0; |
|
93
|
|
|
} else { |
|
94
|
4 |
|
++self::$uniqueConstants[$key]; |
|
95
|
|
|
} |
|
96
|
28 |
|
self::$uniqueConstants[$indexedKey] = self::$uniqueConstants[$key]; |
|
97
|
|
|
|
|
98
|
28 |
|
return self::$uniqueConstants[$key]; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
2 |
|
protected function toJsonSerialize(): array |
|
102
|
|
|
{ |
|
103
|
2 |
|
return [ |
|
104
|
2 |
|
'index' => $this->index, |
|
105
|
2 |
|
]; |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|