Line::setText()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Bpost\BpostApiClient\Bpost\Order;
4
5
use Bpost\BpostApiClient\Common\XmlHelper;
6
use DomDocument;
7
use DOMElement;
8
use SimpleXMLElement;
9
10
/**
11
 * bPost Line class
12
 *
13
 * @author Tijs Verkoyen <[email protected]>
14
 */
15
class Line
16
{
17
    /**
18
     * @var string
19
     */
20
    private $text;
21
22
    /**
23
     * @var int
24
     */
25
    private $numberOfItems;
26
27
    /**
28
     * @param int $nbOfItems
29
     */
30
    public function setNumberOfItems($nbOfItems)
31
    {
32
        $this->numberOfItems = $nbOfItems;
33
    }
34
35
    /**
36
     * @return int
37
     */
38
    public function getNumberOfItems()
39
    {
40
        return $this->numberOfItems;
41
    }
42
43
    /**
44
     * @param string $text
45
     */
46
    public function setText($text)
47
    {
48
        $this->text = $text;
49
    }
50
51
    /**
52
     * @return string
53
     */
54
    public function getText()
55
    {
56
        return $this->text;
57
    }
58
59
    /**
60
     * @param string $text
61
     * @param int    $numberOfItems
62
     */
63
    public function __construct($text = null, $numberOfItems = null)
64
    {
65
        if ($text != null) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $text of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
66
            $this->setText($text);
67
        }
68
        if ($numberOfItems != null) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $numberOfItems of type integer|null against null; this is ambiguous if the integer can be zero. Consider using a strict comparison !== instead.
Loading history...
69
            $this->setNumberOfItems($numberOfItems);
70
        }
71
    }
72
73
    /**
74
     * Return the object as an array for usage in the XML
75
     *
76
     * @param DomDocument $document
77
     * @param string      $prefix
78
     *
79
     * @return DOMElement
80
     */
81
    public function toXML(DOMDocument $document, $prefix = null)
82
    {
83
        $line = $document->createElement(XmlHelper::getPrefixedTagName('orderLine', $prefix));
84
85
        if ($this->getText() !== null) {
0 ignored issues
show
introduced by
The condition $this->getText() !== null is always true.
Loading history...
86
            $line->appendChild(
87
                $document->createElement(
88
                    XmlHelper::getPrefixedTagName('text', $prefix),
89
                    $this->getText()
90
                )
91
            );
92
        }
93
        if ($this->getNumberOfItems() !== null) {
0 ignored issues
show
introduced by
The condition $this->getNumberOfItems() !== null is always true.
Loading history...
94
            $line->appendChild(
95
                $document->createElement(
96
                    XmlHelper::getPrefixedTagName('nbOfItems', $prefix),
97
                    $this->getNumberOfItems()
98
                )
99
            );
100
        }
101
102
        return $line;
103
    }
104
105
    /**
106
     * @param SimpleXMLElement $xml
107
     *
108
     * @return Line
109
     */
110
    public static function createFromXML(SimpleXMLElement $xml)
111
    {
112
        $line = new Line();
113
        if (isset($xml->text) && $xml->text !== '') {
114
            $line->setText((string) $xml->text);
115
        }
116
        if (isset($xml->nbOfItems) && $xml->nbOfItems !== '') {
117
            $line->setNumberOfItems((int) $xml->nbOfItems);
118
        }
119
120
        return $line;
121
    }
122
}
123