Passed
Pull Request — master (#62)
by Sergei
13:28
created

ArrayCollection   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 15
eloc 28
c 1
b 0
f 0
dl 0
loc 93
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getModifiers() 0 3 1
A addModifier() 0 4 1
A addModifiers() 0 4 1
A setModifiers() 0 4 1
A performArray() 0 12 4
A setData() 0 3 1
A keyExists() 0 3 1
A toArray() 0 9 2
A pullCollectionArgs() 0 3 1
A getData() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Arrays\Collection;
6
7
use ArrayAccess;
8
use Countable;
9
use IteratorAggregate;
10
use Yiisoft\Arrays\ArrayAccessTrait;
11
use Yiisoft\Arrays\Collection\Modifier\ModifierInterface;
12
13
final class ArrayCollection implements ArrayAccess, IteratorAggregate, Countable
14
{
15
    use ArrayAccessTrait;
16
17
    private array $data;
18
19
    /**
20
     * @var ModifierInterface[]
21
     */
22
    private array $modifiers = [];
23
24
    public function __construct(array $data = [])
25
    {
26
        $this->data = $data;
27
    }
28
29
    public function setData(array $data): void
30
    {
31
        $this->data = $data;
32
    }
33
34
    /**
35
     * @return ModifierInterface[]
36
     */
37
    public function getModifiers(): array
38
    {
39
        return $this->modifiers;
40
    }
41
42
    /**
43
     * @param ModifierInterface[] $modifiers
44
     * @return self
45
     */
46
    public function setModifiers(array $modifiers): self
47
    {
48
        $this->modifiers = $modifiers;
49
        return $this;
50
    }
51
52
    public function addModifier(ModifierInterface $modifier): self
53
    {
54
        $this->modifiers[] = $modifier;
55
        return $this;
56
    }
57
58
    /**
59
     * @param ModifierInterface[] $modifiers
60
     * @return self
61
     */
62
    public function addModifiers(array $modifiers): self
63
    {
64
        $this->modifiers = array_merge($this->modifiers, $modifiers);
65
        return $this;
66
    }
67
68
    public function pullCollectionArgs(ArrayCollection $collection): void
69
    {
70
        $this->modifiers = array_merge($this->modifiers, $collection->modifiers);
71
    }
72
73
    public function getData(): array
74
    {
75
        return $this->data;
76
    }
77
78
    public function keyExists($key): bool
79
    {
80
        return array_key_exists($key, $this->data);
81
    }
82
83
    public function toArray(): array
84
    {
85
        $array = $this->performArray($this->getIterator()->getArrayCopy());
86
87
        foreach ($this->modifiers as $modifier) {
88
            $array = $modifier->apply($array);
89
        }
90
91
        return $array;
92
    }
93
94
    private function performArray(array $array): array
95
    {
96
        foreach ($array as $k => $v) {
97
            if ($v instanceof ArrayCollection) {
98
                $array[$k] = $v->toArray();
99
            } elseif (is_array($v)) {
100
                $array[$k] = $this->performArray($v);
101
            } else {
102
                $array[$k] = $v;
103
            }
104
        }
105
        return $array;
106
    }
107
}
108