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

Partition::exportArrayCopy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 7
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\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