XmlTransformer::arrayToXml()   B
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 1
Metric Value
c 3
b 1
f 1
dl 0
loc 20
rs 8.8571
cc 5
eloc 12
nc 5
nop 2
1
<?php
2
3
/**
4
 * Author: Nil Portugués Calderó <[email protected]>
5
 * Date: 8/31/15
6
 * Time: 9:33 PM.
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
namespace NilPortugues\Serializer\Transformer;
12
13
use DOMDocument;
14
use SimpleXMLElement;
15
16
/**
17
 * Class XmlTransformer.
18
 */
19
class XmlTransformer extends ArrayTransformer
20
{
21
    /**
22
     * @param mixed $value
23
     *
24
     * @return string
25
     */
26
    public function serialize($value)
27
    {
28
        $array = parent::serialize($value);
29
30
        $xmlData = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><data></data>');
31
        $this->arrayToXml($array, $xmlData);
32
        $xml = $xmlData->asXML();
33
34
        $xmlDoc = new DOMDocument();
35
        $xmlDoc->loadXML($xml);
36
        $xmlDoc->preserveWhiteSpace = false;
37
        $xmlDoc->formatOutput = true;
38
39
        return $xmlDoc->saveXML();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $xmlDoc->saveXML(); (string) is incompatible with the return type of the parent method NilPortugues\Serializer\...yTransformer::serialize of type array.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
40
    }
41
42
    /**
43
     * Converts an array to XML using SimpleXMLElement.
44
     *
45
     * @param array            $data
46
     * @param SimpleXMLElement $xmlData
47
     */
48
    private function arrayToXml(array &$data, SimpleXMLElement $xmlData)
49
    {
50
        foreach ($data as $key => $value) {
51
            if (\is_array($value)) {
52
                if (\is_numeric($key)) {
53
                    $key = 'sequential-item';
54
                }
55
                $subnode = $xmlData->addChild($key);
56
57
                $this->arrayToXml($value, $subnode);
58
            } else {
59
                $subnode = $xmlData->addChild("$key", "$value");
60
61
                $type = \gettype($value);
62
                if ('array' !== $type) {
63
                    $subnode->addAttribute('type', $type);
64
                }
65
            }
66
        }
67
    }
68
}
69