Mutator::deletePaths()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 10
ccs 0
cts 6
cp 0
crap 2
rs 10
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