[ Index ]

PHP Cross Reference of MantisBT

title

Body

[close]

/core/classes/ -> MantisEnum.class.php (source)

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


Generated: Thu Jul 28 15:48:31 2011 Cross-referenced by PHPXref 0.7