1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ups\Entity; |
4
|
|
|
|
5
|
|
|
use DOMDocument; |
6
|
|
|
use DOMElement; |
7
|
|
|
use Ups\NodeInterface; |
8
|
|
|
use Ups\Entity\UnitOfMeasurement; |
9
|
|
|
|
10
|
|
|
class ShipmentTotalWeight implements NodeInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var UnitOfMeasurement |
14
|
|
|
*/ |
15
|
|
|
private $unitOfMeasurement; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
private $weight; |
21
|
|
|
|
22
|
|
View Code Duplication |
public function __construct($response = null) |
|
|
|
|
23
|
|
|
{ |
24
|
|
|
if (null !== $response) { |
25
|
|
|
if (isset($response->unitOfMeasurement)) { |
26
|
|
|
$this->setUnitOfMeasurement(new UnitOfMeasurement($response->unitOfMeasurement)); |
27
|
|
|
} |
28
|
|
|
if (isset($response->Weight)) { |
29
|
|
|
$this->setWeight($response->Weight); |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param null|DOMDocument $document |
36
|
|
|
* |
37
|
|
|
* @return DOMElement |
38
|
|
|
*/ |
39
|
|
|
public function toNode(DOMDocument $document = null) |
40
|
|
|
{ |
41
|
|
|
if (null === $document) { |
42
|
|
|
$document = new DOMDocument(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$node = $document->createElement('ShipmentTotalWeight'); |
46
|
|
|
if ($this->getUnitOfMeasurement()) { |
47
|
|
|
$node->appendChild($this->getUnitOfMeasurement()->toNode($document)); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$node->appendChild($document->createElement('Weight', $this->getWeight())); |
51
|
|
|
|
52
|
|
|
return $node; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return UnitOfMeasurement |
57
|
|
|
*/ |
58
|
|
|
public function getUnitOfMeasurement() |
59
|
|
|
{ |
60
|
|
|
return $this->unitOfMeasurement; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param UnitOfMeasurement $unitOfMeasurement |
65
|
|
|
*/ |
66
|
|
|
public function setUnitOfMeasurement(UnitOfMeasurement $unitOfMeasurement) |
67
|
|
|
{ |
68
|
|
|
$this->unitOfMeasurement = $unitOfMeasurement; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return int |
73
|
|
|
*/ |
74
|
|
|
public function getWeight() |
75
|
|
|
{ |
76
|
|
|
return $this->weight; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param string $weight |
81
|
|
|
*/ |
82
|
|
|
public function setWeight($weight) |
83
|
|
|
{ |
84
|
|
|
if (!is_numeric($weight)) { |
85
|
|
|
throw new \Exception('Weight value should be a numeric value'); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$this->weight = $weight; |
|
|
|
|
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.