Passed
Pull Request — master (#62)
by Sergei
12:01
created

ArrayCollection::setModifiers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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