Passed
Pull Request — master (#2)
by Pol
04:33
created

Partition::getWeight()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 0
dl 0
loc 13
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace drupol\phpartition\Partition;
6
7
use drupol\phpartition\Contract\Valuable;
8
use drupol\phpartition\Contract\PartitionItem as PartitionItemInterface;
9
use drupol\phpartition\Contract\Partition as PartitionInterface;
10
11
/**
12
 * Class Partition.
13
 */
14
class Partition extends \ArrayObject implements PartitionInterface
15
{
16
    /**
17
     * @return array
18
     */
19
    public function exportArrayCopy()
20
    {
21
        return \array_map(
22
            static function (Valuable $item) {
23
                return $item->getValue();
24
            },
25
            $this->getArrayCopy()
26
        );
27
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function getBounds(): array
33
    {
34
        $max = PHP_INT_MAX;
35
        $min = PHP_INT_MIN;
36
37
        $bounds = [];
38
39
        foreach ($this as $index => $item) {
40
            if ($item instanceof PartitionItemInterface) {
41
                $item = $item->getWeight();
42
            }
43
44
            if ($item < $max) {
45
                $max = $item;
46
                $bounds[0] = $index;
47
            }
48
49
            if ($item > $min) {
50
                $min = $item;
51
                $bounds[1] = $index;
52
            }
53
        }
54
55
        return $bounds;
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function getWeight(): float
62
    {
63
        $weight = 0;
64
65
        foreach ($this as $item) {
66
            if ($item instanceof PartitionItemInterface) {
67
                $item = $item->getWeight();
68
            }
69
70
            $weight += $item;
71
        }
72
73
        return $weight;
74
    }
75
}
76