SerializerTrait::formatAmount()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 8
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
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