| 1 |
<?php |
| 2 |
|
| 3 |
namespace TablePress\PhpOffice\PhpSpreadsheet\Calculation\Statistical; |
| 4 |
|
| 5 |
use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Functions; |
| 6 |
use TablePress\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 |
/** @var float[] $mArgs */ |
| 39 |
|
| 40 |
return $mArgs[$entry]; |
| 41 |
} |
| 42 |
|
| 43 |
return ExcelError::VALUE(); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* SMALL. |
| 48 |
* |
| 49 |
* Returns the nth smallest value in a data set. You can use this function to |
| 50 |
* select a value based on its relative standing. |
| 51 |
* |
| 52 |
* Excel Function: |
| 53 |
* SMALL(value1[,value2[, ...]],entry) |
| 54 |
* |
| 55 |
* @param mixed $args Data values |
| 56 |
* |
| 57 |
* @return float|string The result, or a string containing an error |
| 58 |
*/ |
| 59 |
public static function small(...$args) |
| 60 |
{ |
| 61 |
$aArgs = Functions::flattenArray($args); |
| 62 |
|
| 63 |
$entry = array_pop($aArgs); |
| 64 |
|
| 65 |
if ((is_numeric($entry)) && (!is_string($entry))) { |
| 66 |
$entry = (int) floor($entry); |
| 67 |
|
| 68 |
$mArgs = self::filter($aArgs); |
| 69 |
$count = Counts::COUNT($mArgs); |
| 70 |
--$entry; |
| 71 |
if ($count === 0 || $entry < 0 || $entry >= $count) { |
| 72 |
return ExcelError::NAN(); |
| 73 |
} |
| 74 |
sort($mArgs); |
| 75 |
/** @var float[] $mArgs */ |
| 76 |
|
| 77 |
return $mArgs[$entry]; |
| 78 |
} |
| 79 |
|
| 80 |
return ExcelError::VALUE(); |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* @param mixed[] $args Data values |
| 85 |
* |
| 86 |
* @return mixed[] |
| 87 |
*/ |
| 88 |
protected static function filter(array $args): array |
| 89 |
{ |
| 90 |
$mArgs = []; |
| 91 |
|
| 92 |
foreach ($args as $arg) { |
| 93 |
// Is it a numeric value? |
| 94 |
if ((is_numeric($arg)) && (!is_string($arg))) { |
| 95 |
$mArgs[] = $arg; |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
return $mArgs; |
| 100 |
} |
| 101 |
} |
| 102 |
|