[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/spout/src/Spout/Writer/Common/Helper/ -> CellHelper.php (source)

   1  <?php
   2  
   3  namespace Box\Spout\Writer\Common\Helper;
   4  
   5  /**
   6   * Class CellHelper
   7   * This class provides helper functions when working with cells
   8   *
   9   * @package Box\Spout\Writer\Common\Helper
  10   */
  11  class CellHelper
  12  {
  13      /** @var array Cache containing the mapping column index => cell index */
  14      private static $columnIndexToCellIndexCache = [];
  15  
  16      /**
  17       * Returns the cell index (base 26) associated to the base 10 column index.
  18       * Excel uses A to Z letters for column indexing, where A is the 1st column,
  19       * Z is the 26th and AA is the 27th.
  20       * The mapping is zero based, so that 0 maps to A, B maps to 1, Z to 25 and AA to 26.
  21       *
  22       * @param int $columnIndex The Excel column index (0, 42, ...)
  23       * @return string The associated cell index ('A', 'BC', ...)
  24       */
  25      public static function getCellIndexFromColumnIndex($columnIndex)
  26      {
  27          $originalColumnIndex = $columnIndex;
  28  
  29          // Using isset here because it is way faster than array_key_exists...
  30          if (!isset(self::$columnIndexToCellIndexCache[$originalColumnIndex])) {
  31              $cellIndex = '';
  32              $capitalAAsciiValue = ord('A');
  33  
  34              do {
  35                  $modulus = $columnIndex % 26;
  36                  $cellIndex = chr($capitalAAsciiValue + $modulus) . $cellIndex;
  37  
  38                  // substracting 1 because it's zero-based
  39                  $columnIndex = intval($columnIndex / 26) - 1;
  40  
  41              } while ($columnIndex >= 0);
  42  
  43              self::$columnIndexToCellIndexCache[$originalColumnIndex] = $cellIndex;
  44          }
  45  
  46          return self::$columnIndexToCellIndexCache[$originalColumnIndex];
  47      }
  48  
  49      /**
  50       * @param $value
  51       * @return bool Whether the given value is a non empty string
  52       */
  53      public static function isNonEmptyString($value)
  54      {
  55          return (gettype($value) === 'string' && $value !== '');
  56      }
  57  
  58      /**
  59       * Returns whether the given value is numeric.
  60       * A numeric value is from type "integer" or "double" ("float" is not returned by gettype).
  61       *
  62       * @param $value
  63       * @return bool Whether the given value is numeric
  64       */
  65      public static function isNumeric($value)
  66      {
  67          $valueType = gettype($value);
  68          return ($valueType === 'integer' || $valueType === 'double');
  69      }
  70  
  71      /**
  72       * Returns whether the given value is boolean.
  73       * "true"/"false" and 0/1 are not booleans.
  74       *
  75       * @param $value
  76       * @return bool Whether the given value is boolean
  77       */
  78      public static function isBoolean($value)
  79      {
  80          return gettype($value) === 'boolean';
  81      }
  82  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1