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

ArrayCollection::__construct()   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\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 ...$modifiers): self
54
    {
55
        $this->modifiers = array_merge($this->modifiers, $modifiers);
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
    /**
80
     * @param int|string $key
81
     * @return bool
82
     */
83
    public function keyExists($key): bool
84
    {
85
        return array_key_exists($key, $this->data);
86
    }
87
88
    public function toArray(): array
89
    {
90
        $array = $this->performArray($this->getIterator()->getArrayCopy());
91
92
        foreach ($this->modifiers as $modifier) {
93
            if ($modifier instanceof DataModifierInterface) {
94
                $array = $modifier->apply($array);
95
            }
96
        }
97
98
        return $array;
99
    }
100
101
    private function performArray(array $array): array
102
    {
103
        foreach ($array as $k => $v) {
104
            if ($v instanceof ArrayCollection) {
105
                $array[$k] = $v->toArray();
106
            } elseif (is_array($v)) {
107
                $array[$k] = $this->performArray($v);
108
            } else {
109
                $array[$k] = $v;
110
            }
111
        }
112
        return $array;
113
    }
114
}
115