for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
/**
* @author Igor Pomiluyko <[email protected]>
*/
namespace WS\Utils\Collections\Functions\Group\Aggregator;
use WS\Utils\Collections\Collection;
use WS\Utils\Collections\Functions\ObjectFunctions;
class Avg
{
private $fieldName;
public function __construct(string $fieldName)
$this->fieldName = $fieldName;
}
public function __invoke(Collection $collection)
$acc = null;
$cnt = 0;
foreach ($collection as $element) {
$acc += ObjectFunctions::getPropertyValue($element, $this->fieldName);
$cnt++;
if (!$cnt) {
return null;
return $acc / $cnt;