Passed
Pull Request — master (#62)
by Sergei
10:47
created

ReplaceValue   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 12
c 1
b 0
f 0
dl 0
loc 40
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A apply() 0 6 2
A toValue() 0 5 1
A forKey() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Arrays\Collection\Modifier;
6
7
use Yiisoft\Arrays\Collection\Modifier\ModifierInterface\DataModifierInterface;
8
9
final class ReplaceValue implements DataModifierInterface
10
{
11
    /**
12
     * @var int|string|null
13
     */
14
    private $key = null;
15
16
    /**
17
     * @var mixed
18
     */
19
    private $value = null;
20
21
    public function apply(array $data): array
22
    {
23
        if ($this->key !== null) {
24
            $data[$this->key] = $this->value;
25
        }
26
        return $data;
27
    }
28
29
    /**
30
     * @param int|string $key
31
     * @return self
32
     */
33
    public function forKey($key): self
34
    {
35
        $new = clone $this;
36
        $new->key = $key;
37
        return $new;
38
    }
39
40
    /**
41
     * @param mixed $value
42
     * @return self
43
     */
44
    public function toValue($value): self
45
    {
46
        $new = clone $this;
47
        $new->value = $value;
48
        return $new;
49
    }
50
}
51