Bounds::calculate()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 16
rs 9.4286
cc 2
eloc 11
nc 2
nop 1
1
<?php
2
3
/*
4
 * (c) Jean-François Lépine <https://twitter.com/Halleck45>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Hal\Component\Bounds;
11
use Hal\Component\Bounds\Result\BoundsResult;
12
use Hal\Component\Result\ResultCollection;
13
14
/**
15
 * ResultBoundary
16
 *
17
 * @author Jean-François Lépine <https://twitter.com/Halleck45>
18
 */
19
class Bounds implements BoundsInterface {
20
21
    /**
22
     * @inheritdoc
23
     * @return \Hal\Component\Bounds\Result\ResultInterface
24
     */
25
    public function calculate(ResultCollection $collection) {
26
        $array = $collection->asArray();
27
28
        $arrayMerged = call_user_func_array('array_merge_recursive', $array);
29
30
        $min = $max = $average = $sum = array();
31
        foreach($arrayMerged as $key => $values) {
32
            $values = (array) $values;
33
            $max[$key] = max($values);
34
            $min[$key] = min($values);
35
            $sum[$key] = array_sum($values);
36
            $average[$key] = round($sum[$key] / count($values, COUNT_NORMAL),2);
37
        }
38
39
        return new BoundsResult($min, $max, $average, $sum);
40
    }
41
}