1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ShippoClient\Entity; |
4
|
|
|
|
5
|
|
|
use TurmericSpice\Container; |
6
|
|
|
use TurmericSpice\ReadableAttributes; |
7
|
|
|
|
8
|
|
|
class Address extends ObjectInformation |
9
|
|
|
{ |
10
|
|
|
use ReadableAttributes { |
11
|
|
|
mayHaveAsString as public getZip; |
12
|
|
|
mayHaveAsString as public getCountry; |
13
|
|
|
mayHaveAsString as public getPhone; |
14
|
|
|
mayHaveAsString as public getEmail; |
15
|
|
|
mayHaveAsString as public getIp; |
16
|
|
|
mayHaveAsString as public getMetadata; |
17
|
|
|
mayHaveAsArray as public getMessages; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
public function getName() |
21
|
|
|
{ |
22
|
|
|
return $this->mayHaveAsAsciiString('name'); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function getCompany() |
26
|
|
|
{ |
27
|
|
|
return $this->mayHaveAsAsciiString('company'); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function getStreet1() |
31
|
|
|
{ |
32
|
|
|
return $this->mayHaveAsAsciiString('street1'); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function getStreet2() |
36
|
|
|
{ |
37
|
|
|
return $this->mayHaveAsAsciiString('street2'); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function getStreetNo() |
41
|
|
|
{ |
42
|
|
|
return $this->mayHaveAsAsciiString('street_no'); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function getCity() |
46
|
|
|
{ |
47
|
|
|
return $this->mayHaveAsAsciiString('city'); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function getState() |
51
|
|
|
{ |
52
|
|
|
return $this->mayHaveAsAsciiString('state'); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
View Code Duplication |
public function getIsResidential() |
|
|
|
|
56
|
|
|
{ |
57
|
|
|
$is_residential = $this->attributes->mayHave('is_residential')->value(); |
|
|
|
|
58
|
|
|
if ($is_residential === null) { |
59
|
|
|
return null; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return (bool)$is_residential; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
private function mayHaveAsAsciiString($propertyName, callable $validate = null) |
66
|
|
|
{ |
67
|
|
|
return $this->transliterateToAscii( |
68
|
|
|
$this->attributes->mayHave($propertyName)->asString($validate) |
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
private function mustHaveAsAsciiString($propertyName, callable $validate = null) |
73
|
|
|
{ |
74
|
|
|
return $this->transliterateToAscii( |
75
|
|
|
$this->attributes->mustHave($propertyName)->asString($validate) |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
private function transliterateToAscii($str) |
80
|
|
|
{ |
81
|
|
|
$ret = ''; |
82
|
|
|
foreach (preg_split('//u', $str) as $ch) { |
83
|
|
|
$ch = @iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $ch); |
84
|
|
|
if (strlen($ch) > 1) { |
85
|
|
|
$ch = preg_replace('/[^0-9A-Za-z]/', '', $ch); |
86
|
|
|
} |
87
|
|
|
$ret .= $ch; |
88
|
|
|
} |
89
|
|
|
return $ret; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
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.