Completed
Push — master ( 059804...113f38 )
by recca
16:20 queued 15:00
created

Zipcode::__construct()   B

Complexity

Conditions 6
Paths 32

Size

Total Lines 28
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 6.031

Importance

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