public function average(callable $func = null)
{
$resultTotal = 0;
$itemCount = 0;
$source = $this->getSelectIteratorOrInnerIterator($func);
foreach ($source as $item) {
if (!is_numeric($item)) {
throw new UnexpectedValueException("Cannot calculate an average on a none numeric value");
}
$resultTotal += $item;
$itemCount++;
}
return $itemCount == 0 ? 0 : $resultTotal / $itemCount;
}