Failed Conditions
Push — master ( e245a2...503ac3 )
by Arnold
8s
created

DataEnricher::getProcessorsFor()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 10
ccs 0
cts 6
cp 0
rs 9.4285
cc 2
eloc 5
nc 2
nop 2
crap 6
1
<?php
2
3
namespace LegalThings;
4
5
use LegalThings\DataEnricher\Node;
6
use LegalThings\DataEnricher\Processor;
7
use Jasny\DotKey;
8
9
/**
10
 * Enrich objects by processing special properties.
11
 */
12
class DataEnricher
13
{
14
    /**
15
     * Default processors
16
     * @var array
17
     */
18
    public static $defaultProcessors = [
19
        '<ifset>' => Processor\IfSet::class,
20
        '<ref>' => Processor\Reference::class,
21
        '<switch>' => Processor\SwitchChoose::class,
22
        '<merge>' => Processor\Merge::class,
23
        '<tpl>' => Processor\Mustache::class,
24
        '<src>' => Processor\Http::class,
25
        '<jmespath>' => Processor\JmesPath::class,
26
        '<transformation>' => Processor\Transform::class,
27
        
28
        // Deprecated
29
        '_ref' => Processor\Reference::class,
30
        '_switch' => Processor\SwitchChoose::class,
31
        '_src' => Processor\Http::class,
32
        '_merge' => Processor\Merge::class,
33
        '_jmespath' => Processor\JmesPath::class,
34
        '_tpl' => Processor\Mustache::class,
35
        '_transformation' => Processor\Transform::class
36
    ];
37
    
38
    
39
    /**
40
     * Processors, applied in specified order.
41
     * 
42
     * @var DataEnricher\Processor[]
43
     */
44
    public $processors;
45
    
46
    
47
    /**
48
     * Class constructor
49
     */
50 2
    public function __construct()
51
    {
52 2
        foreach (static::$defaultProcessors as $property => $processor) {
53
            if (is_string($processor)) {
54
                $processor = new $processor($property);
55
            }
56
            
57
            $this->processors[] = $processor;
58 2
        }
59 2
    }
60
    
61
    /**
62
     * Create processors
63
     * 
64
     * @param object       $source  Data source
65
     * @param array|object $target  Target or dot key path
66
     * @return Processor[]
67
     */
68
    protected function getProcessorsFor($source, $target)
69
    {
70
        $processors = [];
71
        
72
        foreach ($this->processors as $processor) {
73
            $processors[] = $processor->withSourceAndTarget($source, $target);
74
        }
75
        
76
        return $processors;
77
    }
78
    
79
    
80
    /**
81
     * Apply processing instructions
82
     * 
83
     * @param array|object|string $target  Target or dot key path
84
     * @param object              $source  Data source
85
     */
86 2
    public function applyTo($target, $source = null)
87
    {
88 2
        if (!isset($source)) {
89 2
            $source = $target;
90 2
        }
91
        
92 2
        if (!is_object($source)) {
93 2
            throw new \Exception("Data enricher on works on an object, not on a " . gettype($source));
94
        }
95
        
96
        if (is_string($target)) {
97
            $target = DotKey::on($source)->get($target);
98
        }
99
        
100
        $nodes = $this->findNodes($target);
101
        $processors = $this->getProcessorsFor($source, $target);
102
        
103
        foreach ($nodes as $node) {
104
            foreach ($processors as $processor) {
105
                $node->apply($processor);
106
            }
107
        }
108
        
109
        $this->applyNodeResults($target);
110
    }
111
112
    /**
113
     * Find nodes that have processing instructions
114
     * 
115
     * @param array|object $target
116
     * @return array
117
     */
118
    public function findNodes(&$target)
119
    {
120
        $nodes = [];
121
        
122
        foreach ($target as $key => &$value) {
123
            if (is_array($value) || (is_object($value) && !$value instanceof Node)) {
124
                $nodes = array_merge($nodes, $this->findNodes($value));
125
            }
126
            
127
            if ($value instanceof \stdClass && $this->hasProcessorProperty($value)) {
128
                $value = new Node($value);
129
                $nodes[] = $value;
130
            }
131
        }
132
        
133
        return $nodes;
134
    }
135
    
136
    /**
137
     * Check if object has at leas one process property
138
     * 
139
     * @param \stdClass    $value
140
     * @param Processor[]  $processors
0 ignored issues
show
Bug introduced by
There is no parameter named $processors. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
141
     * @return boolean
142
     */
143
    protected function hasProcessorProperty($value)
144
    {
145
        $processorProps = array_map(function ($processor) {
146
            return $processor->getProperty();
147
        }, $this->processors);
148
        
149
        $valueProps = array_keys(get_object_vars($value));
150
        return count(array_intersect($valueProps, $processorProps)) > 0;
151
    }
152
    
153
    /**
154
     * Replace nodes with their results
155
     * 
156
     * @param array|object $target
157
     */
158
    public function applyNodeResults(&$target)
159
    {
160
        foreach ($target as &$value) {
161
            if ($value instanceof Node) {
162
                $value = $value->getResult();
163
            } elseif (is_array($value) || is_object($value)) {
164
                $this->applyNodeResults($value);
165
            }
166
        }
167
    }
168
}
169