SerializerTrait   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 4
dl 0
loc 54
ccs 19
cts 19
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getSerializer() 0 10 2
A xmlSerialize() 0 16 2
A formatAmount() 0 8 2
1
<?php
2
3
namespace PHPSC\PagSeguro;
4
5
use JMS\Serializer\Annotation as Serializer;
6
use JMS\Serializer\Naming\IdenticalPropertyNamingStrategy;
7
use JMS\Serializer\Naming\SerializedNameAnnotationStrategy;
8
use JMS\Serializer\SerializerBuilder;
9
use SimpleXMLElement;
10
11
/**
12
 * @author Vinícius Fagundes <[email protected]>
13
 */
14
trait SerializerTrait
15
{
16
    /**
17
     * @Serializer\Exclude
18
     *
19
     * @var \JMS\Serializer\Serializer
20
     */
21
    private $serializer;
22
23
    /**
24
     * @return \JMS\Serializer\Serializer
25
     */
26 14
    public function getSerializer()
27
    {
28 14
        if ($this->serializer === null) {
29 14
            $this->serializer = SerializerBuilder::create()
30 14
                ->setPropertyNamingStrategy(new SerializedNameAnnotationStrategy(new IdenticalPropertyNamingStrategy()))
31 14
                ->build();
32
        }
33
34 14
        return $this->serializer;
35
    }
36
37
    /**
38
     * @param SimpleXMLElement|null $xmlRoot
39
     *
40
     * @return SimpleXMLElement
41
     */
42 14
    public function xmlSerialize(SimpleXMLElement $xmlRoot = null)
43
    {
44 14
        $xmlString = $this->getSerializer()->serialize($this, 'xml');
45 14
        $xmlObject = new SimpleXMLElement($xmlString);
46
47 14
        if ($xmlRoot === null) {
48 6
            return $xmlObject;
49
        }
50
51 8
        $domRoot = dom_import_simplexml($xmlRoot);
52 8
        $domObject = dom_import_simplexml($xmlObject);
53
54 8
        $domRoot->appendChild($domRoot->ownerDocument->importNode($domObject, true));
55
56 8
        return $xmlRoot;
57
    }
58
59 12
    protected final function formatAmount($amount)
60
    {
61 12
        if ($amount === null) {
62 3
            return null;
63
        }
64
65 9
        return number_format($amount, 2, '.', '');
66
    }
67
}
68