Completed
Push — master ( fe879a...880422 )
by Moesjarraf
36:15 queued 17:16
created

DataEnricher::getProcessorsFor()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2
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
        '<apply>' => Processor\JmesPath::class,
27
        '<transformation>' => Processor\Transform::class,
28
        '<math>' => Processor\Math::class,
29
        '<sum>' => Processor\Sum::class,
30
        '<enrich>' => Processor\Enrich::class,
31
        '<dateformat>' => Processor\DateFormat::class,
32
        '<numberformat>' => Processor\NumberFormat::class,
33
        '<equal>' => Processor\Equal::class,
34
        '<match>' => Processor\Match::class,
35
        '<replace>' => Processor\Replace::class,
36
        '<if>' => Processor\IfElse::class,
37
        '<join>' => Processor\Join::class,
38
        '<encode>' => Processor\Encode::class,
39
        '<decode>' => Processor\Decode::class,
40
        '<serialize>' => Processor\Serialize::class,
41
        '<unserialize>' => Processor\Unserialize::class,
42
        '<hash>' => Processor\Hash::class,
43
        
44
        // Deprecated
45
        '_ref' => Processor\Reference::class,
46
        '_switch' => Processor\SwitchChoose::class,
47
        '_src' => Processor\Http::class,
48
        '_merge' => Processor\Merge::class,
49
        '_jmespath' => Processor\JmesPath::class,
50
        '_tpl' => Processor\Mustache::class,
51
        '_transformation' => Processor\Transform::class
52
    ];
53
    
54
    
55
    /**
56
     * Processors, applied in specified order.
57
     * 
58
     * @var DataEnricher\Processor[]
59
     */
60
    public $processors;
61
    
62
    
63
    /**
64
     * Class constructor
65
     */
66 5
    public function __construct()
67
    {
68 5
        foreach (static::$defaultProcessors as $property => $processor) {
69 5
            if (is_string($processor)) {
70 5
                $processor = new $processor($property);
71 5
            }
72
            
73 5
            $this->processors[] = $processor;
74 5
        }
75 5
    }
76
    
77
    /**
78
     * Create processors
79
     * 
80
     * @param object       $source  Data source
81
     * @param array|object $target  Target or dot key path
82
     * @return Processor[]
83
     */
84 2
    protected function getProcessorsFor($source, $target)
85
    {
86 2
        $processors = [];
87
        
88 2
        foreach ($this->processors as $processor) {
89 2
            $processors[] = $processor->withSourceAndTarget($source, $target);
90 2
        }
91
        
92 2
        return $processors;
93
    }
94
    
95
    
96
    /**
97
     * Apply processing instructions
98
     * 
99
     * @param array|object|string $target  Target or dot key path
100
     * @param object              $source  Data source
101
     */
102 4
    public function applyTo($target, $source = null)
103
    {
104 4
        if (!isset($source)) {
105 3
            $source = $target;
106 3
        }
107
        
108 4
        if (!is_object($source)) {
109 2
            throw new \Exception("Data enricher on works on an object, not on a " . gettype($source));
110
        }
111
        
112 2
        if (is_string($target)) {
113
            $target = DotKey::on($source)->get($target);
114
        }
115
        
116 2
        $nodes = $this->findNodes($target);
117 2
        $processors = $this->getProcessorsFor($source, $target);
118
        
119 2
        foreach ($nodes as $node) {
120 2
            foreach ($processors as $processor) {
121 2
                $node->apply($processor);
122 2
            }
123 2
        }
124
        
125 2
        $this->applyNodeResults($target);
126 2
    }
127
128
    /**
129
     * Find nodes that have processing instructions
130
     * 
131
     * @param array|object $target
132
     * @return array
133
     */
134 2
    public function findNodes(&$target)
135
    {
136 2
        $nodes = [];
137
        
138 2
        foreach ($target as $key => &$value) {
139 2
            if (is_array($value) || (is_object($value) && !$value instanceof Node)) {
140 2
                $nodes = array_merge($nodes, $this->findNodes($value));
141 2
            }
142
            
143 2
            if ($value instanceof \stdClass && $this->hasProcessorProperty($value)) {
144 2
                $value = new Node($value);
145 2
                $nodes[] = $value;
146 2
            }
147 2
        }
148
        
149 2
        return $nodes;
150
    }
151
    
152
    /**
153
     * Check if object has at leas one process property
154
     * 
155
     * @param \stdClass    $value
156
     * @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...
157
     * @return boolean
158
     */
159
    protected function hasProcessorProperty($value)
160
    {
161 2
        $processorProps = array_map(function ($processor) {
162 2
            return $processor->getProperty();
163 2
        }, $this->processors);
164
        
165 2
        $valueProps = array_keys(get_object_vars($value));
166 2
        return count(array_intersect($valueProps, $processorProps)) > 0;
167
    }
168
    
169
    /**
170
     * Replace nodes with their results
171
     * 
172
     * @param array|object $target
173
     */
174 2 View Code Duplication
    protected function applyNodeResults(&$target)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
175
    {
176 2
        foreach ($target as &$value) {
177 2
            if ($value instanceof Node) {
178 2
                $value = $value->getResult();
179 2
            } elseif (is_array($value) || is_object($value)) {
180 2
                $this->applyNodeResults($value);
181 2
            }
182 2
        }
183 2
    }
184
}
185