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
![]() |
|||
66 | $this->setText($text); |
||
67 | } |
||
68 | if ($numberOfItems != null) { |
||
0 ignored issues
–
show
|
|||
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
|
|||
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
|
|||
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 |