1 | <?php |
||
2 | |||
3 | namespace Bpost\BpostApiClient\Bpost\Order; |
||
4 | |||
5 | use Bpost\BpostApiClient\Common\XmlHelper; |
||
6 | use Bpost\BpostApiClient\Exception\BpostLogicException\BpostInvalidLengthException; |
||
7 | use DomDocument; |
||
8 | use DomElement; |
||
9 | use SimpleXMLElement; |
||
10 | |||
11 | /** |
||
12 | * bPost Customer class |
||
13 | * |
||
14 | * @author Tijs Verkoyen <[email protected]> |
||
15 | */ |
||
16 | class Customer |
||
17 | { |
||
18 | const TAG_NAME = 'customer'; |
||
19 | |||
20 | /** |
||
21 | * @var string |
||
22 | */ |
||
23 | private $name; |
||
24 | |||
25 | /** |
||
26 | * @var string |
||
27 | */ |
||
28 | private $company; |
||
29 | |||
30 | /** |
||
31 | * @var Address |
||
32 | */ |
||
33 | private $address; |
||
34 | |||
35 | /** |
||
36 | * @var string |
||
37 | */ |
||
38 | private $emailAddress; |
||
39 | |||
40 | /** |
||
41 | * @var string |
||
42 | */ |
||
43 | private $phoneNumber; |
||
44 | |||
45 | /** |
||
46 | * @param \Bpost\BpostApiClient\Bpost\Order\Address $address |
||
47 | */ |
||
48 | public function setAddress($address) |
||
49 | { |
||
50 | $this->address = $address; |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * @return \Bpost\BpostApiClient\Bpost\Order\Address |
||
55 | */ |
||
56 | public function getAddress() |
||
57 | { |
||
58 | return $this->address; |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * @param string $company |
||
63 | */ |
||
64 | public function setCompany($company) |
||
65 | { |
||
66 | $this->company = $company; |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * @return string |
||
71 | */ |
||
72 | public function getCompany() |
||
73 | { |
||
74 | return $this->company; |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * @param string $emailAddress |
||
79 | * |
||
80 | * @throws BpostInvalidLengthException |
||
81 | */ |
||
82 | public function setEmailAddress($emailAddress) |
||
83 | { |
||
84 | $length = 50; |
||
85 | if (mb_strlen($emailAddress) > $length) { |
||
86 | throw new BpostInvalidLengthException('emailAddress', mb_strlen($emailAddress), $length); |
||
87 | } |
||
88 | $this->emailAddress = $emailAddress; |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * @return string |
||
93 | */ |
||
94 | public function getEmailAddress() |
||
95 | { |
||
96 | return $this->emailAddress; |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * @param string $name |
||
101 | */ |
||
102 | public function setName($name) |
||
103 | { |
||
104 | $this->name = $name; |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * @return string |
||
109 | */ |
||
110 | public function getName() |
||
111 | { |
||
112 | return $this->name; |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * @param string $phoneNumber |
||
117 | * |
||
118 | * @throws BpostInvalidLengthException |
||
119 | */ |
||
120 | public function setPhoneNumber($phoneNumber) |
||
121 | { |
||
122 | $length = 20; |
||
123 | if (mb_strlen($phoneNumber) > $length) { |
||
124 | throw new BpostInvalidLengthException('phoneNumber', mb_strlen($phoneNumber), $length); |
||
125 | } |
||
126 | $this->phoneNumber = $phoneNumber; |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * @return string |
||
131 | */ |
||
132 | public function getPhoneNumber() |
||
133 | { |
||
134 | return $this->phoneNumber; |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * Return the object as an array for usage in the XML |
||
139 | * |
||
140 | * @param DomDocument $document |
||
141 | * @param string $prefix |
||
142 | * |
||
143 | * @return DomElement |
||
144 | */ |
||
145 | public function toXML(DOMDocument $document, $prefix = null) |
||
146 | { |
||
147 | $customer = $document->createElement(XmlHelper::getPrefixedTagName(static::TAG_NAME, $prefix)); |
||
148 | |||
149 | if ($this->getName() !== null) { |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
150 | $customer->appendChild( |
||
151 | $document->createElement( |
||
152 | 'common:name', |
||
153 | $this->getName() |
||
154 | ) |
||
155 | ); |
||
156 | } |
||
157 | if ($this->getCompany() !== null) { |
||
0 ignored issues
–
show
|
|||
158 | $customer->appendChild( |
||
159 | $document->createElement( |
||
160 | 'common:company', |
||
161 | $this->getCompany() |
||
162 | ) |
||
163 | ); |
||
164 | } |
||
165 | if ($this->getAddress() !== null) { |
||
166 | $customer->appendChild( |
||
167 | $this->getAddress()->toXML($document) |
||
168 | ); |
||
169 | } |
||
170 | if ($this->getEmailAddress() !== null) { |
||
0 ignored issues
–
show
|
|||
171 | $customer->appendChild( |
||
172 | $document->createElement( |
||
173 | 'common:emailAddress', |
||
174 | $this->getEmailAddress() |
||
175 | ) |
||
176 | ); |
||
177 | } |
||
178 | if ($this->getPhoneNumber() !== null) { |
||
0 ignored issues
–
show
|
|||
179 | $customer->appendChild( |
||
180 | $document->createElement( |
||
181 | 'common:phoneNumber', |
||
182 | $this->getPhoneNumber() |
||
183 | ) |
||
184 | ); |
||
185 | } |
||
186 | |||
187 | return $customer; |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * @param SimpleXMLElement $xml |
||
192 | * @param Customer $instance |
||
193 | * |
||
194 | * @return Customer |
||
195 | * |
||
196 | * @throws BpostInvalidLengthException |
||
197 | */ |
||
198 | public static function createFromXMLHelper(SimpleXMLElement $xml, Customer $instance) |
||
199 | { |
||
200 | if (isset($xml->name) && $xml->name != '') { |
||
201 | $instance->setName((string) $xml->name); |
||
202 | } |
||
203 | if (isset($xml->company) && $xml->company != '') { |
||
204 | $instance->setCompany((string) $xml->company); |
||
205 | } |
||
206 | if (isset($xml->address)) { |
||
207 | $instance->setAddress( |
||
208 | Address::createFromXML($xml->address) |
||
209 | ); |
||
210 | } |
||
211 | if (isset($xml->emailAddress) && $xml->emailAddress != '') { |
||
212 | $instance->setEmailAddress( |
||
213 | (string) $xml->emailAddress |
||
214 | ); |
||
215 | } |
||
216 | if (isset($xml->phoneNumber) && $xml->phoneNumber != '') { |
||
217 | $instance->setPhoneNumber((string) $xml->phoneNumber); |
||
218 | } |
||
219 | |||
220 | return $instance; |
||
221 | } |
||
222 | } |
||
223 |