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

InsertValueBeforeKey::setValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Arrays\Collection\Modifier;
6
7
final class InsertValueBeforeKey implements DataModifierInterface
8
{
9
    private $key;
10
11
    private $value;
12
13
    private $beforeKey;
14
15
    public function withKey($key): self
16
    {
17
        $new = clone $this;
18
        $new->key = $key;
19
        return $new;
20
    }
21
22
    public function setValue($value): self
23
    {
24
        $new = clone $this;
25
        $new->value = $value;
26
        return $new;
27
    }
28
29
    public function beforeKey($key): self
30
    {
31
        $new = clone $this;
32
        $new->beforeKey = $key;
33
        return $new;
34
    }
35
36
37
    public function apply(array $data): array
38
    {
39
        $res = [];
40
        foreach ($data as $k => $v) {
41
            if ($k === $this->beforeKey) {
42
                $res[$this->key] = $this->value;
43
            }
44
            $res[$k] = $v;
45
        }
46
47
        return $res;
48
    }
49
}
50