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\LookupRef;
   4  
   5  use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
   6  use PhpOffice\PhpSpreadsheet\Calculation\Functions;
   7  use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
   8  
   9  class Selection
  10  {
  11      use ArrayEnabled;
  12  
  13      /**
  14       * CHOOSE.
  15       *
  16       * Uses lookup_value to return a value from the list of value arguments.
  17       * Use CHOOSE to select one of up to 254 values based on the lookup_value.
  18       *
  19       * Excel Function:
  20       *        =CHOOSE(index_num, value1, [value2], ...)
  21       *
  22       * @param mixed $chosenEntry The entry to select from the list (indexed from 1)
  23       * @param mixed ...$chooseArgs Data values
  24       *
  25       * @return mixed The selected value
  26       */
  27      public static function choose($chosenEntry, ...$chooseArgs)
  28      {
  29          if (is_array($chosenEntry)) {
  30              return self::evaluateArrayArgumentsSubset([self::class, __FUNCTION__], 1, $chosenEntry, ...$chooseArgs);
  31          }
  32  
  33          $entryCount = count($chooseArgs) - 1;
  34  
  35          if (is_numeric($chosenEntry)) {
  36              --$chosenEntry;
  37          } else {
  38              return ExcelError::VALUE();
  39          }
  40          $chosenEntry = floor($chosenEntry);
  41          if (($chosenEntry < 0) || ($chosenEntry > $entryCount)) {
  42              return ExcelError::VALUE();
  43          }
  44  
  45          if (is_array($chooseArgs[$chosenEntry])) {
  46              return Functions::flattenArray($chooseArgs[$chosenEntry]);
  47          }
  48  
  49          return $chooseArgs[$chosenEntry];
  50      }
  51  }