Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 400 and 401]

   1  <?php
   2  
   3  namespace PhpOffice\PhpSpreadsheet\Calculation\TextData;
   4  
   5  use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
   6  use PhpOffice\PhpSpreadsheet\Calculation\Functions;
   7  use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
   8  
   9  class CharacterConvert
  10  {
  11      use ArrayEnabled;
  12  
  13      /**
  14       * CHAR.
  15       *
  16       * @param mixed $character Integer Value to convert to its character representation
  17       *                              Or can be an array of values
  18       *
  19       * @return array|string The character string
  20       *         If an array of values is passed as the argument, then the returned result will also be an array
  21       *            with the same dimensions
  22       */
  23      public static function character($character)
  24      {
  25          if (is_array($character)) {
  26              return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $character);
  27          }
  28  
  29          $character = Helpers::validateInt($character);
  30          $min = Functions::getCompatibilityMode() === Functions::COMPATIBILITY_OPENOFFICE ? 0 : 1;
  31          if ($character < $min || $character > 255) {
  32              return ExcelError::VALUE();
  33          }
  34          $result = iconv('UCS-4LE', 'UTF-8', pack('V', $character));
  35  
  36          return ($result === false) ? '' : $result;
  37      }
  38  
  39      /**
  40       * CODE.
  41       *
  42       * @param mixed $characters String character to convert to its ASCII value
  43       *                              Or can be an array of values
  44       *
  45       * @return array|int|string A string if arguments are invalid
  46       *         If an array of values is passed as the argument, then the returned result will also be an array
  47       *            with the same dimensions
  48       */
  49      public static function code($characters)
  50      {
  51          if (is_array($characters)) {
  52              return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $characters);
  53          }
  54  
  55          $characters = Helpers::extractString($characters);
  56          if ($characters === '') {
  57              return ExcelError::VALUE();
  58          }
  59  
  60          $character = $characters;
  61          if (mb_strlen($characters, 'UTF-8') > 1) {
  62              $character = mb_substr($characters, 0, 1, 'UTF-8');
  63          }
  64  
  65          return self::unicodeToOrd($character);
  66      }
  67  
  68      private static function unicodeToOrd(string $character): int
  69      {
  70          $retVal = 0;
  71          $iconv = iconv('UTF-8', 'UCS-4LE', $character);
  72          if ($iconv !== false) {
  73              $result = unpack('V', $iconv);
  74              if (is_array($result) && isset($result[1])) {
  75                  $retVal = $result[1];
  76              }
  77          }
  78  
  79          return $retVal;
  80      }
  81  }