forked from mikaelcom/WsdlToPhp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWsdlToPhpStructValue.php
148 lines (148 loc) · 4.43 KB
/
WsdlToPhpStructValue.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
/**
* File for WsdlToPhpStructValue
* @package WsdlToPhpGenerator
* @date 19/12/2012
*/
/**
* Class WsdlToPhpStructValue stands for an enumeration value
* @package WsdlToPhpGenerator
* @date 19/12/2012
*/
class WsdlToPhpStructValue extends WsdlToPhpModel
{
/**
* Store the constants generated per structName
* @var array
*/
private static $uniqueConstants = array();
/**
* The index of the value in the enumeration struct
* @var int
*/
private $index = 0;
/**
* Main constructor
* @see WsdlToPhpModel::__construct()
* @uses WsdlToPhpModel::setOwner()
* @uses WsdlToPhpStructValue::setIndex()
* @param string $_name the original name
* @param string $_index the index of the value in the enumeration struct
* @param WsdlToPhpStruct $_wsdlToPhpStruct defines the struct which owns this value
* @return WsdlToPhpStructValue
*/
public function __construct($_name,$_index,WsdlToPhpStruct $_wsdlToPhpStruct)
{
parent::__construct($_name);
$this->setIndex($_index);
$this->setOwner($_wsdlToPhpStruct);
}
/**
* Method to return the name of the value as constant
* @see WsdlToPhpModel::getCleanName()
* @uses WsdlToPhpModel::getCleanName()
* @uses WsdlToPhpModel::getName()
* @uses WsdlToPhpModel::getOwner()
* @uses WsdlToPhpStructValue::constantSuffix()
* @uses WsdlToPhpStructValue::getIndex()
* @uses WsdlToPhpGenerator::getOptionGenericConstantsNames()
* @param bool $_keepMultipleUnderscores optional, allows to keep the multiple consecutive underscores
* @return string
*/
public function getCleanName($_keepMultipleUnderscores = false)
{
if(WsdlToPhpGenerator::getOptionGenericConstantsNames() && is_numeric($this->getIndex()) && $this->getIndex() >= 0)
return 'ENUM_VALUE_' . $this->getIndex();
else
{
$key = self::constantSuffix($this->getOwner()->getName(),parent::getCleanName($_keepMultipleUnderscores),$this->getIndex());
return 'VALUE_' . strtoupper(parent::getCleanName($_keepMultipleUnderscores)) . ($key?'_' . $key:'');
}
}
/**
* Returns the value with good type
* @uses WsdlToPhpModel::getName()
* @uses WsdlToPhpModel::getValueWithinItsType()
* @return mixed
*/
public function getValue()
{
return self::getValueWithinItsType($this->getName());
}
/**
* Get the index attribute value
* @return int
*/
public function getIndex()
{
return $this->index;
}
/**
* Set the index attribute value
* @param int
* @return int $_index
*/
public function setIndex($_index)
{
return ($this->index = $_index);
}
/**
* Returns the comment lines for this value
* @see WsdlToPhpModel::getComment()
* @uses WsdlToPhpStructValue::getValue()
* @uses WsdlToPhpModel::addMetaComment()
* @param int $_part comment part
* @return array
*/
public function getComment($_part = '')
{
$value = $this->getValue();
$comments = array();
array_push($comments,'Constant for value ' . var_export($value,true));
$this->addMetaComment($comments);
array_push($comments,'@return ' . gettype($value) . ' ' . var_export($value,true));
return $comments;
}
/**
* Returns the declaration of the value
* @see WsdlToPhpStructValue::getCleanName()
* @see WsdlToPhpStructValue::getValue()
* @param string $_structName the name of the struct which the value belongs to
* @param int $_index the index of the constant contained by the struct class
* @return string
*/
public function getDeclaration($_structName,$_index = -1)
{
return 'const ' . $this->getCleanName() . ' = ' . var_export($this->getValue(),true) . ';';
}
/**
* Return the index which has to be added at the end of natural constant name defined with the value cleaned
* Allows to avoid multiple constant name to be indentic
* @param string $_structName the struct name
* @param mixed $_value the value
* @param int $_index the position of the value
* @return int
*/
private static function constantSuffix($_structName,$_value,$_index)
{
$key = strtoupper($_structName . '_' . $_value);
$indexedKey = $key . '_' . $_index;
if(array_key_exists($indexedKey,self::$uniqueConstants))
return self::$uniqueConstants[$indexedKey];
elseif(!array_key_exists($key,self::$uniqueConstants))
self::$uniqueConstants[$key] = 0;
else
self::$uniqueConstants[$key]++;
self::$uniqueConstants[$indexedKey] = self::$uniqueConstants[$key];
return self::$uniqueConstants[$key];
}
/**
* Return class name
* @return string __CLASS__
*/
public function __toString()
{
return __CLASS__;
}
}
?>