Passed
Push — master ( 67b740...62de9e )
by Peter
03:54
created

JsonConverter   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 0
cbo 2
dl 0
loc 33
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 17 3
A processChild() 0 8 2
1
<?php
2
3
namespace PeterColes\XmlSoccer\Converters;
4
5
use stdClass;
6
7
class JsonConverter
8
{
9
    const STRINGS = [
10
        'AccountInformation'
11
    ];
12
13
    public function handle($xml)
14
    {
15
        $response = new stdClass;
16
17
        foreach ($xml->children() as $child) {
18
19
            $name = $child->getName();
20
21
            if (in_array($name, self::STRINGS)) {
22
                $response->$name = (string) $child;
23
            } else {
24
                $response->$name[] = $this->processChild($name, $child);
25
            }
26
        }
27
28
        return json_encode($response);
29
    }
30
31
    protected function processChild($name, $child)
32
    {
33
        if ($name == 'Match') {
34
            return (new Json\Match)->handle($child);
35
        }  
36
37
        return (new Json\Generic)->handle($child);
38
    }
39
}
40