Completed
Push — master ( 503ac3...8ef7d4 )
by Sven
10s
created

JmesPath::objectivy()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 12
ccs 0
cts 10
cp 0
rs 8.8571
cc 5
eloc 6
nc 6
nop 1
crap 30
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
    public function applyToNode(Node $node)
24
    {
25
        $path = $node->getInstruction($this);
26
        $input = $node->getResult();
27
        
28
        $result = jmespath_search($path, $input);
29
        static::objectivy($result);
30
        
31
        $node->setResult($result);
32
    }
33
    
34
    /**
35
     * Cast associated arrays to objects
36
     * 
37
     * @return mixed
38
     */
39
    protected static function objectivy(&$value)
40
    {
41
        if (Utils::isObject($value)) {
42
            $value = (object)$value;
43
        }
44
        
45
        if (is_array($value) || is_object($value)) {
46
            foreach ($value as &$item) {
47
                static::objectivy($item);
48
            }
49
        }
50
    }
51
}
52