* @package Vhs * @subpackage ViewHelpers\Math */ class MedianViewHelper extends AbstractSingleMathViewHelper { /** * @return mixed * @throw Exception */ public function render() { $a = $this->getInlineArgument(); $aIsIterable = $this->assertIsArrayOrIterator($a); if (TRUE === $aIsIterable) { $a = $this->arrayFromArrayOrTraversableOrCSV($a); sort($a, SORT_NUMERIC); $size = count($a); $midpoint = $size / 2; if (1 === $size % 2) { /* * Array indexes of float are truncated to integers, * not everybody knows, let's make it explicit for everybody * wondering. */ return $a[(integer) $midpoint]; } $candidates = array_slice($a, floor($midpoint) - 1, 2); return array_sum($candidates) / 2; } return $a; } }