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\Statistical;
   4  
   5  use PhpOffice\PhpSpreadsheet\Calculation\Functions;
   6  use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
   7  
   8  class Size
   9  {
  10      /**
  11       * LARGE.
  12       *
  13       * Returns the nth largest value in a data set. You can use this function to
  14       *        select a value based on its relative standing.
  15       *
  16       * Excel Function:
  17       *        LARGE(value1[,value2[, ...]],entry)
  18       *
  19       * @param mixed $args Data values
  20       *
  21       * @return float|string The result, or a string containing an error
  22       */
  23      public static function large(...$args)
  24      {
  25          $aArgs = Functions::flattenArray($args);
  26          $entry = array_pop($aArgs);
  27  
  28          if ((is_numeric($entry)) && (!is_string($entry))) {
  29              $entry = (int) floor($entry);
  30  
  31              $mArgs = self::filter($aArgs);
  32              $count = Counts::COUNT($mArgs);
  33              --$entry;
  34              if ($count === 0 || $entry < 0 || $entry >= $count) {
  35                  return ExcelError::NAN();
  36              }
  37              rsort($mArgs);
  38  
  39              return $mArgs[$entry];
  40          }
  41  
  42          return ExcelError::VALUE();
  43      }
  44  
  45      /**
  46       * SMALL.
  47       *
  48       * Returns the nth smallest value in a data set. You can use this function to
  49       *        select a value based on its relative standing.
  50       *
  51       * Excel Function:
  52       *        SMALL(value1[,value2[, ...]],entry)
  53       *
  54       * @param mixed $args Data values
  55       *
  56       * @return float|string The result, or a string containing an error
  57       */
  58      public static function small(...$args)
  59      {
  60          $aArgs = Functions::flattenArray($args);
  61  
  62          $entry = array_pop($aArgs);
  63  
  64          if ((is_numeric($entry)) && (!is_string($entry))) {
  65              $entry = (int) floor($entry);
  66  
  67              $mArgs = self::filter($aArgs);
  68              $count = Counts::COUNT($mArgs);
  69              --$entry;
  70              if ($count === 0 || $entry < 0 || $entry >= $count) {
  71                  return ExcelError::NAN();
  72              }
  73              sort($mArgs);
  74  
  75              return $mArgs[$entry];
  76          }
  77  
  78          return ExcelError::VALUE();
  79      }
  80  
  81      /**
  82       * @param mixed[] $args Data values
  83       */
  84      protected static function filter(array $args): array
  85      {
  86          $mArgs = [];
  87  
  88          foreach ($args as $arg) {
  89              // Is it a numeric value?
  90              if ((is_numeric($arg)) && (!is_string($arg))) {
  91                  $mArgs[] = $arg;
  92              }
  93          }
  94  
  95          return $mArgs;
  96      }
  97  }