JmesPath   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 0
cbo 3
dl 0
loc 44
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A applyToNode() 0 16 5
A objectivy() 0 12 5
1
<?php
2
3
namespace LegalThings\DataEnricher\Processor;
4
5
use LegalThings\DataEnricher\Node;
6
use LegalThings\DataEnricher\Processor;
7
use function JmesPath\search as jmespath_search;
8
use JmesPath\Utils;
9
10
/**
11
 * JMESPath processor
12
 * @see http://jmespath.org/
13
 */
14
class JmesPath implements Processor
15
{
16
    use Processor\Implementation;
17
    
18
    /**
19
     * Apply processing to a single node
20
     * 
21
     * @param Node $node
22
     */
23 2
    public function applyToNode(Node $node)
24
    {
25 2
        $instruction = $node->getInstruction($this);
26
        
27 2
        if (is_array($instruction) || is_object($instruction)) {
28 1
            $instruction = (object)$instruction;
29 1
        }
30
        
31 2
        $input = is_string($instruction) ? $node->getResult() : $instruction->input;
32 2
        $query = is_string($instruction) ? $instruction : $instruction->query;
33
        
34 2
        $result = jmespath_search($query, $input);
35 2
        static::objectivy($result);
36
        
37 2
        $node->setResult($result);
38 2
    }
39
    
40
    /**
41
     * Cast associated arrays to objects
42
     * 
43
     * @return mixed
44
     */
45 2
    protected static function objectivy(&$value)
46
    {
47 2
        if (Utils::isObject($value)) {
48 2
            $value = (object)$value;
49 2
        }
50
        
51 2
        if (is_array($value) || is_object($value)) {
52 2
            foreach ($value as &$item) {
53 2
                static::objectivy($item);
54 2
            }
55 2
        }
56 2
    }
57
}
58