Completed
Push — master ( 91d601...da580a )
by Moesjarraf
04:29
created

Merge::merge()   C

Complexity

Conditions 8
Paths 17

Size

Total Lines 39
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 8.125

Importance

Changes 0
Metric Value
dl 0
loc 39
ccs 21
cts 24
cp 0.875
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 21
nc 17
nop 1
crap 8.125
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 6
    public function applyToNode(Node $node)
21
    {
22 6
        $instruction = $node->getInstruction($this);
23
        
24 6
        $list = $this->resolve($instruction);
25
        
26 6
        if (isset($list) && !is_array($list)) {
27 1
            throw new \Exception("Unable to apply {$this->property} processing instruction:"
28 1
                . " Expected an array, got a " . (is_object($list) ? get_class($list) . ' ' : '') . gettype($list));
29
        }
30
        
31 5
        $result = $this->merge((array)$list);
32 4
        $node->setResult($result);
33 4
    }
34
    
35
    /**
36
     * Resolve processing nodes in the instruction
37
     * 
38
     * @param array $list
39
     * @return array
40
     */
41 6
    public function resolve($list)
42
    {
43 6
        if ($list instanceof Node) {
44
            $list = $list->getResult();
45
        }
46
        
47 6
        if (is_array($list)) {
48 5
            foreach ($list as &$item) {
49 5
                if ($item instanceof Node) {
50 1
                    $item = $item->getResult();
51 1
                }
52
53 5
                if ($item instanceof \Traversable) {
54
                    $item = iterator_to_array($item);
55
                }
56 5
            }
57 5
        }
58
        
59 6
        return $list;
60
    }
61
    
62
    /**
63
     * Merge properties of an object
64
     * 
65
     * @param array $list
66
     * @return \stdClass|array|string
67
     */
68 5
    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...
69
    {
70 5
        if (empty($list)) {
71
            return null;
72
        }
73
        
74 5
        $scalar = [];
75
        
76 5
        foreach ($list as $key => &$item) {
77 5
            if (!isset($item)) {
78
                unset($list[$key]);
79
                continue;
80
            }
81
            
82 5
            if (is_object($item)) {
83 2
                $item = get_object_vars($item);
84 2
            }
85
            
86 5
            $scalar[] = is_scalar($item);
87 5
        }
88
89 5
        if (count(array_unique($scalar)) > 1) {
90 1
            throw new \Exception("Unable to apply {$this->property} processing instruction:"
91 1
                . " Mixture of scalar and non-scalar values");
92
        }
93
        
94 4
        if ($scalar[0]) {
95 1
            $result = join('', $list);
96 1
        } else {
97 3
            $result = call_user_func_array('array_merge', $list);
98
99
            // Is associative array
100 3
            if (array_keys($result) !== array_keys(array_keys($result))) {
101 2
                $result = (object)$result;
102 2
            }
103
        }
104
        
105 4
        return $result;
106
    }
107
}
108
109