Completed
Pull Request — master (#2)
by Pol
04:12
created

Partition   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 31
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A exportArrayCopy() 0 7 1
A getWeight() 0 13 3
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\Weightable;
9
use drupol\phpartition\Contract\PartitionItem as PartitionItemInterface;
10
11
/**
12
 * Class Partition.
13
 */
14
class Partition extends \ArrayObject implements Weightable
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 getWeight(): float
33
    {
34
        $weight = 0;
35
36
        foreach ($this as $item) {
37
            if ($item instanceof PartitionItemInterface) {
38
                $item = $item->getWeight();
39
            }
40
41
            $weight += $item;
42
        }
43
44
        return $weight;
45
    }
46
}
47