PageRenderTime 37ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/mantisbt-1.2.8/core/classes/MantisEnum.class.php

#
PHP | 220 lines | 78 code | 33 blank | 109 comment | 9 complexity | f10ec9443822303379585fbec67570e3 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0
  1. <?php
  2. # MantisBT - a php based bugtracking system
  3. # MantisBT is free software: you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation, either version 2 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # MantisBT is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with MantisBT. If not, see <http://www.gnu.org/licenses/>.
  15. /**
  16. * @package MantisBT
  17. * @copyright Copyright (C) 2000 - 2011 MantisBT Team - mantisbt-dev@lists.sourceforge.net
  18. * @link http://www.mantisbt.org
  19. */
  20. /**
  21. * A class that handles MantisBT Enumerations.
  22. *
  23. * For example: 10:lablel1,20:label2
  24. *
  25. * @package MantisBT
  26. * @subpackage classes
  27. */
  28. class MantisEnum {
  29. /**
  30. * Separator that is used to separate the enum values from their labels.
  31. */
  32. const VALUE_LABEL_SEPARATOR = ':';
  33. /**
  34. * Separator that is used to separate the enum tuples within an enumeration definition.
  35. */
  36. const TUPLE_SEPARATOR = ',';
  37. /**
  38. *
  39. * @var array Used to cache previous results
  40. */
  41. private static $_cacheAssocArrayIndexedByValues = array();
  42. /**
  43. * Get the string associated with the $p_enum value
  44. *
  45. * @param string $enumString
  46. * @param int $value
  47. * @return string
  48. */
  49. public static function getLabel( $enumString, $value ) {
  50. $assocArray = MantisEnum::getAssocArrayIndexedByValues( $enumString );
  51. $valueAsInteger = (int)$value;
  52. if ( isset( $assocArray[$valueAsInteger] ) ) {
  53. return $assocArray[$valueAsInteger];
  54. }
  55. return MantisEnum::getLabelForUnknownValue( $valueAsInteger );
  56. }
  57. /**
  58. * Gets the localized label corresponding to a value. Note that this method
  59. * takes in the standard / localized enums so that if the value is in the localized
  60. * enum but not the standard one, then it returns not found.
  61. *
  62. * @param string $enumString The standard enum string.
  63. * @param string $localizedEnumString The localized enum string.
  64. * @param integer $value The value to lookup.
  65. *
  66. * @return the label or the decorated value to represent not found.
  67. */
  68. public static function getLocalizedLabel( $enumString, $localizedEnumString, $value ) {
  69. if ( !MantisEnum::hasValue( $enumString, $value ) ) {
  70. return MantisEnum::getLabelForUnknownValue( $value );
  71. }
  72. return MantisEnum::getLabel( $localizedEnumString, $value );
  73. }
  74. /**
  75. * Gets the value associated with the specified label.
  76. *
  77. * @param string $enumString The enumerated string.
  78. * @param string $label The label to map.
  79. * @return integer value of the enum or false if not found.
  80. */
  81. public static function getValue( $enumString, $label ) {
  82. $assocArrayByLabels = MantisEnum::getAssocArrayIndexedByLabels( $enumString );
  83. if ( isset( $assocArrayByLabels[$label] ) ) {
  84. return $assocArrayByLabels[$label];
  85. }
  86. return false;
  87. }
  88. /**
  89. * Get an associate array for the tuples of the enum where the values
  90. * are the array indices and the labels are the array values.
  91. *
  92. * @param string $enumString
  93. * @return associate array indexed by labels.
  94. */
  95. public static function getAssocArrayIndexedByValues( $enumString ) {
  96. if( isset( self::$_cacheAssocArrayIndexedByValues[$enumString] ) ) {
  97. return self::$_cacheAssocArrayIndexedByValues[$enumString];
  98. }
  99. $tuples = MantisEnum::getArrayOfTuples( $enumString );
  100. $tuplesCount = count( $tuples );
  101. $assocArray = array();
  102. foreach ( $tuples as $tuple ) {
  103. $tupleTokens = MantisEnum::getArrayForTuple( $tuple );
  104. # if not a proper tuple, skip.
  105. if ( count( $tupleTokens ) != 2 ) {
  106. continue;
  107. }
  108. $value = (int) trim( $tupleTokens[0] );
  109. # if already set, skip.
  110. if ( isset( $assocArray[ $value ] ) ) {
  111. continue;
  112. }
  113. $label = trim( $tupleTokens[1] );
  114. $assocArray[$value] = $label;
  115. }
  116. self::$_cacheAssocArrayIndexedByValues[$enumString] = $assocArray;
  117. return $assocArray;
  118. }
  119. /**
  120. * Get an associate array for the tuples of the enum where the labels
  121. * are the array indices and the values are the array values.
  122. *
  123. * @param string $enumString
  124. * @return associate array indexed by labels.
  125. */
  126. public static function getAssocArrayIndexedByLabels( $enumString ) {
  127. return array_flip( MantisEnum::getAssocArrayIndexedByValues( $enumString ) );
  128. }
  129. /**
  130. * Gets an array with all values in the enum.
  131. *
  132. * @param $enumString
  133. * @return array of unique values.
  134. */
  135. public static function getValues( $enumString ) {
  136. return array_unique( array_keys( MantisEnum::getAssocArrayIndexedByValues( $enumString ) ) );
  137. }
  138. /**
  139. * Checks if the specified enum string contains the specified value.
  140. *
  141. * @param string $enumString The enumeration string.
  142. * @param integer $value The value to chec,
  143. * @return bool true if found, false otherwise.
  144. */
  145. public static function hasValue( $enumString, $value ) {
  146. $assocArray = MantisEnum::getAssocArrayIndexedByValues( $enumString );
  147. $valueAsInteger = (int)$value;
  148. return isset( $assocArray[$valueAsInteger] );
  149. }
  150. /**
  151. * Breaks up an enum string into num:value elements
  152. *
  153. * @param string $enumString enum string
  154. * @return array array of num:value elements
  155. */
  156. private static function getArrayOfTuples( $enumString ) {
  157. if ( strlen( trim( $enumString ) ) == 0 ) {
  158. return array();
  159. }
  160. $rawArray = explode( MantisEnum::TUPLE_SEPARATOR, $enumString );
  161. $trimmedArray = array();
  162. foreach ( $rawArray as $tuple ) {
  163. $trimmedArray[] = trim( $tuple );
  164. }
  165. return $trimmedArray;
  166. }
  167. /**
  168. * Given one num:value pair it will return both in an array
  169. * num will be first (element 0) value second (element 1)
  170. *
  171. * @param string $tuple a num:value pair
  172. * @return array array(value, label)
  173. */
  174. private static function getArrayForTuple( $tuple ) {
  175. return explode( MantisEnum::VALUE_LABEL_SEPARATOR, $tuple );
  176. }
  177. /**
  178. * Given a value it decorates it and returns it as the label.
  179. *
  180. * @param integer The value (e.g. 50).
  181. * @return The decorated value (e.g. @50@).
  182. */
  183. private static function getLabelForUnknownValue( $value ) {
  184. $valueAsInteger = (int)$value;
  185. return '@' . $valueAsInteger . '@';
  186. }
  187. }