Completed
Push — develop ( a3939b...aae45c )
by Christian
02:23
created

XmlSerializer   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A serialize() 0 5 1
A contentType() 0 3 1
A toXml() 0 13 4
A active() 0 3 1
1
<?php
2
3
/**
4
 * Serializer for xml.
5
 * @author Christian Blank <[email protected]>
6
 */
7
class XmlSerializer extends Object implements IRestSerializer {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
8
9
    /**
10
     * @config
11
     */
12
    private static $is_active = true;
0 ignored issues
show
Unused Code introduced by
The property $is_active is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
13
    
14
    /**
15
     * The content type.
16
     * @var string
17
     */
18
    private $contentType = "application/xml";
19
20
    /**
21
     * Serializes the given data into a xml string.
22
     *
23
     * @param array $data the data that should be serialized
24
     * @return string a xml formatted string
0 ignored issues
show
Documentation introduced by
Should the return type not be string|false?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
25
     */
26
    public function serialize($data) {
27
        $xml = new SimpleXMLElement('<result/>');
28
        $this->toXml($xml, $data);
29
        return $xml->asXML();
30
    }
31
32
    public function contentType() {
33
        return $this->contentType;
34
    }
35
36
    private function toXml(SimpleXMLElement $object, array $data) {
37
        foreach( $data as $key => $value ) {
38
            if(is_array($value) ) {
39
                if( is_numeric($key) ){
40
                    $key = 'item'.$key; //dealing with <0/>..<n/> issues
41
                }
42
                $subnode = $object->addChild($key);
43
                $this->toXml($subnode, $value);
44
            } else {
45
                $object->addChild("$key",htmlspecialchars("$value"));
46
            }
47
        }
48
    }
49
50
    /**
51
     * Indicates if the serializer is active.
52
     * Serializers can be deactivated to use another implementation for the same mime type.
53
     *
54
     * @return boolean
0 ignored issues
show
Documentation introduced by
Should the return type not be array|integer|double|string|boolean?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
55
     */
56
    public function active() {
57
        return $this->config()->get('is_active');
58
    }
59
}
60