Failed Conditions
Pull Request — master (#32)
by Moesjarraf
09:36
created

Replace::applyToNode()   C

Complexity

Conditions 9
Paths 8

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 24
rs 5.3563
cc 9
eloc 13
nc 8
nop 1
1
<?php
2
3
namespace LegalThings\DataEnricher\Processor;
4
5
use LegalThings\DataEnricher\Node;
6
use LegalThings\DataEnricher\Processor;
7
8
/**
9
 * Replace processor
10
 */
11
class Replace implements Processor
12
{
13
    use Processor\Implementation;
14
    
15
    /**
16
     * Apply processing to a single node
17
     * 
18
     * @param Node $node
19
     */
20
    public function applyToNode(Node $node)
21
    {
22
        $instruction = $node->getInstruction($this);
23
        
24
        if (is_array($instruction) || is_object($instruction)) {
25
            $instruction = (object)$instruction;
26
        }
27
        
28
        if (!isset($instruction) || !isset($instruction->input) || !isset($instruction->replacement)) {
29
            return;
30
        }
31
        
32
        if (!isset($instruction->find) && !isset($instruction->regex)) {
33
            return;
34
        }
35
        
36
        if (isset($instruction->find)) {
37
            $result = str_replace($instruction->find, $instruction->replacement, $instruction->input);
38
        } else {
39
            $result = preg_replace($instruction->regex, $instruction->replacement, $instruction->input);
40
        }
41
        
42
        $node->setResult($result);
43
    }
44
}
45