Completed
Push — master ( 4fd434...e1a522 )
by Bohuslav
03:35
created

Parser   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 2 Features 0
Metric Value
wmc 7
c 3
b 2
f 0
lcom 1
cbo 0
dl 0
loc 55
ccs 24
cts 24
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B parse() 0 24 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