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

ArrayCollection::pullCollectionArgs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
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\ModifierInterface\DataModifierInterface;
12
use Yiisoft\Arrays\Collection\Modifier\ModifierInterface\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): self
31
    {
32
        $this->data = $data;
33
        return $this;
34
    }
35
36
    /**
37
     * @return ModifierInterface[]
38
     */
39
    public function getModifiers(): array
40
    {
41
        return $this->modifiers;
42
    }
43
44
    /**
45
     * @param ModifierInterface[] $modifiers
46
     * @return self
47
     */
48
    public function setModifiers(array $modifiers): self
49
    {
50
        $this->modifiers = $modifiers;
51
        return $this;
52
    }
53
54
    public function addModifier(ModifierInterface ...$modifiers): self
55
    {
56
        $this->modifiers = array_merge($this->modifiers, $modifiers);
57
        return $this;
58
    }
59
60
    /**
61
     * @param ModifierInterface[] $modifiers
62
     * @return self
63
     */
64
    public function addModifiers(array $modifiers): self
65
    {
66
        $this->modifiers = array_merge($this->modifiers, $modifiers);
67
        return $this;
68
    }
69
70
    public function pullCollectionArgs(ArrayCollection $collection): void
71
    {
72
        $this->modifiers = array_merge($this->modifiers, $collection->modifiers);
73
    }
74
75
    public function getData(): array
76
    {
77
        return $this->data;
78
    }
79
80
    /**
81
     * @param int|string $key
82
     * @return bool
83
     */
84
    public function keyExists($key): bool
85
    {
86
        return array_key_exists($key, $this->data);
87
    }
88
89
    public function toArray(): array
90
    {
91
        $array = $this->performArray($this->getIterator()->getArrayCopy());
92
93
        foreach ($this->modifiers as $modifier) {
94
            if ($modifier instanceof DataModifierInterface) {
95
                $array = $modifier->apply($array);
96
            }
97
        }
98
99
        return $array;
100
    }
101
102
    private function performArray(array $array): array
103
    {
104
        foreach ($array as $k => $v) {
105
            if ($v instanceof ArrayCollection) {
106
                $array[$k] = $v->toArray();
107
            } elseif (is_array($v)) {
108
                $array[$k] = $this->performArray($v);
109
            } else {
110
                $array[$k] = $v;
111
            }
112
        }
113
        return $array;
114
    }
115
}
116