Completed
Push — master ( a7faa2...c7788e )
by Christian
9s
created

XmlSerializer::active()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
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