Cancelled
Push — master ( c4d9a4...768131 )
by Sven
503:54 queued 503:54
created

Merge::resolve()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4.128

Importance

Changes 1
Bugs 1 Features 1
Metric Value
c 1
b 1
f 1
dl 0
loc 14
ccs 8
cts 10
cp 0.8
rs 9.2
cc 4
eloc 7
nc 6
nop 1
crap 4.128
1
<?php
2
3
namespace LegalThings\DataEnricher\Processor;
4
5
use LegalThings\DataEnricher\Node;
6
use LegalThings\DataEnricher\Processor;
7
8
/**
9
 * Merge multiple object into one
10
 */
11
class Merge implements Processor
12
{
13
    use Processor\Implementation;
14
    
15
    /**
16
     * Apply processing to a single node
17
     * 
18
     * @param Node $node
19
     */
20 3
    public function applyToNode(Node $node)
21
    {
22 3
        $instruction = $node->getInstruction($this);
23
        
24 3
        $list = $this->resolve($instruction);
25
        
26 3
        $result = $this->merge($list);
27 3
        $node->setResult($result);
28 3
    }
29
    
30
    /**
31
     * Resolve processing nodes in the instruction
32
     * 
33
     * @param array $list
34
     * @return array
35
     */
36 3
    public function resolve($list)
37
    {
38 3
        if ($list instanceof Node) {
39
            $list = $list->getResult();
40
        }
41
        
42 3
        foreach ($list as &$item) {
43 3
            if ($item instanceof Node) {
44 1
                $item = $item->getResult();
45 1
            }
46 3
        }
47
        
48 3
        return $list;
49
    }
50
    
51
    /**
52
     * Merge properties of an object
53
     * 
54
     * @param array $list
55
     * @return \stdClass|array
56
     */
57 3
    protected function merge(array $list)
0 ignored issues
show
Coding Style Best Practice introduced by
Please use __construct() instead of a PHP4-style constructor that is named after the class.
Loading history...
58
    {
59 3
        foreach ($list as &$item) {
60 3
            if (is_object($item)) {
61 2
                $item = get_object_vars($item);
62 2
            }
63 3
        }
64
        
65 3
        $result = call_user_func_array('array_merge', $list);
66
        
67
        // Is associative array
68 3
        if (array_keys($result) !== array_keys(array_keys($result))) {
69 2
            $result = (object)$result;
70 2
        }
71
        
72 3
        return $result;
73
    }
74
}
75