Test Setup Failed
Push — master ( 581479...69dd87 )
by Vasil
02:14
created

Parser   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 0
dl 0
loc 128
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A outward() 0 4 1
A inward() 0 4 2
A area() 0 4 2
A district() 0 4 2
A subdistrict() 0 4 2
A sector() 0 4 2
A unit() 0 4 2
A parse() 0 4 1
1
<?php
2
/**
3
 * UK Postcode Parser
4
 *
5
 * @copyright Copyright (c) Vasil Dakov <[email protected]>
6
 * @license http://opensource.org/licenses/MIT MIT
7
 */
8
namespace VasilDakov\Postcode;
9
10
use VasilDakov\Postcode\Postcode;
11
12
class Parser implements ParserInterface
13
{
14
    /**
15
     * Regular expression pattern for Outward code
16
     */
17
    const OUTWARD     = "/\d[A-Za-z]{1,2}$/i";
18
19
20
    /**
21
     * Regular expression pattern for Inward code
22
     */
23
    const INWARD      = "/\d[A-Za-z]{2}$/i";
24
25
26
    /**
27
     * Regular expression pattern for Area code
28
     */
29
    const AREA        = "/^[A-Za-z]{1,2}/i";
30
31
32
    /**
33
     * Regular expression pattern for Sector code
34
     */
35
    const SECTOR      = "/^[A-Za-z]{1,2}\d[A-Za-z\d]?\s*\d/i";
36
37
38
    /**
39
     * Regular expression pattern for Unit code
40
     */
41
    const UNIT        =  "/[A-Za-z]{2}$/i";
42
43
44
    /**
45
     * Regular expression pattern for District code
46
     */
47
    const DISTRICT    = "/^([A-Za-z]{1,2}\d)([A-Za-z])$/i";
48
49
50
    /**
51
     * Regular expression pattern for Subdistrict code
52
     */
53
    const SUBDISTRICT = "/^([A-Za-z]{1,2}\d)([A-Za-z])$/i";
54
55
56
    /**
57
     * Postcode $postcode
58
     */
59
    private $postcode;
60
61
62
    /**
63
     * Constructor
64
     *
65
     * @param Postcode $postcode
66
     */
67
    public function __construct(Postcode $postcode)
68
    {
69
        $this->postcode = (string)$postcode;
70
    }
71
72
    /**
73
     * @return string $outward
74
     */
75
    public function outward()
76
    {
77
        return \trim(\preg_replace(self::OUTWARD, "", $this->postcode));
78
    }
79
80
    /**
81
     * @return string $inward
82
     */
83
    public function inward()
84
    {
85
        return (\preg_match(self::INWARD, $this->postcode, $matches)) ? $matches[0] : "";
86
    }
87
88
89
    /**
90
     * @return string $area
91
     */
92
    public function area()
93
    {
94
        return (\preg_match(self::AREA, $this->postcode, $matches)) ? $matches[0] : "";
95
    }
96
97
    /**
98
     * @return string $district
99
     */
100
    public function district()
101
    {
102
        return (\preg_match(self::DISTRICT, $this->outward(), $matches)) ? $matches[1] : "";
103
    }
104
105
    /**
106
     * Subdistrict code
107
     *
108
     * @return string  Example: "AA9A"
109
     */
110
    public function subdistrict()
111
    {
112
        return (\preg_match(self::REGEXP_SUBDISTRICT, $this->outward(), $matches)) ? $matches[0] : "";
113
    }
114
    
115
    /**
116
     * @return string $sector
117
     */
118
    public function sector()
119
    {
120
        return (\preg_match(self::SECTOR, $this->postcode, $matches)) ? $matches[0] : "";
121
    }
122
123
124
    /**
125
     * @return string $unit
126
     */
127
    public function unit()
128
    {
129
        return (\preg_match(self::UNIT, $this->postcode, $matches)) ? $matches[0] : "";
130
    }
131
132
    /**
133
     * @return array
134
     */
135
    public function parse()
136
    {
137
        return [];
138
    }
139
}
140