Completed
Push — master ( 5bebc3...591c9a )
by Ben
9s
created

Address::getPostalCodeExtended()   A

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
use SimpleUPS\Api\InvalidParameterException;
4
5
/**
6
 * Information about an address
7
 * This class is used as a container throughout the library to pass addresses back
8
 * and forth between objects.
9
 * @since 1.0
10
 */
11
class Address extends \SimpleUPS\Model
12
{
13
14
    /**
15
     * @var string
16
     */
17
    private $street;
18
19
    /**
20
     * @var string
21
     */
22
    private $city;
23
24
    /**
25
     * @var string
26
     */
27
    private $stateProvinceCode;
28
29
    /**
30
     * @var string
31
     */
32
    private $postalCode;
33
34
    /**
35
     * @var string
36
     */
37
    private $postalCodeExtended;
38
39
    /**
40
     * @var string
41
     */
42
    private $countryCode;
43
44
    /**
45
     * Set the street
46
     *
47
     * @param string $street
48
     *
49
     * @return Address
50
     */
51
    public function setStreet($street)
52
    {
53
        $this->street = (string)$street;
54
        return $this;
55
    }
56
57
    /**
58
     * Street name and number (when applicable)
59
     * @return string
60
     */
61
    public function getStreet()
62
    {
63
        return $this->street;
64
    }
65
66
    /**
67
     * Set the city
68
     * Must be between 1-40 characters
69
     *
70
     * @param string $city
71
     *
72
     * @throws \SimpleUPS\Api\InvalidParameterException
73
     * @return Address
74
     */
75 View Code Duplication
    public function setCity($city)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
76
    {
77
        if (!$this->isResponse() && $city != null && strlen($city) > 40) {
78
            throw new InvalidParameterException('City must be between 1-40 characters');
79
        }
80
81
        $this->city = (string)$city;
82
        return $this;
83
    }
84
85
    /**
86
     * Get the city
87
     * @return string
88
     */
89
    public function getCity()
90
    {
91
        return $this->city;
92
    }
93
94
    /**
95
     * Set the state or province code
96
     * Must be 2 characters
97
     *
98
     * @param string $stateProvinceCode
99
     *
100
     * @throws \SimpleUPS\Api\InvalidParameterException
101
     * @return Address
102
     */
103 View Code Duplication
    public function setStateProvinceCode($stateProvinceCode)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
104
    {
105
        if (!$this->isResponse() && $stateProvinceCode != null && strlen($stateProvinceCode) != 2)
106
            throw new InvalidParameterException('State/Province Code must be 2 characters');
107
108
        $this->stateProvinceCode = (string)$stateProvinceCode;
109
        return $this;
110
    }
111
112
    /**
113
     * Get the state or province
114
     * @return string
115
     */
116
    public function getStateProvinceCode()
117
    {
118
        return $this->stateProvinceCode;
119
    }
120
121
    /**
122
     * Set the postal code
123
     * Must be between 1-9 characters
124
     * @see setPostalCodeExtended
125
     *
126
     * @param string $postalCode
127
     *
128
     * @return Address
129
     * @throws \SimpleUPS\Api\InvalidParameterException
130
     */
131
    public function setPostalCode($postalCode)
132
    {
133
        if (!$this->isResponse() && $postalCode != null && (strlen($postalCode) < 1 || strlen($postalCode) > 9))
134
            throw new InvalidParameterException('Postal Code must be between 1-9 characters');
135
136
        if (!$this->isResponse() && strstr($postalCode, '-'))
137
            throw new InvalidParameterException(
138
                'Postal Code may not contain any special characters, use the extended postal code'
139
            );
140
141
        $this->postalCode = (string)$postalCode;
142
        return $this;
143
    }
144
145
    /**
146
     * Get the postal code
147
     * @see getPostalCodeExtended
148
     * @return string
149
     */
150
    public function getPostalCode()
151
    {
152
        return $this->postalCode;
153
    }
154
155
    /**
156
     * Set the extended postal code
157
     * @link http://en.wikipedia.org/wiki/ZIP_code#ZIP.2B4
158
     *
159
     * @param string $postalCodeExtended
160
     *
161
     * @return Address
162
     */
163
    public function setPostalCodeExtended($postalCodeExtended)
164
    {
165
        $this->postalCodeExtended = (string)$postalCodeExtended;
166
        return $this;
167
    }
168
169
    /**
170
     * The last segment of a zip code from the format xxxxx-xxxx
171
     * @link http://en.wikipedia.org/wiki/ZIP_code#ZIP.2B4
172
     * @return string
173
     */
174
    public function getPostalCodeExtended()
175
    {
176
        return $this->postalCodeExtended;
177
    }
178
179
    /**
180
     * Set the country
181
     * Must be 2 characters
182
     *
183
     * @param string $countryCode
184
     *
185
     * @return Address
186
     * @throws \SimpleUPS\Api\InvalidParameterException
187
     */
188 View Code Duplication
    public function setCountryCode($countryCode)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
189
    {
190
        if (!$this->isResponse() && $countryCode != null && strlen($countryCode) != 2)
191
            throw new InvalidParameterException('Country Code must be 2 characters');
192
193
        $this->countryCode = (string)$countryCode;
194
        return $this;
195
    }
196
197
    /**
198
     * Get the country code
199
     * @return string
200
     */
201
    public function getCountryCode()
202
    {
203
        return $this->countryCode;
204
    }
205
206
    /**
207
     * Create an address from XML.  SimpleXMLElement passed must have immediate children like AddressLine1, City, etc.
208
     * @internal
209
     *
210
     * @param \SimpleXMLElement $xml
211
     *
212
     * @return \SimpleUPS\Address
213
     */
214
    public static function fromXml(\SimpleXMLElement $xml)
215
    {
216
        $address = new Address();
217
        $address->setIsResponse();
218
219
        //@todo Consider alternatives for what to do with Consignee
220
        if (isset($xml->AddressLine1)) {
221
            $street = $xml->AddressLine1;
0 ignored issues
show
Bug introduced by
The property AddressLine1 does not seem to exist in SimpleXMLElement.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
222
            if (isset($xml->AddressLine2))
223
                $street .= ' ' . $xml->AddressLine2;
224
            if (isset($xml->AddressLine3))
225
                $street .= ' ' . $xml->AddressLine3;
226
227
            $address->setStreet(trim((string)$street));
228
        }
229
230
        if (isset($xml->City))
231
            $address->setCity($xml->City);
232
233
        if (isset($xml->StateProvinceCode))
234
            $address->setStateProvinceCode((string)$xml->StateProvinceCode);
235
236
        if (isset($xml->PostalCode))
237
            $address->setPostalCode((string)$xml->PostalCode);
238
239
        if (isset($xml->CountryCode))
240
            $address->setCountryCode((string)$xml->CountryCode);
241
242
        return $address;
243
    }
244
}
245