Parser::parse()   B
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 24
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 21
cts 21
cp 1
rs 8.5125
c 0
b 0
f 0
cc 6
eloc 20
nc 6
nop 1
crap 6
1
<?php
2
3
namespace Kambo\Http\Message\Parser;
4
5
/**
6
 * Parse input data according their type
7
 */
8
class Parser
9
{
10
    /**
11
     * Data type
12
     *
13
     * @var string
14
     */
15
    private $type;
16
17
    /**
18
     * Object constructor
19
     *
20
     * @param string $type data type for parsing a proper parsing method will be used
21
     *                     according this value.
22
     *
23
     */
24 7
    public function __construct($type)
25
    {
26 7
        $this->type = $type;
27 7
    }
28
29
    /**
30
     * Parse data according their type.
31
     *
32
     * @param mixed $data data for parsing
33
     *
34
     * @return mixed Type of returned valus is based on type of data if the type is XML 
35
     *               an instance of SimpleXMLElement is returned, else an array is returned
36
     *               If the data type is not support a null is returned.
37
     */
38 7
    public function parse($data)
39
    {
40 7
        $parsedData = null;
41 7
        switch ($this->type) {
42 7
            case 'application/json':
43 1
                $parsedData = json_decode($data, true);
44 1
                break;
45 6
            case 'application/xml':
46 6
            case 'text/xml':
47 1
                $backup     = libxml_disable_entity_loader(true);
48 1
                $parsedData = simplexml_load_string($data);
49 1
                libxml_disable_entity_loader($backup);
50 1
                break;
51 5
            case 'application/x-www-form-urlencoded':
52 5
            case 'multipart/form-data':
53 3
                parse_str($data, $parsedData);
54 3
                break;
55 2
            default:
56 2
                $parsedData = null;
57 2
                break;
58 7
        }
59
60 7
        return $parsedData;
61
    }
62
}
63