Completed
Push — master ( 591c9a...4aa073 )
by Ben
02:54
created

Address::jsonSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
cc 1
eloc 8
c 2
b 0
f 2
nc 1
nop 0
dl 0
loc 11
rs 9.4285
1
<?php namespace SimpleUPS;
2
3
use SimpleUPS\Api\InvalidParameterException;
4
use JsonSerializable;
5
6
/**
7
 * Information about an address
8
 * This class is used as a container throughout the library to pass addresses back
9
 * and forth between objects.
10
 * @since 1.0
11
 */
12
class Address extends \SimpleUPS\Model implements JsonSerializable
13
{
14
15
    /**
16
     * @var string
17
     */
18
    private $street;
19
20
    /**
21
     * @var string
22
     */
23
    private $city;
24
25
    /**
26
     * @var string
27
     */
28
    private $stateProvinceCode;
29
30
    /**
31
     * @var string
32
     */
33
    private $postalCode;
34
35
    /**
36
     * @var string
37
     */
38
    private $postalCodeExtended;
39
40
    /**
41
     * @var string
42
     */
43
    private $countryCode;
44
45
    /**
46
     * Set the street
47
     *
48
     * @param string $street
49
     *
50
     * @return Address
51
     */
52
    public function setStreet($street)
53
    {
54
        $this->street = (string)$street;
55
        return $this;
56
    }
57
58
    /**
59
     * Street name and number (when applicable)
60
     * @return string
61
     */
62
    public function getStreet()
63
    {
64
        return $this->street;
65
    }
66
67
    /**
68
     * Set the city
69
     * Must be between 1-40 characters
70
     *
71
     * @param string $city
72
     *
73
     * @throws \SimpleUPS\Api\InvalidParameterException
74
     * @return Address
75
     */
76 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...
77
    {
78
        if (!$this->isResponse() && $city != null && strlen($city) > 40) {
79
            throw new InvalidParameterException('City must be between 1-40 characters');
80
        }
81
82
        $this->city = (string)$city;
83
        return $this;
84
    }
85
86
    /**
87
     * Get the city
88
     * @return string
89
     */
90
    public function getCity()
91
    {
92
        return $this->city;
93
    }
94
95
    /**
96
     * Set the state or province code
97
     * Must be 2 characters
98
     *
99
     * @param string $stateProvinceCode
100
     *
101
     * @throws \SimpleUPS\Api\InvalidParameterException
102
     * @return Address
103
     */
104 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...
105
    {
106
        if (!$this->isResponse() && $stateProvinceCode != null && strlen($stateProvinceCode) != 2)
107
            throw new InvalidParameterException('State/Province Code must be 2 characters');
108
109
        $this->stateProvinceCode = (string)$stateProvinceCode;
110
        return $this;
111
    }
112
113
    /**
114
     * Get the state or province
115
     * @return string
116
     */
117
    public function getStateProvinceCode()
118
    {
119
        return $this->stateProvinceCode;
120
    }
121
122
    /**
123
     * Set the postal code
124
     * Must be between 1-9 characters
125
     * @see setPostalCodeExtended
126
     *
127
     * @param string $postalCode
128
     *
129
     * @return Address
130
     * @throws \SimpleUPS\Api\InvalidParameterException
131
     */
132
    public function setPostalCode($postalCode)
133
    {
134
        if (!$this->isResponse() && $postalCode != null && (strlen($postalCode) < 1 || strlen($postalCode) > 9))
135
            throw new InvalidParameterException('Postal Code must be between 1-9 characters');
136
137
        if (!$this->isResponse() && strstr($postalCode, '-'))
138
            throw new InvalidParameterException(
139
                'Postal Code may not contain any special characters, use the extended postal code'
140
            );
141
142
        $this->postalCode = (string)$postalCode;
143
        return $this;
144
    }
145
146
    /**
147
     * Get the postal code
148
     * @see getPostalCodeExtended
149
     * @return string
150
     */
151
    public function getPostalCode()
152
    {
153
        return $this->postalCode;
154
    }
155
156
    /**
157
     * Set the extended postal code
158
     * @link http://en.wikipedia.org/wiki/ZIP_code#ZIP.2B4
159
     *
160
     * @param string $postalCodeExtended
161
     *
162
     * @return Address
163
     */
164
    public function setPostalCodeExtended($postalCodeExtended)
165
    {
166
        $this->postalCodeExtended = (string)$postalCodeExtended;
167
        return $this;
168
    }
169
170
    /**
171
     * The last segment of a zip code from the format xxxxx-xxxx
172
     * @link http://en.wikipedia.org/wiki/ZIP_code#ZIP.2B4
173
     * @return string
174
     */
175
    public function getPostalCodeExtended()
176
    {
177
        return $this->postalCodeExtended;
178
    }
179
180
    /**
181
     * Set the country
182
     * Must be 2 characters
183
     *
184
     * @param string $countryCode
185
     *
186
     * @return Address
187
     * @throws \SimpleUPS\Api\InvalidParameterException
188
     */
189 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...
190
    {
191
        if (!$this->isResponse() && $countryCode != null && strlen($countryCode) != 2)
192
            throw new InvalidParameterException('Country Code must be 2 characters');
193
194
        $this->countryCode = (string)$countryCode;
195
        return $this;
196
    }
197
198
    /**
199
     * Get the country code
200
     * @return string
201
     */
202
    public function getCountryCode()
203
    {
204
        return $this->countryCode;
205
    }
206
207
    /**
208
     * Create an address from XML.  SimpleXMLElement passed must have immediate children like AddressLine1, City, etc.
209
     * @internal
210
     *
211
     * @param \SimpleXMLElement $xml
212
     *
213
     * @return \SimpleUPS\Address
214
     */
215
    public static function fromXml(\SimpleXMLElement $xml)
216
    {
217
        $address = new Address();
218
        $address->setIsResponse();
219
220
        //@todo Consider alternatives for what to do with Consignee
221
        if (isset($xml->AddressLine1)) {
222
            $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...
223
            if (isset($xml->AddressLine2))
224
                $street .= ' ' . $xml->AddressLine2;
225
            if (isset($xml->AddressLine3))
226
                $street .= ' ' . $xml->AddressLine3;
227
228
            $address->setStreet(trim((string)$street));
229
        }
230
231
        if (isset($xml->City))
232
            $address->setCity($xml->City);
233
234
        if (isset($xml->StateProvinceCode))
235
            $address->setStateProvinceCode((string)$xml->StateProvinceCode);
236
237
        if (isset($xml->PostalCode))
238
            $address->setPostalCode((string)$xml->PostalCode);
239
240
        if (isset($xml->CountryCode))
241
            $address->setCountryCode((string)$xml->CountryCode);
242
243
        return $address;
244
    }
245
246
    /**
247
     * @return array
248
     */
249
    public function jsonSerialize()
250
    {
251
        return array(
252
            'street' => $this->getStreet(),
253
            'city' => $this->getCity(),
254
            'stateProvinceCode' => $this->getStateProvinceCode(),
255
            'postalCode' => $this->getPostalCode(),
256
            'postalCodeExtended' => $this->getPostalCodeExtended(),
257
            'countryCode' => $this->getCountryCode(),
258
        );
259
    }
260
}
261