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