Issues (113)

lib/Elements/Address/Addr.php (10 issues)

1
<?php
2
3
4
/**
5
 * The MIT License
6
 *
7
 * Copyright 2018  Peter Gee <https://github.com/pgee70>.
8
 *
9
 * Permission is hereby granted, free of charge, to any person obtaining a copy
10
 * of this software and associated documentation files (the "Software"), to deal
11
 * in the Software without restriction, including without limitation the rights
12
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
 * copies of the Software, and to permit persons to whom the Software is
14
 * furnished to do so, subject to the following conditions:
15
 *
16
 * The above copyright notice and this permission notice shall be included in
17
 * all copies or substantial portions of the Software.
18
 *
19
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
 * FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE
22
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25
 * THE SOFTWARE.
26
 */
27
28
namespace i3Soft\CDA\Elements\Address;
29
30
/**
31
 *
32
 * @package     i3Soft\CDA
33
 * @author      Peter Gee <https://github.com/pgee70>
34
 * @link        https://github.com/pgee70/cda
35
 *
36
 */
37
38
39
use i3Soft\CDA\DataType\Code\AddressCodeType;
40
use i3Soft\CDA\Elements\AbstractElement;
41
42
/**
43
 * Class Addr
44
 *
45
 * @package i3Soft\CDA\Elements\Address
46
 */
