Passed
Push — master ( 31414b...55792b )
by Edward
04:28
created

Mutator::replacePaths()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 7
c 0
b 0
f 0
nc 1
nop 3
dl 0
loc 13
ccs 0
cts 6
cp 0
crap 2
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace Remorhaz\JSON\Path\Processor\Mutator;
5
6
use Remorhaz\JSON\Data\Event\ValueWalkerInterface;
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
12
final class Mutator implements MutatorInterface
13
{
14
15
    private $valueWalker;
16
17
    private $eventDecoder;
18
19
    public function __construct(ValueWalkerInterface $walker, EventDecoderInterface $eventDecoder)
20
    {
21
        $this->valueWalker = $walker;
22
        $this->eventDecoder = $eventDecoder;
23
    }
24
25
    public function deletePaths(NodeValueInterface $rootNode, PathInterface ...$paths): ?NodeValueInterface
26
    {
27
        $modifier = new DeleteMutation(...$paths);
28
        $events =  $this
29
            ->valueWalker
30
            ->createMutableEventIterator($rootNode, new Path, $modifier);
31
32
        return $this
33
            ->eventDecoder
34
            ->exportEvents($events);
35
    }
36
37
    public function replacePaths(
38
        NodeValueInterface $rootNode,
39
        NodeValueInterface $newNode,
40
        PathInterface ...$paths
41
    ): NodeValueInterface {
42
        $modifier = new ReplaceMutation($this->valueWalker, $newNode, ...$paths);
43
        $events =  $this
44
            ->valueWalker
45
            ->createMutableEventIterator($rootNode, new Path, $modifier);
46
47
        return $this
48
            ->eventDecoder
49
            ->exportExistingEvents($events);
50
    }
51
}
52