Completed
Push — master ( bbe4ca...8538c3 )
by Arnold
09:48
created

Transform   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 6
c 1
b 0
f 1
lcom 1
cbo 2
dl 0
loc 51
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B applyToNode() 0 27 6
1
<?php
2
3
namespace LegalThings\DataEnricher\Processor;
4
5
use LegalThings\DataEnricher\Node;
6
use LegalThings\DataEnricher\Processor;
7
8
/**
9
 * Transform processor, apply transformation functions on data
10
 */
11
class Transform implements Processor
12
{
13
    use Processor\Implementation;
14
    
15
    /**
16
     * Allowed transformation functions
17
     * @var type 
18
     */
19
    public $allowed = [
20
        'hash',
21
        'base64_encode',
22
        'base64_decode',
23
        'json_encode',
24
        'json_decode',
25
        'serialize',
26
        'unserialize'
27
    ];
28
    
29
    /**
30
     * Apply processing to a single node
31
     * 
32
     * @param Node $node
33
     */
34
    public function applyToNode(Node $node)
35
    {
36
        $transformations = (array)$node->getInstruction($this);
37
        
38
        if (!isset($node->input)) {
39
            return;
40
        }
41
        
42
        $value = $node->input;
43
        
44
        if ($value instanceof Node) {
45
            $value = $value->getResult();
46
        }
47
        
48
        foreach ($transformations as $transformation) {
49
            list($fn, $arg) = explode(':', $transformation) + [null];
50
            
51
            if (!in_array($fn, $this->allowed)) {
52
                trigger_error("Unknown transformation '$transformation'", E_USER_WARNING);
53
                continue;
54
            }
55
            
56
            $value = isset($arg) ? $fn($arg, $value) : $fn($value);
57
        }
58
        
59
        $node->setResult($value);
60
    }
61
}
62