1 | <?php |
||
5 | class Zipcode |
||
6 | { |
||
7 | /** |
||
8 | * $address. |
||
9 | * |
||
10 | * @var Address |
||
11 | */ |
||
12 | protected $address; |
||
13 | |||
14 | /** |
||
15 | * $attributes. |
||
16 | * |
||
17 | * @var array |
||
18 | */ |
||
19 | public $attributes = [ |
||
20 | 'zip3' => null, |
||
21 | 'zip5' => null, |
||
22 | 'county' => null, |
||
23 | 'district' => null, |
||
24 | 'address' => null, |
||
25 | 'shortAddress' => null, |
||
26 | ]; |
||
27 | |||
28 | /** |
||
29 | * __construct. |
||
30 | * |
||
31 | * @param string $address |
||
32 | * @param \Recca0120\Twzipcode\Rules $rules |
||
33 | */ |
||
34 | 4 | public function __construct($address, Rules $rules = null) |
|
35 | { |
||
36 | 4 | $rules = $rules ?: new Rules(); |
|
37 | 4 | $this->address = new Address($address); |
|
38 | 4 | $zip = $rules->match($this->address); |
|
39 | |||
40 | 4 | if (strlen($zip) === 5) { |
|
41 | 4 | $this->attributes['zip3'] = substr($zip, 0, 3); |
|
42 | 4 | $this->attributes['zip5'] = $zip; |
|
43 | } else { |
||
44 | $this->attributes['zip3'] = $zip; |
||
45 | $this->attributes['zip5'] = $zip; |
||
46 | } |
||
47 | |||
48 | 4 | $this->attributes['county'] = empty($this->attributes['zip3']) === false |
|
49 | 4 | ? $this->address->flat(1, 0) |
|
50 | : null; |
||
51 | |||
52 | 4 | $this->attributes['district'] = empty($this->attributes['zip3']) === false |
|
53 | 4 | ? $this->address->flat(1, 1) |
|
54 | : null; |
||
55 | |||
56 | 4 | $this->attributes['shortAddress'] = empty($this->attributes['zip3']) === false |
|
57 | 4 | ? $this->address->flat(null, 2) |
|
58 | : $this->address->flat(); |
||
59 | |||
60 | 4 | $this->attributes['address'] = $this->address->flat(); |
|
61 | 4 | } |
|
62 | |||
63 | /** |
||
64 | * zip3. |
||
65 | * |
||
66 | * @return string |
||
67 | */ |
||
68 | 4 | public function zip3() |
|
72 | |||
73 | /** |
||
74 | * zip5. |
||
75 | * |
||
76 | * @return string |
||
77 | */ |
||
78 | 4 | public function zip5() |
|
82 | |||
83 | /** |
||
84 | * county. |
||
85 | * |
||
86 | * @return string |
||
87 | */ |
||
88 | 4 | public function county() |
|
92 | |||
93 | /** |
||
94 | * district. |
||
95 | * |
||
96 | * @return string |
||
97 | */ |
||
98 | 4 | public function district() |
|
102 | |||
103 | /** |
||
104 | * address. |
||
105 | * |
||
106 | * @return string |
||
107 | */ |
||
108 | 4 | public function address() |
|
112 | |||
113 | /** |
||
114 | * shortAddress. |
||
115 | * |
||
116 | * @return string |
||
117 | */ |
||
118 | 4 | public function shortAddress() |
|
122 | |||
123 | /** |
||
124 | * parse. |
||
125 | * |
||
126 | * @return static |
||
127 | */ |
||
128 | 4 | public static function parse($address, Rules $rules = null) |
|
132 | } |
||
133 |