|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace WsdlToPhp\PackageGenerator\Model; |
|
4
|
|
|
|
|
5
|
|
|
use WsdlToPhp\PackageGenerator\Generator\Generator; |
|
6
|
|
|
use WsdlToPhp\PackageGenerator\Generator\Utils; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class StructValue stands for an enumeration value |
|
10
|
|
|
*/ |
|
11
|
|
|
class StructValue extends AbstractModel |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var string |
|
15
|
|
|
*/ |
|
16
|
|
|
const MATCH_PATTERN = '/([[:upper:]]+[[:lower:]]*)|([[:lower:]]+)|(\d+)/'; |
|
17
|
|
|
/** |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
const REAPLCEMENT_PATTERN = '$1$2$3_'; |
|
21
|
|
|
/** |
|
22
|
|
|
* Store the constants generated per structName |
|
23
|
|
|
* @var array |
|
24
|
|
|
*/ |
|
25
|
|
|
private static $uniqueConstants = array(); |
|
26
|
|
|
/** |
|
27
|
|
|
* The index of the value in the enumeration struct |
|
28
|
|
|
* @var int |
|
29
|
|
|
*/ |
|
30
|
|
|
private $index = 0; |
|
31
|
|
|
/** |
|
32
|
|
|
* Main constructor |
|
33
|
|
|
* @see AbstractModel::__construct() |
|
34
|
|
|
* @uses AbstractModel::setOwner() |
|
35
|
|
|
* @uses StructValue::setIndex() |
|
36
|
|
|
* @param Generator $generator |
|
37
|
|
|
* @param string $name the original name |
|
38
|
|
|
* @param string $index the index of the value in the enumeration struct |
|
39
|
|
|
* @param Struct $struct defines the struct which owns this value |
|
40
|
|
|
*/ |
|
41
|
272 |
|
public function __construct(Generator $generator, $name, $index, Struct $struct) |
|
42
|
|
|
{ |
|
43
|
272 |
|
parent::__construct($generator, $name); |
|
44
|
272 |
|
$this->setIndex($index); |
|
45
|
268 |
|
$this->setOwner($struct); |
|
46
|
268 |
|
} |
|
47
|
|
|
/** |
|
48
|
|
|
* Returns the name of the value as constant |
|
49
|
|
|
* @see AbstractModel::getCleanName() |
|
50
|
|
|
* @uses AbstractModel::getCleanName() |
|
51
|
|
|
* @uses AbstractModel::getName() |
|
52
|
|
|
* @uses AbstractModel::getOwner() |
|
53
|
|
|
* @uses StructValue::constantSuffix() |
|
54
|
|
|
* @uses StructValue::getIndex() |
|
55
|
|
|
* @uses StructValue::getOwner() |
|
56
|
|
|
* @uses Generator::getOptionGenericConstantsNames() |
|
57
|
|
|
* @param bool $keepMultipleUnderscores optional, allows to keep the multiple consecutive underscores |
|
58
|
|
|
* @return string |
|
59
|
|
|
*/ |
|
60
|
52 |
|
public function getCleanName($keepMultipleUnderscores = false) |
|
61
|
|
|
{ |
|
62
|
52 |
|
if ($this->getGenerator()->getOptionGenericConstantsNames()) { |
|
63
|
4 |
|
return 'ENUM_VALUE_' . $this->getIndex(); |
|
64
|
|
|
} else { |
|
65
|
48 |
|
$nameWithSeparatedWords = $this->getNameWithSeparatedWords($keepMultipleUnderscores); |
|
66
|
48 |
|
$key = self::constantSuffix($this->getOwner()->getName(), $nameWithSeparatedWords, $this->getIndex()); |
|
67
|
48 |
|
return 'VALUE_' . strtoupper($nameWithSeparatedWords . ($key ? '_' . $key : '')); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
/** |
|
71
|
|
|
* @param bool $keepMultipleUnderscores optional, allows to keep the multiple consecutive underscores |
|
72
|
|
|
* @return string |
|
73
|
|
|
*/ |
|
74
|
48 |
|
public function getNameWithSeparatedWords($keepMultipleUnderscores = false) |
|
75
|
|
|
{ |
|
76
|
48 |
|
return trim(self::cleanString(preg_replace(self::MATCH_PATTERN, self::REAPLCEMENT_PATTERN, $this->getName()), $keepMultipleUnderscores), '_'); |
|
77
|
|
|
} |
|
78
|
|
|
/** |
|
79
|
|
|
* Returns the value with good type |
|
80
|
|
|
* @uses AbstractModel::getName() |
|
81
|
|
|
* @uses Utils::getValueWithinItsType() |
|
82
|
|
|
* @return mixed |
|
83
|
|
|
*/ |
|
84
|
52 |
|
public function getValue() |
|
85
|
|
|
{ |
|
86
|
52 |
|
return Utils::getValueWithinItsType($this->getName()); |
|
87
|
|
|
} |
|
88
|
|
|
/** |
|
89
|
|
|
* Gets the index attribute value |
|
90
|
|
|
* @return int |
|
91
|
|
|
*/ |
|
92
|
52 |
|
public function getIndex() |
|
93
|
|
|
{ |
|
94
|
52 |
|
return $this->index; |
|
95
|
|
|
} |
|
96
|
|
|
/** |
|
97
|
|
|
* Sets the index attribute value |
|
98
|
|
|
* @throws \InvalidArgumentException |
|
99
|
|
|
* @param int $index |
|
100
|
|
|
* @return StructValue |
|
101
|
|
|
*/ |
|
102
|
272 |
|
public function setIndex($index) |
|
103
|
|
|
{ |
|
104
|
272 |
|
if (!is_int($index) || $index < 0) { |
|
105
|
8 |
|
throw new \InvalidArgumentException(sprintf('The value\'s index must be aa positive integer, "%s" given', var_export($index, true))); |
|
106
|
|
|
} |
|
107
|
268 |
|
$this->index = $index; |
|
108
|
268 |
|
return $this; |
|
109
|
|
|
} |
|
110
|
|
|
/** |
|
111
|
|
|
* Returns the index which has to be added at the end of natural constant name defined with the value cleaned |
|
112
|
|
|
* Allows to avoid multiple constant name to be indentic |
|
113
|
|
|
* @param string $structName the struct name |
|
114
|
|
|
* @param string $value the value |
|
115
|
|
|
* @param int $index the position of the value |
|
116
|
|
|
* @return int |
|
117
|
|
|
*/ |
|
118
|
48 |
|
private static function constantSuffix($structName, $value, $index) |
|
119
|
|
|
{ |
|
120
|
48 |
|
$key = strtoupper($structName . '_' . $value); |
|
121
|
48 |
|
$indexedKey = $key . '_' . $index; |
|
122
|
48 |
|
if (array_key_exists($indexedKey, self::$uniqueConstants)) { |
|
123
|
44 |
|
return self::$uniqueConstants[$indexedKey]; |
|
124
|
28 |
|
} elseif (!array_key_exists($key, self::$uniqueConstants)) { |
|
125
|
28 |
|
self::$uniqueConstants[$key] = 0; |
|
126
|
21 |
|
} else { |
|
127
|
4 |
|
self::$uniqueConstants[$key]++; |
|
128
|
|
|
} |
|
129
|
28 |
|
self::$uniqueConstants[$indexedKey] = self::$uniqueConstants[$key]; |
|
130
|
28 |
|
return self::$uniqueConstants[$key]; |
|
131
|
|
|
} |
|
132
|
|
|
/** |
|
133
|
|
|
* Returns the owner model object, meaning a Struct object |
|
134
|
|
|
* @see AbstractModel::getOwner() |
|
135
|
|
|
* @uses AbstractModel::getOwner() |
|
136
|
|
|
* @return Struct |
|
137
|
|
|
*/ |
|
138
|
48 |
|
public function getOwner() |
|
139
|
|
|
{ |
|
140
|
48 |
|
return parent::getOwner(); |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|