Completed
Pull Request — master (#11)
by Arnold
03:47
created

JmesPath::objectivy()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5

Importance

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