47
class Addr extends AbstractElement
48
{
49
50
  /** @var AddressCodeType $address_code_use_attribute */
51
  protected $address_code_use_attribute;
52
  /** @var HouseNumber */
53
  private $house_number;
54
  /** @var StreetName */
55
  private $street_name;
56
  /** @var StreetNameType */
57
  private $street_name_type;
58
  /** @var StreetAddressLine */
59
  private $street_address_line;
60
  /** @var City */
61
  private $city;
62
  /** @var PostalCode */
63
  private $postal_code;
64
  /** @var State */
65
  private $state;
66
  /** @var Country */
67
  private $country;
68
  /** @var AdditionalLocator */
69
  private $additional_locator;
70
71
  /**
72
   * Addr constructor.
73
   *
74
   * @param null $street_address_line
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $street_address_line is correct as it would always require null to be passed?
Loading history...
75
   * @param null $city
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $city is correct as it would always require null to be passed?
Loading history...
76
   * @param null $state
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $state is correct as it would always require null to be passed?
Loading history...
77
   * @param null $postal_code
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $postal_code is correct as it would always require null to be passed?
Loading history...
78
   * @param null $additional_locator
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $additional_locator is correct as it would always require null to be passed?
Loading history...
79
   */
80
  public function __construct (
81
    $street_address_line = NULL,
82
    $city = NULL,
83
    $state = NULL,
84
    $postal_code = NULL,
85
    $additional_locator = NULL
86
  ) {
87
    if ($street_address_line)
0 ignored issues
show
$street_address_line is of type null, thus it always evaluated to false.
Loading history...
88
    {
89
      if ($street_address_line instanceof StreetAddressLine)
90
      {
91
        $this->setStreetAddressLine($street_address_line);
92
      }
93
      elseif (\is_string($street_address_line))
94
      {
95
        $this->setStreetAddressLine(new StreetAddressLine($street_address_line));
96
      }
97
    }
98
    if ($city)
0 ignored issues
show
$city is of type null, thus it always evaluated to false.
Loading history...
99
    {
100
      if ($city instanceof City)
101
      {
102
        $this->setCity($city);
103
      }
104
      elseif (\is_string($city))
105
      {
106
        $this->setCity(new City($city));
107
      }
108
    }
109
    if ($state)
0 ignored issues
show
$state is of type null, thus it always evaluated to false.
Loading history...
110
    {
111
      if ($state instanceof State)
112
      {
113
        $this->setState($state);
114
      }
115
      elseif (\is_string($state))
116
      {
117
        $this->setState(new State($state));
118
      }
119
    }
120
    if ($postal_code)
0 ignored issues
show
$postal_code is of type null, thus it always evaluated to false.
Loading history...
121
    {
122
      if ($postal_code instanceof PostalCode)
123
      {
124
        $this->setPostalCode($postal_code);
125
      }
126
      elseif (\is_string($postal_code))
127
      {
128
        $this->setPostalCode(new PostalCode($postal_code));
129
      }
130
    }
131
    if ($additional_locator)
0 ignored issues
show
$additional_locator is of type null, thus it always evaluated to false.
Loading history...
132
    {
133
      if ($additional_locator instanceof AdditionalLocator)
134
      {
135
        $this->setAdditionalLocator($additional_locator);
136
      }
137
      elseif (\is_string($additional_locator))
138
      {
139
        $this->setAdditionalLocator(new AdditionalLocator($additional_locator));
140
      }
141
    }
142
    $this->address_code_use_attribute = new AddressCodeType('');
143
  }
144
145
  /**
146
   * @return AddressCodeType
147
   */
148
  public function getUseAttribute (): AddressCodeType
149
  {
150
    return $this->address_code_use_attribute;
151
  }
152
153
  /**
154
   * @param $address_code_type
155
   *
156
   * @return Addr
157
   */
158
  public function setUseAttribute ($address_code_type): Addr
159
  {
160
    $this->address_code_use_attribute = $address_code_type instanceof AddressCodeType
161
      ? $address_code_type
162
      : new AddressCodeType($address_code_type);
163
    return $this;
164
  }
165
166
  /**
167
   * @param \DOMDocument $doc
168
   *
169
   * @return \DOMElement
170
   */
171
  public function toDOMElement (\DOMDocument $doc): \DOMElement
172
  {
173
    $el = $this->createElement($doc, ['address_code_use_attribute']);
174
    // if the street address line is present use it, otherwise see if the individual components were set.
175
    if ($this->hasStreetAddressLine())
176
    {
177
      $el->appendChild($this->getStreetAddressLine()->toDOMElement($doc));
178
    }
179
    else
180
    {
181
      if (NULL !== $this->house_number)
182
      {
183
        $el->appendChild($this->getHouseNumber()->toDOMElement($doc));
184
      }
185
      if (NULL !== $this->street_name)
186
      {
187
        $el->appendChild($this->getStreetName()->toDOMElement($doc));
188
      }
189
      if (NULL !== $this->street_name_type)
190
      {
191
        $el->appendChild($this->getStreetNameType()->toDOMElement($doc));
192
      }
193
    }
194
195
    if ($this->hasCity())
196
    {
197
      $el->appendChild($this->getCity()->toDOMElement($doc));
198
    }
199
    if ($this->hasState())
200
    {
201
      $el->appendChild($this->getState()->toDOMElement($doc));
202
    }
203
    if ($this->hasPostalCode())
204
    {
205
      $el->appendChild($this->getPostalCode()->toDOMElement($doc));
206
    }
207
    if ($this->hasAdditionalLocator())
208
    {
209
      $el->appendChild($this->getAdditionalLocator()->toDOMElement($doc));
210
    }
211
    if ($this->hasCountry())
212
    {
213
      $el->appendChild($this->getCountry()->toDOMElement($doc));
214
    }
215
    return $el;
216
  }
217
218
  /**
219
   * @return bool
220
   */
221
  public function hasStreetAddressLine (): bool
222
  {
223
    return $this->street_address_line !== NULL;
224
  }
225
226
  /**
227
   * @return StreetAddressLine
228
   */
229
  public function getStreetAddressLine (): StreetAddressLine
230
  {
231
    return $this->street_address_line;
232
  }
233
234
  /**
235
   * @param StreetAddressLine $street_address_line
236
   *
237
   * @return Addr
238
   */
239
  public function setStreetAddressLine (StreetAddressLine $street_address_line): Addr
240
  {
241
    $this->street_address_line = $street_address_line;
242
    return $this;
243
  }
244
245
  /**
246
   * @return HouseNumber
247
   */
248
  public function getHouseNumber (): HouseNumber
249
  {
250
    return $this->house_number;
251
  }
252
253
  /**
254
   * @param HouseNumber $house_number
255
   *
256
   * @return Addr
257
   */
258
  public function setHouseNumber (HouseNumber $house_number): Addr
259
  {
260
    $this->house_number = $house_number;
261
    return $this;
262
  }
263
264
  /**
265
   * @return StreetName
266
   */
267
  public function getStreetName (): StreetName
268
  {
269
    return $this->street_name;
270
  }
271
272
  /**
273
   * @param StreetName $street_name
274
   *
275
   * @return Addr
276
   */
277
  public function setStreetName (StreetName $street_name): Addr
278
  {
279
    $this->street_name = $street_name;
280
    return $this;
281
  }
282
283
  /**
284
   * @return StreetNameType
285
   */
286
  public function getStreetNameType (): StreetNameType
287
  {
288
    return $this->street_name_type;
289
  }
290
291
  /**
292
   * @param StreetNameType $street_name_type
293
   *
294
   * @return Addr
295
   */
296
  public function setStreetNameType (StreetNameType $street_name_type): Addr
297
  {
298
    $this->street_name_type = $street_name_type;
299
    return $this;
300
  }
301
302
  /**
303
   * @return bool
304
   */
305
  public function hasCity (): bool
306
  {
307
    return NULL !== $this->city;
308
  }
309
310
  /**
311
   * @return City
312
   */
313
  public function getCity (): City
314
  {
315
    return $this->city;
316
  }
317
318
  /**
319
   * @param City $city
320
   *
321
   * @return Addr
322
   */
323
  public function setCity (City $city): Addr
324
  {
325
    $this->city = $city;
326
    return $this;
327
  }
328
329
  /**
330
   * @return bool
331
   */
332
  public function hasState (): bool
333
  {
334
    return NULL !== $this->state;
335
  }
336
337
  /**
338
   * @return State
339
   */
340
  public function getState (): State
341
  {
342
    return $this->state;
343
  }
344
345
  /**
346
   * @param State $state
347
   *
348
   * @return Addr
349
   */
350
  public function setState (State $state): Addr
351
  {
352
    $this->state = $state;
353
    return $this;
354
  }
355
356
  /**
357
   * @return bool
358
   */
359
  public function hasPostalCode (): bool
360
  {
361
    return NULL !== $this->postal_code;
362
  }
363
364
  /**
365
   * @return PostalCode
366
   */
367
  public function getPostalCode (): PostalCode
368
  {
369
    return $this->postal_code;
370
  }
371
372
  /**
373
   * @param PostalCode $postal_code
374
   *
375
   * @return Addr
376
   */
377
  public function setPostalCode (PostalCode $postal_code): Addr
378
  {
379
    $this->postal_code = $postal_code;
380
    return $this;
381
  }
382
383
  /**
384
   * @return bool
385
   */
386
  public function hasAdditionalLocator (): bool
387
  {
388
    return NULL !== $this->additional_locator;
389
  }
390
391
  /**
392
   * @return AdditionalLocator
393
   */
394
  public function getAdditionalLocator (): AdditionalLocator
395
  {
396
    return $this->additional_locator;
397
  }
398
399
  /**
400
   * @param AdditionalLocator $additional_locator
401
   *
402
   * @return Addr
403
   */
404
  public function setAdditionalLocator (AdditionalLocator $additional_locator): Addr
405
  {
406
    $this->additional_locator = $additional_locator;
407
    return $this;
408
  }
409
410
  /**
411
   * @return bool
412
   */
413
  public function hasCountry (): bool
414
  {
415
    return NULL !== $this->country;
416
  }
417
418
  /**
419
   * @return Country
420
   */
421
  public function getCountry (): Country
422
  {
423
    return $this->country;
424
  }
425
426
  /**
427
   * @param Country $country
428
   *
429
   * @return Addr
430
   */
431
  public function setCountry (Country $country): Addr
432
  {
433
    $this->country = $country;
434
    return $this;
435
  }
436
437
  /**
438
   * @return string
439
   */
440
  protected function getElementTag (): string
441
  {
442
    return 'addr';
443
  }
444
}
445