Test Setup Failed
Push — master ( 1989fc...da57ce )
by kouinkouin
01:42
created

StreetParser::getLineResult()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 2
1
<?php
2
3
namespace kouinkouin\StreetParser;
4
5
class StreetParser
6
{
7
    /**
8
     * @param string $fullStreet
9
     *
10
     * @return array
11
     */
12
    public function getStreetDataFromFullStreet($fullStreet)
13
    {
14
        $bestResult = ['score' => -1];
15
16
        foreach ($this->getRegexLines() as $regexLine) {
17
            $result = $this->getLineResult($regexLine, $fullStreet);
18
19
            if ($result['score'] > $bestResult['score']) {
20
                $bestResult = $result;
21
            }
22
        }
23
24
        return $bestResult;
25
    }
26
27
    /**
28
     * @return array
29
     */
30
    private function getRegexLines()
31
    {
32
        // Use https://www.regex101.com to debug regex :)
33
        return [
34
            [
35
                'regex'  => '/(.*)/', // basic
36
                'street' => 1,
37
                'number' => -1,
38
                'box'    => -1,
39
            ],
40
            [ // Belgian format. "," mandatory to separate the street and the number+box
41
              'regex'  => '/^(.*\w)\ *\,\ *(\d+\w*)[\/\ ]*(\d*\w*)$/',
42
              'street' => 1,
43
              'number' => 2,
44
              'box'    => 3,
45
            ],
46
            [ // Belgian format. Without "," to separate the street and the number. Don't detect the box
47
              'regex'  => '/^(.*[a-zA-Z])\ *(\d+\w*)([\/\ ]*)(\d*\w*)$/',
48
              'street' => 1,
49
              'number' => 2,
50
              'box'    => -1,
51
            ],
52
            [ // Belgian format. Without "," to separate the street and the number. Don't detect the box
53
              'regex'  => '/^(.*[a-zA-Z])\ *(\d+\w*)[\/\ ]+(\d+\w*)$/',
54
              'street' => 1,
55
              'number' => 2,
56
              'box'    => 3,
57
            ],
58
        ];
59
    }
60
61
    /**
62
     * @param array  $regexLine
63
     * @param string $fullStreet
64
     *
65
     * @return array
66
     */
67
    private function getLineResult($regexLine, $fullStreet)
68
    {
69
        if (preg_match($regexLine['regex'], $fullStreet, $matches)) {
70
            return $this->getMatchResult($matches, $regexLine);
71
        }
72
73
        return ['score' => -1];
74
    }
75
76
    /**
77
     * @param array $matches
78
     * @param array $regexLine
79
     *
80
     * @return array
81
     */
82
    private function getMatchResult(array $matches, array $regexLine)
83
    {
84
        $result = [];
85
        $score  = 0;
86
87
        foreach (['street', 'number', 'box'] as $streetItem) {
88
            if (isset($matches[$regexLine[$streetItem]])) {
89
                $score++;
90
                $result[$streetItem] = $matches[$regexLine[$streetItem]];
91
            } else {
92
                $result[$streetItem] = '';
93
            }
94
        }
95
96
        $result['score'] = $score;
97
98
        return $result;
99
    }
100
}
101