Completed
Push — master ( 818df4...32aed8 )
by recca
02:37
created

Zipcode::__construct()   B

Complexity

Conditions 6
Paths 32

Size

Total Lines 28
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 6.5625

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 28
ccs 15
cts 20
cp 0.75
rs 8.439
cc 6
eloc 20
nc 32
nop 2
crap 6.5625
1
<?php
2
3
namespace Recca0120\Twzipcode;
4
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()
69
    {
70 4
        return $this->attributes['zip3'];
71
    }
72
73
    /**
74
     * zip5.
75
     *
76
     * @return string
77
     */
78 4
    public function zip5()
79
    {
80 4
        return $this->attributes['zip5'];
81
    }
82
83
    /**
84
     * county.
85
     *
86
     * @return string
87
     */
88 4
    public function county()
89
    {
90 4
        return $this->attributes['county'];
91
    }
92
93
    /**
94
     * district.
95
     *
96
     * @return string
97
     */
98 4
    public function district()
99
    {
100 4
        return $this->attributes['district'];
101
    }
102
103
    /**
104
     * address.
105
     *
106
     * @return string
107
     */
108 4
    public function address()
109
    {
110 4
        return $this->attributes['address'];
111
    }
112
113
    /**
114
     * shortAddress.
115
     *
116
     * @return string
117
     */
118 4
    public function shortAddress()
119
    {
120 4
        return $this->attributes['shortAddress'];
121
    }
122
123
    /**
124
     * parse.
125
     *
126
     * @return static
127
     */
128 4
    public static function parse($address, Rules $rules = null)
129
    {
130 4
        return new static($address, $rules);
131
    }
132
}
133