Mutator   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 38
ccs 0
cts 16
cp 0
rs 10
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A replacePaths() 0 13 1
A deletePaths() 0 10 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Remorhaz\JSON\Path\Processor\Mutator;
6
7
use Remorhaz\JSON\Data\Export\EventDecoderInterface;
8
use Remorhaz\JSON\Data\Path\Path;
9
use Remorhaz\JSON\Data\Path\PathInterface;
10
use Remorhaz\JSON\Data\Value\NodeValueInterface;
11
use Remorhaz\JSON\Data\Walker\ValueWalkerInterface;
12
13
final class Mutator implements MutatorInterface
14
{
15
16
    private $valueWalker;
17
18
    private $eventDecoder;
19
20
    public function __construct(ValueWalkerInterface $walker, EventDecoderInterface $eventDecoder)
21
    {
22
        $this->valueWalker = $walker;
23
        $this->eventDecoder = $eventDecoder;
24
    }
25
26
    public function deletePaths(NodeValueInterface $rootNode, PathInterface ...$paths): ?NodeValueInterface
27
    {
28
        $modifier = new DeleteMutation(...$paths);
29
        $events =  $this
30
            ->valueWalker
31
            ->createMutableEventIterator($rootNode, new Path(), $modifier);
32
33
        return $this
34
            ->eventDecoder
35
            ->exportEvents($events);
36
    }
37
38
    public function replacePaths(
39
        NodeValueInterface $rootNode,
40
        NodeValueInterface $newNode,
41
        PathInterface ...$paths
42
    ): NodeValueInterface {
43
        $modifier = new ReplaceMutation($newNode, ...$paths);
44
        $events =  $this
45
            ->valueWalker
46
            ->createMutableEventIterator($rootNode, new Path(), $modifier);
47
48
        return $this
49
            ->eventDecoder
50
            ->exportExistingEvents($events);
51
    }
52
}
53