Completed
Pull Request — develop (#41)
by Vinicius
03:09
created

SerializerTrait::getSerializer()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6667
cc 2
eloc 6
nc 2
nop 0
1
<?php
2
3
namespace PHPSC\PagSeguro\Requests;
4
5
use JMS\Serializer\SerializerBuilder;
6
use SimpleXMLElement;
7
use JMS\Serializer\Naming\SerializedNameAnnotationStrategy;
8
use JMS\Serializer\Naming\IdenticalPropertyNamingStrategy;
9
10
/**
11
 * @author Vinícius Fagundes <[email protected]>
12
 */
13
trait SerializerTrait
14
{
15
    /**
16
     * @var JMS\Serializer\Serializer
17
     */
18
    private $serializer;
19
20
    /**
21
     * @return JMS\Serializer\Serializer
22
     */
23
    public function getSerializer()
24
    {
25
        if (!$this->serializer) {
26
            $this->serializer = SerializerBuilder::create()
0 ignored issues
show
Documentation Bug introduced by
It seems like \JMS\Serializer\Serializ...ngStrategy()))->build() of type object<JMS\Serializer\Serializer> is incompatible with the declared type object<PHPSC\PagSeguro\R...\Serializer\Serializer> of property $serializer.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
27
                ->setPropertyNamingStrategy(new SerializedNameAnnotationStrategy(new IdenticalPropertyNamingStrategy()))
28
                ->build();
29
        }
30
        return $this->serializer;
31
    }
32
33
    /**
34
     * @return SimpleXMLElement
35
     */
36
    public function xmlSerialize(SimpleXMLElement $xmlRoot = null)
37
    {
38
        $xmlString = $this->getSerializer()->serialize($this, 'xml');
39
        $xmlObject = new SimpleXMLElement($xmlString);
40
41
        if ($xmlRoot===null) {
42
            return $xmlObject;
43
        }
44
45
        $domRoot = dom_import_simplexml($xmlRoot);
46
        $domObject = dom_import_simplexml($xmlObject);
47
48
        $domRoot->appendChild($domRoot->ownerDocument->importNode($domObject, true));
49
50
        return $xmlRoot;
51
    }
52
}
53