Weight::isPounds()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php namespace SimpleUPS;
2
3
/**
4
 * The weight of a package
5
 * @since 1.0
6
 */
7
class Weight extends Model
8
{
9
    private
10
        /* @var string $code */
11
        $code = 'LBS',
12
13
        /* @var string $description */
14
        $description,
15
16
        /* @var float $weight */
17
        $weight;
18
19
    /**
20
     * @internal
21
     *
22
     * @param string $code
23
     *
24
     * @return Weight
25
     */
26
    public function setCode($code)
27
    {
28
        $this->code = (string)$code;
29
        return $this;
30
    }
31
32
    /**
33
     * @return string
34
     */
35
    public function getCode()
36
    {
37
        return $this->code;
38
    }
39
40
    /**
41
     * @internal
42
     *
43
     * @param string $description
44
     *
45
     * @return Weight
46
     */
47
    public function setDescription($description)
48
    {
49
        $this->description = (string)$description;
50
        return $this;
51
    }
52
53
    /**
54
     * @return string
55
     */
56
    public function getDescription()
57
    {
58
        return $this->description;
59
    }
60
61
    /**
62
     * @internal
63
     *
64
     * @param float $weight
65
     *
66
     * @return Weight
67
     */
68
    public function setWeight($weight)
69
    {
70
        $this->weight = (float)$weight;
71
        return $this;
72
    }
73
74
    /**
75
     * @return float
76
     */
77
    public function getWeight()
78
    {
79
        return $this->weight;
80
    }
81
82
    /**
83
     * Determine if the unit of measurement is LBS
84
     * @return bool
85
     */
86
    public function isPounds()
87
    {
88
        return $this->getCode() == 'LBS';
89
    }
90
91
    /**
92
     * Determine if the unit of measurement is KGS
93
     * @return bool
94
     */
95
    public function isKilograms()
96
    {
97
        return $this->getCode() == 'KGS';
98
    }
99
100
    /**
101
     * @internal
102
     *
103
     * @param \DomDocument $dom
104
     *
105
     * @return \DOMElement
106
     */
107
    public function toXml(\DomDocument $dom)
108
    {
109
        $packageWeight = $dom->createElement('PackageWeight');
110
111
        if ($this->getCode() != null) {
112
            $packageWeight->appendChild($unitOfMeasurement = $dom->createElement('UnitOfMeasurement'));
113
            $unitOfMeasurement->appendChild($dom->createElement('Code', $this->getCode()));
114
        }
115
116
        if ($this->getWeight() != null) {
117
            $packageWeight->appendChild($dom->createElement('Weight', $this->getWeight()));
118
        }
119
120
        return $packageWeight;
121
    }
122
123
    /**
124
     * @internal
125
     *
126
     * @param \SimpleXMLElement $xml
127
     *
128
     * @return Weight
129
     */
130
    public static function fromXml(\SimpleXMLElement $xml)
131
    {
132
        $shipmentWeight = new Weight();
133
        $shipmentWeight->setIsResponse();
134
        $shipmentWeight
135
            ->setCode($xml->UnitOfMeasurement->Code)
136
            ->setDescription($xml->UnitOfMeasurement->Description)
137
            ->setWeight($xml->Weight);
138
139
        return $shipmentWeight;
140
    }
141
}